What:
Simplefactroypattern, a factory class dynamically decides which product class to create based on the parameters passed in (these product classes inherit from a class or interface).
Why:
Encapsulates the details of creating objects, and the client calls only to focus on the desired object, without having to worry about the details of the creation and reducing dependencies between classes.
How:
Roles included in a simple factory and their responsibilities
Factory Class (Factory): The core of the simple factory model, responsible for the creation of internal logic for all forces, the factory class can be called directly by the external to create the desired object.
Abstract Product class: The parent class of all the specific product classes in the simple factory model, which is responsible for describing the attributes and behaviors common to all instances.
Specific product class (CONCRETEPRODUCT): Create a meter in a simple Factory mode, and all created objects act as instances of a specific class that you feel.
Before using the Simple Factory mode: The class diagram looks like this, and the client is highly dependent on the specific product class.
Suppose there is a scenario: now a company owner wants to listen to a few positions on the future development of the company's views, so the need for different positions of staff to talk about the relevant views board, the boss analysis of the positions of staff on the company's development prospects.
in real life, a little bigger business, the boss will have an assistant, the boss told the assistant, and then let the assistant to inform the staff. In fact, here the boss is the client, the Secretary is the factory category, employees are abstract product category, the specific position of the staff is the specific product category.
If there is no secretary, what will the boss do? The boss needs to know the relevant employees in various positions, and then inform them to complete such a task, then the company's various positions of staff bosses must be clear, and to understand their contact, and then one by one inform the relevant staff, if the real boss so hands, that got the staff's opinion, There's not much time to think about how companies are going to develop.
So what's the benefit of adding a boss assistant? By the secretary to complete the details of the notice staff, inside how to shout the relevant staff by the Secretary to complete, the boss do not care, just to tell the secretary now need to which position of people to interview, which Post is here to pass the parameters of the factory method.
After using the Factory mode: The client is no longer dependent on the specific product class to achieve the purpose of decoupling.
Abstract Product Category: Employee class
Abstract class Employee { // Internal Abstract void putforwardsuggestions (); }
Specific Product Categories: engineers
class Engineer:employee { internaloverridevoid putforwardsuggestions () { Console.WriteLine (" Engineer Representative Opinion: Regular skills training to improve the staff's business level "); } }
Specific Product Categories: Finance
class Accountant:employee { internaloverridevoid putforwardsuggestions () { Console.WriteLine (" Financial representative Opinion: the introduction of financial software, will make the financial management of the company more convenient ");} }
Factory Category: Secretary
classAssistant {//find the employee who made the boss's position Internal StaticEmployee Lookforemployee (stringpositionname) {Employee result=NULL; if(Positionname = ="Engineer") {result=NewEngineer (); } Else if(Positionname = ="Accountant") {result=NewAccountant (); } returnresult; } }
Client calls:
class program { staticvoid Main (string[] args) { = Assistant.lookforemployee ("engineer"); Firstempolyee.putforwardsuggestions (); = Assistant.lookforemployee ("accountant"); Secondempolyee.putforwardsuggestions (); Console.ReadLine (); } }
Operation Result:
The disadvantage of the simple factory model: All the creation logic is concentrated in the factory class, as the specific product class continuously increases, each time needs to modify the factory class, violates the open closure principle, the expansibility is not good.
Simple Factory mode of design mode