简体   繁体   中英

Design patterns - webapp without DI framework

I need to implement servlet 3.0 webapp without dependency injection framework. Which solution is the best practice ? (in terms of performance/memory usage and scalability)

1) static methods for DAO and service layer

@WebServlet("/welcome")
public class MyController extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {

        MyService.doSomething(/*args*/);
    }
}

public class MyService {

    public static void doSomething(/*args*/) {
        MyDAO.crud(/*args*/);
    }
}

public class MyDAO {

    public static void crud(/*args*/) {
        //...
    }
}

2) interfaces

@WebServlet("/welcome")
public class MyController extends HttpServlet {

    private MyService myService;

    public MyController() {
        super();
        if (myService != null) {
            myService = new MyServiceImpl();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {

        myService.doSomething(/*args*/);
    }
}

public interface MyService {

    void doSomething(/*args*/);
}

public class MyServiceImpl implements MyService {

    private MyDAO myDAO;

    public MyServiceImpl() {
        if (myService != null) {
            myDAO = new MyDAOImpl();
        }
    }

    @Override
    public void doSomething(/*args*/) {
        myDAO.crud(/*args*/);
    }
}

public interface MyDAO {

    void crud(/*args*/);
}

public class MyDAOImpl implements MyDAO {

    @Override
    public void crud(/*args*/) {
        //...
    }
}

3) something else...

Thanks.

Its best to make both MyService and MyDAO in your application as stateless . You can make both of them as singletons (which is any way the default scope of all spring injected beans ), and just use it in your controller . Since its stateless there is no problem with concurrency issues.

Don't use a static method as it can't be mocked or injected. Although assuming you dao is not in memory (and is not itself stateful) it would be threadsafe.

Why do you have to do it without a framework ?

See if you want to implement singleton than create a

class public class FetchBean {

 static Map<String, Object> beans; static { beans = new HashMap<String, Object>(); } public static Object getBean(Class bean) { try { if (!beans.containsKey(bean.getName())) { Object instance = bean.newInstance(); beans.put(bean.getName(), instance); } return beans.get(bean.getName()); } catch (Exception e) { e.printStackTrace(); } return null; } public static void main(String[] args) { Person person = (Person) FetchBean.getBean(Person.class); person.setName("Taher"); System.out.println(person.getName()); Person person2 = (Person) FetchBean.getBean(Person.class); person2.setName("Tinwala"); System.out.println(person2.getName()); } } 
public class Person {

    public Person() {
        System.out.println("In person");
    }

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM