Simple Factory and Factory method patterns

This is one of the pattern not born from GOF and most of the people considers this as the default Factory method pattern.

Here, we will just take out object creation process out of the client code and put into some other class.

1) Factory method pattern

The factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.

How does it differ from the simple factory?

The factory method pattern is the best-known use of factories and factory methods, but not all uses of factory methods are examples of the factory method pattern – only when inheritance is involved (a class implementing an interface, or derived class, implements a factory method) is it an example of the factory method pattern
More basic uses of factories are not examples of the factory method pattern, and may instead be referred to as the factory patternor a simple factory;these are discussed at factory

A Simple Factory is just a public static factory method creating objects based on some input parameters and returning the object. The return type could be as simple as a Class of the same type as the objects created. [When the return type is Interface or Abstract class, it becomes somewhat similar to factory method pattern. You use an interface as in first two examples, and you use Abstract class when derived class implements the abstract method of the abstract class – third example]

example1

example2

example3

 

2. Abstract Factory Pattern

517px-Abstract_factory.svg

https://en.wikipedia.org/wiki/Abstract_factory_pattern

code example

References:

Simple Factory pattern , Factory method pattern:
codeproject.com /Articles/716413/ Factory-Method-Pattern-vs-Abstract-Factory-Pattern

https://en.wikipedia.org/wiki/Factory_method_pattern

Abstract Factory Pattern:
https://en.wikipedia.org/wiki/Abstract_factory_pattern