[design-patterns] Factory Pattern. When to use factory methods?

Factory classes are useful for when the object type that they return has a private constructor, when different factory classes set different properties on the returning object, or when a specific factory type is coupled with its returning concrete type.

WCF uses ServiceHostFactory classes to retrieve ServiceHost objects in different situations. The standard ServiceHostFactory is used by IIS to retrieve ServiceHost instances for .svc files, but a WebScriptServiceHostFactory is used for services that return serializations to JavaScript clients. ADO.NET Data Services has its own special DataServiceHostFactory and ASP.NET has its ApplicationServicesHostFactory since its services have private constructors.

If you only have one class that's consuming the factory, then you can just use a factory method within that class.

Examples related to design-patterns

How to implement a simple scenario the OO way Implementing Singleton with an Enum (in Java) What is difference between MVC, MVP & MVVM design pattern in terms of coding c# Best Practices for mapping one object to another REST API Login Pattern When should we use Observer and Observable? How to implement a FSM - Finite State Machine in Java Function in JavaScript that can be called only once Thread Safe C# Singleton Pattern Repository Pattern Step by Step Explanation

Examples related to factory

AngularJS: factory $http.get JSON file Pass arguments to Constructor in VBA What does java:comp/env/ do? What is the basic difference between the Factory and Abstract Factory Design Patterns? Factory Pattern. When to use factory methods?

Examples related to factory-pattern

Implement a simple factory pattern with Spring 3 annotations What are the differences between Abstract Factory and Factory design patterns? What is the difference between Builder Design pattern and Factory Design pattern? Dependency Injection vs Factory Pattern Factory Pattern. When to use factory methods?

Examples related to factory-method

What are the differences between Abstract Factory and Factory design patterns? How to implement the factory method pattern in C++ correctly What is the basic difference between the Factory and Abstract Factory Design Patterns? What are static factory methods? What is the difference between Builder Design pattern and Factory Design pattern? Factory Pattern. When to use factory methods?