1.Topics‎ > ‎Architecture‎ > ‎Patterns‎ > ‎a.Design Patterns‎ > ‎B.Creational Patterns‎ > ‎

B.Factory Pattern

  1. Objective 
    1. It Create an objects/family of related object (like Pizza ingredients) with some special construction logic.
    2. Sole motive is to hides Object creation process from client.
  2. When to use 
    1. If object construction is complex and you want to abstract (hide)
    2. 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. 
  3. Factory Pattern Types 
    1. Simple Factory 
      1. 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.
      2. Further readings :  http://www.oodesign.com/factory-pattern.html
    2. Factory Method Pattern 
      1. 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)
      2. Further Readings : http://www.oodesign.com/factory-method-pattern.html
    3. Abstract Factory Pattern
      1. 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.
      2. Concrete factory actually implement actual object creation pattern. http://www.oodesign.com/abstract-factory-pattern.html
  4. Examples
    1. Pizza Factory Simulation 
    2. UI manager
  5. Special Consideration
    1. Factory Registration Issues 
      1. 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. 
        1. Compile Time Registration 
          1. This registration model is mainly switch case based and cannot be modified at run time. 
        2. Run time Registration 
          1. Class registration via Reflection 
            1. Loads component classes via reflection and assemblies can be configured via config files. 
          2. Class registration via Configuration files 
            1. It also uses reflection but configuration files map ID Vs classes and assemblies. 
          3. Plug in based Auto Class registration 
            1. 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.
    2. Factories as Singleton
      1. In most of the case factories are suppose to be singleton
    3. Factories and Dependency Inversion 
      1. Any implementation of Abstract Factory Pattern must obey Dependency inversion principle to avoid hard coupling. 
    4. Factories and Dependency Injection
      1. Any implementation of Factory Pattern must obey DI principle to avoid hard coupling.
    5. Factory Vs Factory Method Vs Abstract Factory
      1. Simple Factory: - Also called Simple Factory and Simply a class create any registered product. It is more about programming technique than design pattern.
      2. Factory Method :- A derived class creates just one type of concrete product
      3. Abstract Factory:
        1. Further extend Factory Method using inheritance logic and creates family of related products
        2. Core motivation is  modularization
    6. Side Effects of Factories
      1. Factories are usually it non-extensible and implemented in custom way making them less reusable.
    7. Factory Vs Containers
      1. 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    
    8. Static Vs Dynamic factories 
      1. 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.
    9. Container Vs Factory 
      1. TODO?
  6. Related Patterns 
    1. Dependency Injection
    2. Inversion of Control
    3. Service Locator Pattern