I have been studying the design pattern for a while, and I have been reading it for the second time, but I am afraid to write it again. Forget it. Take a simple start.
The simple factory mode is a simple and basic design mode in the design mode.
Definition:
A factory class dynamically determines which product class (these product classes inherit from a parent class or interface) to create based on input parameters.
Element:
Factory (Creator) Role
The core of the simple factory mode is to implement the internal logic for creating all instances. The factory class can be directly called by the outside world to create the required product objects.
Abstract Product role
The parent class of all objects created in simple factory mode, which describes the common public interfaces of all instances.
Product (ConcreteProduct) Role
Is the creation target of the simple factory mode. All created objects are instances of a specific class that acts as this role.
Let's talk about the Automatic Beverage dispenser that our school just installed last year. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + ICAgINL7wc + 7 + Environment/LK7yfqy + tL7wc + Environment/Environment + qGxs/a1xNL7wc + wyaGjtvi/Environment + Environment/Environment + zcrHs + Environment/Environment + region/zs /bSu7j20vvBz8DgPC9wPgo8cD48cHJlIGNsYXNzPQ = "brush: java; "> abstract class Drink // beverage class {public abstract void GetDrink ();}
Then cola, Sprite, and other drinks inherit the beverage class.
Class CokeCola: Drink // cola class {public override void GetDrink () {Console. writeLine ("give you a bottle of cola") ;}} class Sprite: Drink // Sprite class {public override void GetDrink () {Console. writeLine ("give you a bottle of Sprites ");}}
Establish a beverage factory and decide to produce (instantiate) that kind of Beverage
Class DrinkFactory // create a beverage factory {public static Drink createDrink (stringdrink) {Drink drk = null; switch (drink) {case "Cola": drk = new CokeCola (); break; case "Sprite": drk = new Sprite (); break;} return drk ;}}
Finally, you took your coin and selected the "Cola" button, so the beverage machine was automatically sold to you to "vomit" a can of cola.
Class Program {static void Main (string [] args) {Drink drk; drk = DrinkFactory. createDrink ("Cola"); // select "Cola" drk. getDrink ();}}
When the number of product categories in the system increases, the requirements for the factory category to create different instances based on different conditions may arise. this kind of judgment on the condition is intertwined with the judgment on the specific product type, and it is difficult to avoid the spread of module functions, which is very unfavorable for system maintenance and expansion;
These shortcomings have been overcome in the factory method model.
Use Cases
The factory class is responsible for creating fewer objects;
The customer only knows the parameters passed into the factory class and does not care about how to create objects (logic;
Because simple factories are easy to violate the High Cohesion responsibility allocation principle, they are generally used only in simple cases.
Summary:
In the simple factory mode, different specific objects are generated according to different "requirements" of the client, and the methods of specific objects are called by using polymorphism.