Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does this class comply to Factory method pattern?

public class ServiceFactory {
    private static ServiceFactory instance;
    private final DishService dishService = new DishService();
    private final OrderService orderService = new OrderService();
    private  final UserService userService = new UserService();

    private ServiceFactory() {
    }

    public static synchronized ServiceFactory getInstance() {
        if (instance == null) instance = new ServiceFactory();
        return instance;
    }

    public DishService getDishService() {
        return dishService;
    }

    public OrderService getOrderService() {
        return orderService;
    }

    public UserService getUserService() {
        return userService;
    }
}

It is just a simple class for getting objects, but it seems like a messed up with naming.

If not, what should be a proper name for such a class?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Since you are essentially accessing the same instance of a set of xxxxService instead of "constructing" new ones, it does not comply with the factory pattern.

Naming it SharedServices makes more sense, given that you made it a singleton, and that it shares various instances of services across the application.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading