B.Factory Pattern
Objective
It Create an objects/family of related object (like Pizza ingredients) with some special construction logic.
Sole motive is to hides Object creation process from client.
When to use
If object construction is complex and you want to abstract (hide)
If you have many objects of the same base type and you manipulate them mostly by casting to abstract types, then you need a factory.
Factory Pattern Types
Simple Factory
Client directly holds Concrete Factory and request object instance, factory creates and return abstract instance (interface). This is classic example of factory and use full when factories are not going to change in future.
Further readings : http://www.oodesign.com/factory-pattern.html
Factory Method Pattern
It is same as simple factory but concrete factories are derived from an abstract factory that allows organizing base line logic in base class. In this case Client still holds concrete factory and factory return abstract product instance (interface)
Further Readings : http://www.oodesign.com/factory-method-pattern.html
Abstract Factory Pattern
This is further augmentation of factory method pattern. In this pattern client holds an abstract factory instance that are capable to produce multiple component object such as automobile parts or pizza ingredients.
Concrete factory actually implement actual object creation pattern. http://www.oodesign.com/abstract-factory-pattern.html
Examples
Pizza Factory Simulation
UI manager
Special Consideration
Factory Registration Issues
Any types of factories are implanted with class registration mechanism in which factory is made aware about that class it has to support and each class has a unique name or ID and client request the instance using the ID. There are two main types of Class Registration.
Compile Time Registration
This registration model is mainly switch case based and cannot be modified at run time.
Run time Registration
Class registration via Reflection
Loads component classes via reflection and assemblies can be configured via config files.
Class registration via Configuration files
It also uses reflection but configuration files map ID Vs classes and assemblies.
Plug in based Auto Class registration
Factories again uses reflection but assemblies are loaded from a drop folder and client request directly using full class names. These mechanisms involve minimal maintenance and deployment overhead.
Factories as Singleton
In most of the case factories are suppose to be singleton
Factories and Dependency Inversion
Any implementation of Abstract Factory Pattern must obey Dependency inversion principle to avoid hard coupling.
Factories and Dependency Injection
Any implementation of Factory Pattern must obey DI principle to avoid hard coupling.
Factory Vs Factory Method Vs Abstract Factory
Simple Factory: - Also called Simple Factory and Simply a class create any registered product. It is more about programming technique than design pattern.
Factory Method :- A derived class creates just one type of concrete product
Abstract Factory:
Further extend Factory Method using inheritance logic and creates family of related products
Core motivation is modularization
Side Effects of Factories
Factories are usually it non-extensible and implemented in custom way making them less reusable.
Factory Vs Containers
Dependency Injection can be implemented using factory but because of limitations of factories it is suggested to use framework that provide Containers like Spring.Net . ref http://msdn.microsoft.com/en-us/magazine/cc163739.aspx#S1
Static Vs Dynamic factories
Static factory creator method should not be used, because being static it can't be changed/modified/extended. But if the static method picks info from some other source (like config file), it is OK.
Container Vs Factory
TODO?
Related Patterns