Simple factory of design mode Reading Notes

Source: Internet
Author: User

--- Each mode is used to solve one or more types of problems, or to decouple the coupling relationship between certain objects, so that the tight coupling becomes a loose coupling relationship.
1. Preface (decoupling process)
When we were just getting started or getting started with object-oriented programmers or learners. We will use classes to describe something with the same attributes.
Such as Apple. It also has attributes such as name and skin. Then we will define an Apple ):

View Code
1 public class Apple 2 {3 public string Name {get; set;} 4 public Color Skin {get; set;} 5 6 public void Display () 7 {8 Console. write ("I'm an Apple"); 9} 10} Then when we want to get an apple, we get used to doing an event. The code and structure diagram are as follows:

View Code
1 Apple = new apple {Name = "Apple", Skin = "Green"} tightly coupled structure:

 

Figure 1-1

Then some people will think that since we have learned the apple class, Apple can write like this, bananas, grapes and so on. The Code is as follows:
 

View Code
1 public class Banana 2 {3 public string Name {get; set;} 4 public string Skin {get; set;} 5 6 public void Display () 7 {8 Console. write ("I'm a banana"); 9} 10} 11 12 public class Grape13 {14 public string Name {get; set;} 15 public string Skin {get; set ;} 16 17 public void Display () 18 {19 Console. write ("I'm grape"); 20} 21} multiple tightly coupled structure charts:

 

Figure 1-2

When you gradually become familiar with OO and find that this code is not good, you should use interfaces or abstract classes to implement (polymorphism) for a slight improvement ).
Therefore, a fruit interface is defined, and all fruits inherit it. The Code is as follows:
 

View Code
1 public Interface IFruit2 {3 public void Display (); 4}

Then there is a lot of code in the Code:
 

View Code
1 IFruit fruit; 2 if (fruitType. equal ("Apple") 3 {4 fruit = new Apple {Name = "Apple", Skin = "Green"}; 5} else if (fruitType. equal ("Banana") 6 {7 fruit = new Banana {Name = "Banana", Skin = "Yellow"}; 8} else if (fruitType. equal ("Grape") 9 {10 fruit = new Grape {Name = "Grape", Skin = "Grape"}; 11} uses the interface structure:

 

Figure 1-3

In this way, if I add a new type of fruit in the future, I need to modify the logic code in multiple places. This leads to a difficult code maintenance problem.
As a result, we can use an Orchard to solve this problem. The Orchard can provide us with fruit. The Code is as follows:
 

View Code
1 public class OrchardFactory 2 {3 public IFruit ProvideFruit (string fruitType) 4 {5 if (fruitType. equal ("Apple") 6 {7 return new Apple {Name = "Apple", Skin = "Green"}; 8} else if (fruitType. equal ("Banana") 9 {10 return new Banana {Name = "Banana", Skin = "Yellow"}; 11} else if (fruitType. equal ("Grape") 12 {13 return new Grape {Name = "Grape", Skin = "Grape" };14} 15} 16} 17

In this way, the above Code modification problem is solved, but a new problem is introduced, that is, each time a parameter is passed in, and the fruit method is provided.
Logic judgment is required every time. If there are many fruit types, the performance will be affected, so some modifications have been made to the orchard. The Code is as follows:
 

View Code
1 public class OrchardFactory 2 {3 public IFruit ProvideApple () 4 {5 return new Apple {Name = "Apple", Skin = "Green"}; 6} 7 public IFruit ProvideBanana () 8 {9 return new Banana {Name = "Banana", Skin = "Yellow"}; 10} 11 public IFruit ProvideGrape () 12 {13 return new Grape {Name = "Grape", Skin = "Grape" };14} 15} 16 simple factory structure:

 

Figure 1-4

This leads to a design model "simple factory ".


2. Summary
What is a simple factory? In fact, a simple factory is not strictly a design pattern, but a programming habit.
A simple factory is a creation mode, mainly for customers (here the customer is not a person, but the object requestor in the Program) provide different products required by the customer with the same parent class or the same interface (this can be considered as the same product family ).


3. structure chart analysis:
Figure 1-1 The product is completely coupled with the client. The customer must know how the specific product is created.
Figure 1-2 coupling between multiple products and clients so that the client must remember each product.
Figure 1-3 The fruit interface is coupled with the client, and the client does not need to know any products. But one bad thing is that the client will have repeated code in multiple places.
Figure 1-4 complete simple factory diagram: the fruit interface is coupled with the factory, and the customer is coupled with the factory. This completely decouples the customer from the specific product, and changes the factory to the specific product to the send coupling relationship.

 

4. Static Factory:
If you add a keyword of static variables to a simple factory method, this factory is called a static factory.
Advantage: it is easy to use. You can create a product without dynamically instantiating the factory.
Disadvantage: static keywords do not support inheritance. Therefore, subclass (derived class) does not have static attributes and static methods of the parent class (base class.


5. Purpose:
The purpose of a simple factory is to solve the direct dependency between objects and decouple the tight coupling between objects.
From the code point of view, the main problem is to solve the problem of repeated wheel creation. That is to say, the same code appears repeatedly in the Code, making the code increasingly difficult to maintain.
To replace the Copy code.


6. usage scenarios:
Abstract: It is applicable to different products (same product family) that meet product requestor, product provider, and provide the same parent class or interface ). If the preceding relationship exists
You can use a simple factory.
From the perspective of practical application: This method can be used together with other design modes for example, creating a connection pool for a database.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.