I have been studying the design model for a long time. In fact, I have already tried it again before, but I feel that I have not learned anything and have a deep understanding of it. Now I want to reconstruct the data center and try again, it's not too late.
In fact, after I knocked on the data center, I looked at the mode. In fact, it was hard to understand it in the past, and the code that was hard to understand can be understood at a Glance. I felt a little bit about it. Let's get it early.
Simple factory mode:
Simply put, the simple factory model can be used where it is easy to change.
Instance:
For example, when we shop in a mall, we offer promotions in the mall. Various promotions include 300 to 100 Off, off, and lucky draws.
Promotions are actually changes.
Another example: When we were buying breakfast, there was a wide array of contents in the breakfast shop, but no matter who came to the store to buy it, the salesperson asked you what you needed, he knows what to get for you. What we need is change.
Introduction:
The simple factory mode returns an instance of several possible classes of macros through input data. However, these types have a common feature: These types have a common parent class and a common method, but each method is different and optimized based on different data.
In-depth:
The above is a talk about the essence: In the face of these changes, the object-oriented idea is to encapsulate these changes. After the encapsulation changes, it can increase its scalability and no longer modify the previous classes.
Simple factory mode structure:
Mall promotion:
Parent class:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> Abstract class CashSuper // parent class, an abstraction or encapsulation of {public abstract double acceptCash (double money );}
Subclass:
Class CashNormal: CashSuper // each instantiated object implements the polymorphism {public override double acceptCash (double money) // implements the methods in the parent class, and there are differences between different classes {return money;} class CashRebate: CashSuper {private double moneyRebate = 1d; public CashRebate (string moneyRebate) {this. moneyRebate = double. parse (moneyRebate);} public override double acceptCash (double money) {return money * moneyRebate;} class CashReturn: CashSuper {private double moneyCondition = 0.0d; private double moneyReturn = 0.0d; public CashReturn (string moneyCondition, string moneyReturn) {this. moneyCondition = double. parse (moneyCondition); this. moneyReturn = double. parse (moneyReturn);} public override double acceptCash (double money) {double result = money; if (money> = moneyCondition) result = money-Math. floor (money/moneyCondition) * moneyReturn; return result ;}}
Factory model:
// Simple factory mode class CashFactory // simple factory mode is implemented. The selection is placed on the business layer, instead of the interface layer {public static CashSuper createCashAccept (string type) {CashSuper cs = null; // defines the type of a parent class switch (type) {case "normal charge": cs = new CashNormal (); // instantiate the break of each subclass object; case "300 to 100": CashReturn cr1 = new CashReturn ("300", "100"); break; case "off ": cashRebate cr2 = new CashRebate ("0.8"); break;} return cs ;}}
Interface Layer:
Private void button#click (object sender, EventArgs e) {// simple factory mode ------------------------------------------------------------------- CashSuper csuper = CashFactory. createCashAccept (cbxType. selectedItem. toString (); // write method in simple factory mode. The client recognizes two classes, CashSuper and CashFactory. Then, in simple factory mode, each subclass object double totalPrices = 0d has been created; totalPrices = csuper. acceptCash (Convert. toDouble (txtPrice. text) * Convert. toDouble (txtNum. text); total = total + totalPrices; lbxList. items. add ("unit price" + txtPrice. text + "Quantity:" + txtNum. text + "" + cbxType. selectedItem + "Total:" + totalPrices. toString (); label4.Text = total. toString ();}
We all know that stores do not always have promotions, and this is not necessarily the case this year. Next year, things will change with time. If you change the discount quota and Rebate quota each time, every time you have to maintain or expand the billing method, you have to change the factory and re-compile and deploy the code. This is terrible. In the face of market changes in algorithms, you need a policy model.
Rule mode:
For example, you need to change the discount quota and Rebate quota each year for a promotion in a store.
Structure:
Chat:
In fact, the policy Pattern Defines the algorithm family and encapsulates them separately so that they can replace each other. This pattern changes the algorithm and does not affect the customers who use the algorithm. All these algorithms perform the same job, but they implement different ones. They can call all algorithms in the same way, reducing the coupling between algorithm classes and algorithm classes.
In-depth:
A simple factory is used to generate algorithm objects. algorithms can be replaced with each other at any time. This is the change point and encapsulation change point.
Store promotions:
The previous parent class and promotion subclass do not need to be modified:
Rule mode code:
Class CashContext // use a CashContext to configure and maintain a reference to the Strategy object. {CashSuper cs = null; public CashContext (string type) // applies the simple factory mode to instantiate the object of the parent class (different subclasses) {switch (type) // specify a value to select different objects. Specify the policy object {case "normal charges": CashNormal cs0 = new CashNormal (); cs = cs0; break; case "300 to 100": CashReturn cr1 = new CashReturn ("300", "100"); cs = cr1; break; case "off ": cashRebate cr2 = new CashRebate ("0.8"); cs = cr2; break;} public double GetResult (double money) {return cs. acceptCash (money );}}
Interface code:
Private void button#click (object sender, EventArgs e) {// policy mode and simple factory mode combined ---------------------------------------------------- CashContext csuper = new CashContext (cbxType. selectedItem. toString (); // The client only knows CashContext, such as the specific policy object double totalprices = 0d; totalprices = csuper. getResult (Convert. toDouble (txtPrice. text) * Convert. toDouble (txtNum. text); total = total + totalprices; lbxList. items. add ("unit price:" + txtPrice. text + "Quantity:" + txtNum. text + "" + cbxType. selectedItem + "Total:" + totalprices. toString (); label4.Text = total. toString ();}
The above is the combination of the policy model and the simple factory model. The simple factory model is simply a process of instance creation. Policy mode: policies may be replaced at any time.