Simple factory mode and Design Mode

Source: Internet
Author: User

Simple factory mode and Design Mode
Simple factory mode in Design Mode

The design pattern is the only way for C # programmers to grow from drag controls to technical giants. After the baptism of Head First, they will stay in the blog garden as a breeze.

OK. Let's proceed with the first task: invite friends to drink tea or coffee.

Sort out our ideas: to drink tea or coffee, you must cook boiled water, wash cups, and brew tea (coffee ). The boiling water and cup washing code can be reused. Consider this principle (to reuse the code and select an abstract class to implement the interface for selecting polymorphism). We choose to create an abstract class, it is used for boiling water and cup washing.

The Code is as follows:

1 using System; 2 3 namespace SimpleDrinkFactory 4 {5 abstract class BeforeDrink 6 {7 // boiled water 8 public void BoilWater () 9 {10 Console. writeLine ("boiled water"); 11} 12 // wash the cup 13 public void reset cup () 14 {15 Console. writeLine ("wash Cup"); 16} 17 // brewing (subclass inherited override) 18 public abstract void Function (); 19} 20}BeforeDrink 1 using System; 2 3 namespace SimpleDrinkFactory 4 {5 class Tea: BeforeDrink 6 {7 public override void Function () 8 {9 Console. writeLine ("tea brewing"); 10 // throw new NotImplementedException (); 11} 12} 13}Tea 1 using System; 2 3 namespace SimpleDrinkFactory 4 {5 class Coffee: BeforeDrink 6 {7 public override void Function () 8 {9 Console. writeLine ("brewed coffee"); 10 // throw new NotImplementedException (); 11} 12} 13}Coffee 1 using System; 2 3 namespace SimpleDrinkFactory 4 {5 class Program 6 {7 static void Main (string [] args) 8 {9 Console. writeLine ("My friend came to my house to play on Sunday. I invite you to come and drink. "); 10 Console. writeLine ("select a drink: Tea or Coffee"); 11 string type = Console. readLine (); 12 BeforeDrink drink = null; 13 switch (type) 14 {15 case "Tea": 16 drink = new Tea (); 17 break; 18 case "Coffee ": 19 drink = new Coffee (); 20 break; 21 default: 22 Console. writeLine ("Sorry, no drinks selected"); 23 break; 24} 25 drink. boilWater (); 26 drink. round cup (); 27 drink. function (); 28 Console. writeLine ("friend drink" + type + ""); 29 Console. writeLine ("My friend is satisfied"); 30 Console. readKey (); 31} 32} 33}Main ()

It should be shown after execution

After a while, my father-in-law came to my house and called for a warm drink.

My father-in-law spoke about it. Please follow the steps below to re-modify our Drink program:

1. Add the alcohol class, inherit from BeforeDrink, and override the Function method.

2. Modify the Switch method in Main () and add the alcohol branch.

OK. After modification, my father-in-law can have a warm drink. But the mother-in-law is here, and she wants to name and drink brown sugar; her father is here, and she wants to name and drink...

Then we will find that the maintenance is too troublesome, and our Switch will also come to more branches.

At this time, we finally need to use our factory model.

Look at the UML diagram of the simple factory model (UML won't be used, sorry)

Write our code according to the above UML diagram.

The Tea, Coffee, and BeforeDrink classes remain unchanged. SimpleDrinkFactory classes are added and Main () is modified ();

1 namespace SimpleDrinkFactory 2 {3 class SimpleDrinkFactory 4 {5 public static BeforeDrink CreatDrink (string type) 6 {7 BeforeDrink drink = null; 8 if (type. equals ("Tea") 9 {10 drink = new Tea (); 11} 12 else if (type. equals ("Coffee") 13 {14 drink = new Coffee (); 15} 16 return drink; 17} 18} 19}SimpleDrinkFactory 1 using System; 2 3 namespace SimpleDrinkFactory 4 {5 class Program 6 {7 static void Main (string [] args) 8 {9 Console. writeLine ("My friend came to my house to play on Sunday. I invite you to come and drink. "); 10 Console. writeLine ("Enter the drink to drink:"); 11 string type = Console. readLine (); 12 BeforeDrink sdf = SimpleDrinkFactory. creatDrink (type); 13 sdf. boilWater (); 14 sdf. round cup (); 15 sdf. function (); 16 // switch (type) 17 // {18 // case "Tea": 19 // drink = new Tea (); 20 // break; 21 // case "Coffee": 22 // drink = new Coffee (); 23 // break; 24 // default: 25 // Console. writeLine ("Sorry, no drinks selected"); 26 // break; 27 //} 28 // drink. boilWater (); 29 // drink. reset cup (); 30 // drink. function (); 31 Console. writeLine ("friend drink" + type + ""); 32 Console. writeLine ("My friend is satisfied"); 33 Console. readKey (); 34} 35} 36}Main ()

The execution result is as follows:

From the code point of view, the actually changed part is to Copy the code in Main () to the newly added SimpleDrinkFactory class.

However, this is precisely a key idea of object-oriented design. (Do you still remember encapsulation, inheritance, and polymorphism)

In the future diary, we will encounter a simpler method, which allows our code to automatically create different instances without having to modify the Switch or if statement every time we add a new drink.

Ps: In the old saying, the only two most difficult tasks in the world are to put others' money into their pockets, and to put their thoughts into others' heads. The ancients did not bully me.


Design Mode simple factory mode factory refers to the place where the production instance is located. It is simple to name it createInstance () directly (). This method is usually static and includes parameters and return values. For example, if cattle, sheep, horses, and dogs all inherit the animal class, then the return value of createInstance () should be an animal (because the factory is used to produce animals, so the return value should also be an animal), and the parameter should be an animal name (so that the factory knows which animal you want to produce based on the name ). In this way, an animal instance can be generated based on the input animal name. CreateInstance implementation: switch (animal name) case BULL: return new ox (); case goat: return new goat (); case horse: return new horse (); case dog: return new dog ();
Design pattern, factory pattern principle factory pattern includes simple factory pattern, factory method pattern and abstract factory pattern. No matter which factory mode is actually a specific application of the OCP (Open-Closed Principle) Principle. OCP is open to extensions and closed to modifications. So the most important thing to understand and use the factory model is to clarify the OCP principles. Of course, more accurately, the simple factory model does not fully reflect the OCP principle, but it can be considered as a transition from a non-OCP design to An OCP design. Beginners should learn from the basic principles or principles of OO design rather than the design pattern. In fact, when you fully understand the principle of OOD, the design mode is very easy to understand, and you can even deduce the design mode or its instantiated variant of the scenario.

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.