Design Pattern series-Rule Pattern

Source: Internet
Author: User

Design Pattern series-Rule Pattern
Little story

In the previous article, we talked about the hotel. Because of its good operation, we have recently recruited many chefs and launched many dishes. At this time, every time we add a cook, we need to add a food information to our menu and add a new judgment to the simple factory, this table always needs to be modified or added.

When changing the factory, the hotel needs to suspend business and then modify the table. The boss is in a bad mood when the customers who come and go are unable to operate. So I thought of another way to solve the problem. The boss wrote the producers of each dish on the menu. After ordering the food, the customer only needed to tell the kitchen which cook the system needed to cook, after the cook finishes the meal, the cook selects personnel to deliver the dish to the customer.

In this way, when adding a dish, you do not need to suspend business all the time. You only need to stop it for a while modifying the menu, which greatly reduces the time spent on adding (maintaining) dishes ,,,

Rule Mode

The mode used by the boss is the policy mode. The user directly transmits the requirement to the strategy class (select a person in the kitchen), then the policy class calls the parent class of the kitchen, and the Child class is multi-qualified through the parent class, the result is then sent to the customer.

Among them, the customer sends the policy class to the customer's needs (passing the value), while the policy mode returns the specific value to the customer.

Concept of Rule Mode: It defines the algorithm family and encapsulates them separately so that they can be exchanged. This pattern does not affect the customers who use the algorithm.

Related code

// Client class Program {static void Main (string [] args) {// submit the menu to the user Console. WriteLine ("enter what you want to eat? We have tofu brain, scrambled eggs, and fried beans for your choice !! "); // Select the required menu string greens =" "; greens = Console. readLine (); chef Chef; // after calling, select Chef = greensoptionfactory. getchef (greens); // get the food string result = Chef. getfood; Console. writeLine ("hello, sir, this is yours: {0 }! ", Result) ;}// you can use this interface to call public class context {chef; // this method instantiates the specific public context (chef) {this. chef = chef;} // This method transfers the cook's food out of public void contextinterface () {chef. getfood ;}// defines the cook class (the parent class of the cook) public class chef {private string food = ""; // The Cook's cooking method public virtual string getfood {get {return food ;}}// the cook's public class cheftofu: chef {public override string getfood {get {return "bean curd brain" ;}}/// chef public class chefbean for fried beans: chef {public override string getfood {get {return "" ;}}// public class chefegg for scrambled eggs: chef {public override string getfood {get {return "scrambled eggs ";}}}}

The rule mode shares the same with the simple factory mode. Both the policy mode and the simple factory use polymorphism. by calling the parent class, different sub-classes are formed by instances. In our example, the simple factory mode and Policy mode both define the chef class, and there is a getfood class, which is empty here. In actual use, we use its subclass. When we call it, we directly call the parent class and get different outputs through the multi-state method.
Differences: 1. Simple factory ModeThe client needs to know two classes: the client first needs to find the greensoptionfactory (simple factory) class, then finds the chef class from the greensoptionfactory class, and calls the chef subclass method. It feels like the boss said to the Secretary, "you have to look for Lao Wang. I want him to do something. Then the secretary looked for Lao Wang, and the boss said to Lao Wang, "you can give me a scrambled eggs ". Old Wang will give the scrambled eggs to the boss; Rule ModeThe client only needs to know one class: the client only needs to find the context (policy) class and pass the parameter to it. Then the policy class can instantiate the subclass of the chef class and return the result to the client. The feeling is that the boss said to the secretary: You can get me fried eggs. Then the Secretary went to Lao Wang and asked him to cook something and then deliver it to the boss.

2.Simple factory ModeYou do not need to input the selection during use. The specific selection process is completed by the factory. For example, in our example, we only need to tell the simple factory what I want to eat, and the specific Chef should be determined by the simple factory class;Rule ModeThere is no judgment function, and a judgment object needs to be input. Therefore, in the policy mode, we need to tell the policy class which class is called by itself.


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.