Rule mode and design principle, rule Mode Design Principle
If you want to understand deeply, we suggest you read headfirst in depth. This is really useful. If you just want to take a look at it, you can't actually learn anything. This article consists of two parts: 1. Application of policy mode in Petshop, 2. Small application in console)
1. Application of policy pattern in Petshop
As for the application in the program, I am also a newbie. I don't know when this mode is needed. I can refer to the policy mode in petshop to understand how to use it in real projects. My understanding of the policy mode in petshop is that there may be two synchronous and asynchronous data insertion methods when processing orders in it. Specifically, you can modify the method directly in the configuration file. Reading text is better than reading pictures. I drew a UML diagram myself. I don't use tools, but I mean the same.
This is the IBLLStrage interface in the IBLLStrategy class library. To implement synchronous asynchronous order insertion, if there is a new order insertion method, you can inherit these two classes to process synchronous asynchronous order classes. It inherits from IBLLStrage and implements its own insertion methods. In the order class in The BLL layer, ** static readonly PetShop. IBLLStrategy. IOrderStrategy orderInsertStrategy = LoadInsertStrategy (); The LoadInsertStrategy method determines whether to instantiate a synchronous or asynchronous transfer order. In The BLL logic layer, the insert method is changed in Order, so the change needs to be independent according to the design principles.
2. Small applications in the console
Instance Description: simulate a duck application. because there may be N kinds of ducks in the future, the ducks cannot fly. How can they fly? Can they be called ducks? How can they be called, if you only write two interfaces, Ifly and Iquack, The subclass either inherits or does not inherit, and the flight behavior cannot be controlled, because the flying action and calling method are ever changing. Design principle 1. identify the potential changes in the application and separate them. Do not mix them with the code that does not need to be changed. (In this example, the behavior of the duck is changed.) 2. for interface programming, rather than for implementing programming for interface programming, it really means for Super-type programming. With polymorphism, the program can be for Super-type programming, when declaring a class, it is usually a super class or an interface. This also means that when declaring the class, you don't need to care about the real objects to be executed in the future. For example, Animal is an abstract class, two subclasses of Dog and Cat inherit Animal. The program for implementation is: Cat c = new Cat (); c. meow (); // cat call mode Dog d = new Dog (); d. bark (); // The method of calling a Dog for interface programming is: Animal a = new Dog ();. makeSound (); // In makeSound, bark (); Animal a = new Cat ();. makeSound (); // calls in makeSound Use meow (); 3. Use multiple combinations and use less c # examples inherited based on the read headfirst policy mode.
Rule mode: defines algorithm families, which are encapsulated separately so that they can replace each other. The secondary mode allows algorithm changes to be independent of algorithm customers.
What problems does the policy mode generate to solve? When do I use the policy mode?
Application scenarios:
1. Multiple classes only differ in performance behavior. You can use the Strategy Mode to dynamically select the behavior to be executed during running. (For example, FlyBehavior and QuackBehavior)
2. Different policies (algorithms) need to be used in different situations, or they may be implemented in other ways in the future. (For example, the specific implementation of FlyBehavior and QuackBehavior can be changed or expanded at Will)
3. Hiding implementation details of specific policies (algorithms) from the customer (Duck) is completely independent of each other.
I hope it will be helpful to you...
What is the policy design model?
Strategy Pattern defines a series of algorithms that encapsulate each algorithm and can be replaced with each other, the policy mode allows algorithms to change independently of the customer applications that use them.
Policy mode is a behavior mode that processes different variants of an algorithm. by defining an algorithm interface or encapsulating an algorithm identifier in an abstract policy, a specific subclass of this abstract policy is implemented as a separate algorithm, that is, the specific policy. Rule mode uses multiple classes to differentiate different behaviors, and uses rule mode to avoid exposing complicated internal data structures related to algorithms. When an operation in a class appears in the form of multiple condition branch statements, you can use the policy mode to move the related condition branches into their specific policy classes to replace these condition statements, this reduces the complexity of system processing.
We recommend you to read an authoritative book on design patterns: "software secrets-the point of design patterns" Edited by Zheng AQI. The descriptions are in place, and the instances are easy to understand!
I wish you an early learning of the design model!