Simple Factory Pattern (SimpleFactory)
implementation points: The factory has a method createProduct() that can produce different concrete products of the abstract product. Each specific product may require a different identification.
public static Operation createOperate(string operate) { Operation oper = null; switch (operate) { case "+": { oper = new OperationAdd(); break; } case "-": { oper = new OperationSub(); break; } case "*": { oper = new OperationMul(); break; } case "/": { oper = new OperationDiv(); break; } } return oper; } }
Factory Method Pattern (Factory Method)
GOF intent: Define an interface for creating objects and let subclasses decide which class to instantiate. (a concrete factory creates a concrete product)
Role:
Abstract Factory (Creator) role: Provides the factorymethod() method for creating products
Concrete Creator role: implement factorymethod() to create concrete products
Abstract Product role:
Concrete Product role: Concrete products correspond to a specific factory.
Implementation points:
A specific factory produces a specific product.
Abstract Factory Pattern (Abstract Factory)
GOF Intent: Provides an interface for creating a series of related or interdependent objects without specifying their concrete classes
Role:
AbstractFactory abstract factory: abstract factory provides an interface for producing A product and B product
concreteFactory1 concrete factory: concrete factory 1 produces concrete product A1 and concrete B1
AbstractProductA Abstract Product A:
prroductA1 specific product A1;
AbstractProductB Abstract Product B:
prroductB1 concrete product B1;
Implementation points:
Generally product A and product B are different components of a larger product. For example, the keyboard and mouse that make up a computer.
The difference between the three is as follows: