# Factory

Factories handle the details of object creation. By encapsulating object creation in one class we have only one place to make modifications when the implementation changes.

The factory should be the only part of the application that refers to concrete classes it creates.

## Factory Method

This is a method in a superclass without an implementation. Sub-classes must provide the implementation. This decouples the client code in the super-class from the object creation in sub-classes.

All factory patterns encapsulate object creation. The Factory Method patter encapsulates object creation by letting sub-classes decide what objects to create.

### Design Principle

Depend on abstractions. Don't depend on concrete classes. This is similar to "program to an interface not an implementation". But, it's a stronger statement.

### Dependency Inversion Principle

It says that our high-level components should not depend on low-level components. They both should depend on abstractions. A high-level component is one that is defined in terms of lower-level ones.

The "inversion" here refers to how low-level components depend on high-level abstractions. The high-level component also depends on the same abstraction.

### Guidelines For Following the Dependency Inversion Principle

* No variable should hold a reference to a concrete class
* No class should derive from a concrete class. If you derive from a concrete class, then you're depending on a concrete class. Instead derive from an abstraction.
* No method should override a method of its base class. Otherwise, the base class wasn't really an abstraction.

Note: the first two points don't always apply to Python because in Python you almost always depend on abstractions since there's no type checking by the interpreter.

## Abstract Factory

This patterns provides an interface for creating families of related or dependent objects without specifying their concrete classes. Often the methods of an Abstract Factory are implemented as Factory Methods.

Each method in this interface is responsible for creating a concrete class or product and we implement a sub-class that provides the implementation for these methods.

### Factory Method vs. Abstract Factory

Factory Method creates objects through inheritance, but Abstract Factory does it using object composition.

To create objects using Factory Method you extend a class and provide an implementation for a factory method. You're relying on a sub-class to provide the implementation. The client code knows only the abstract type of the created objects whereas sub-classes know the concrete type. This way clients are decoupled from concrete classes.

Objects that sub-classes create implement an interface. So, the sub-class that creates them doesn't depend on their concrete class but on the interface they implement.

Abstract Factory provides an abstract type for creating a group of products. Object creation is implemented in factory methods exposed in the factory interface. Sub-classes of this type define how these products are produced. To use the factory you instantiate one of these factories that have an implementation and pass it into some code that is written against the abstract type. The clients are decoupled from the actual products they use. The Abstract Factory has a big interface - it has a large number of abstract methods (not-implemented methods) - because there can many groups of products to create.

Here, sub-classes create a group of objects. Each one of these objects implements a different interface. That's why sub-classes implement different factory methods. Each factory method creates a different object.
