Java circular references

前端 未结 4 1338
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 02:01

In the project im working on, people wrote services class to access DAO. Almost every business object has it\'s own service which use it\'s own DAO. On some services, we are usi

相关标签:
4条回答
  • 2021-01-21 02:41

    Can you separate out the "service" from the Constructor?

    Or in other words, lets say you have an OrderService and it needs to consult its own personal copy of an ItemService. Will this ItemService instance need it's own copy of the OrderService to fulfill the request from the OrderService calling it?

    Thus, it would be a sort of lazy initialization--don't create the new item unless and until you actually need it. And don't link up the additional service unless and until you need it.

    Second idea: can you pass a copy as part of the Constructor?

    e.g.:

    //Constructor of OrderService public OrderService() 
    {     orderDAO = DAOFactory.getDAOFactory().getOrderDAO();
          itemService = new ItemService(this); 
    }
    //Constructor of ItemService public ItemService(OrderService orderService) 
    {     itemDAO = DAOFactory.getDAOFactory().getItemDAO();
          this.orderService = orderService; 
    }
    

    Or possibly in the reverse direction?

    0 讨论(0)
  • 2021-01-21 02:46

    The Spring Framework solves this problem by using dependency injection. In short, what it does is to instantiate all the DAOs, and then set the dao-dependencies after instantiation, but before main business logic.

    If you have to do this manually, here's an example:

    /*
      OrderService
     */
    public OrderService ()
    {
         orderDAO = DAOFactory.getDAOFactory().getOrderDAO();
    }
    
    public setItemService (ItemService service)
    {
         itemService = service;
    }
    
    /*
      ItemService
     */
    public ItemService ()
    {
         itemDAO = DAOFactory.getDAOFactory().getItemDAO();
    }
    
    public setOrderService (OrderService service)
    {
         orderService = service;
    }
    
    /*
       Bring it together in some other class
     */
    ...
    // instantiate singletons
    orderService = new OrderService ();
    itemService = new ItemService ();
    
    // inject dependencies
    orderService.setItemService (itemService);
    itemService.setOrderService (orderService);
    
    0 讨论(0)
  • 2021-01-21 03:00

    Let the OrderService just do things with orders. Let the ItemService just do things with items. Then create a OrderItemService which combines the two.

    0 讨论(0)
  • 2021-01-21 03:02

    Yes, the "singleton pattern" along with lazy initialisation will do. Don't initialise services in the constructor, but in static getters:

    class OrderService {
      private static OrderService instance;
      private OrderDAO orderDAO;
    
      public OrderService() {
        orderDAO = DAOFactory.getDAOFactory().getOrderDAO();
      }
    
      public static synchronized OrderService getInstance() {
        if (instance == null) {
          instance = new OrderService();
        }
    
        return instance;
      }
    }
    

    As Jonathan stated, you can also inject services to other services, but that might not be needed. If synchronisation is prone to lead to a memory issue, you can resolve this using volatile. See also this answer here, elaborating on the "double-checked locking pattern" (be careful though, to get this right!)

    0 讨论(0)
提交回复
热议问题