Definition:
Defines an interface for creating objects, allowing subclasses to decide which class to instantiate. The factory method defers the instantiation of a class to its subclasses. Explanation:
The factory approach model abstracts The Simple factory model, which, depending on the reversal principle, abstracts factories and products, abstracting abstract factories (abstract classes or interfaces) and abstract products (abstract classes or interfaces). The instantiation of the product class is realized by the concrete factory that implements the abstract factory. class Diagram:
role:
Abstract Factory Class (Creator): An abstract class or interface that defines an abstract factory object and provides a way to create a product object.
The Concrete factory class (Concretecreator): Implements the class, implements the abstract factory class, realizes the method which creates the concrete product.
Abstract Product Class (product): An abstract class or interface that defines an abstract product object.
Specific product class (CONCRETEPRODUCT): Implement class, implement abstract product class, define specific Product object. Code:
Factory interface
interface Ifactory {public
ICar createcar ();
}
Product Interface
interface ICar {public
void run ();
}
Small car factory class
Smallcarfactory implements Ifactory {public
ICar Createcar () {return
new Smallcar ();
}
//Large auto factory class
Bigcarfactory implements Ifactory {public
ICar Createcar () {return
new Bigcar ();
}
Small car Products Class
Smallcar implements ICar {public
void run () {
System.out.println ("Car Startup");
}
//Large Auto Products class
Bigcar implements ICar {public
void run () {
System.out.println ("Big car Startup");
}
}
The client public
class Client {
private static ifactory factory1, Factory2;
private static ICar car1, car2;
public static void Main (string[] args) {
factory1 = new Smallcarfactory ();
Car1 = Factory1.createcar ();
Car1.run ()//car start
Factory2 = new Bigcarfactory ();
CAR2 = Factory2.createcar ();
Car2.run ()//Big car start
}
}
Benefits:Good encapsulation, the structure of the code is clear. The factory class encapsulates the product creation process and provides only the means to obtain the product. Good extensibility. When the system needs to add new products, only need to add a product class and a corresponding factory class. Shielding factory class and product class. Clients rely only on abstract factory classes and abstract product classes, not directly on specific factory classes and specific product classes. Reduce coupling. Because the client does not depend on the specific factory class and the specific product class, the client's code is not affected when the process of the specific factory class instantiation of the product changes.
Usage Scenarios:The factory method pattern is used as the creation pattern and can be substituted for new to create the object. Objects should have a public behavior method, and the creation of objects is relatively complex. If the object creation process is too simple, you do not have to use the factory method pattern and you can create it directly using new.