Computer room cooperation--responsibility chain + Strategy mode

Source: Internet
Author: User

</pre><p><span style= "FONT-SIZE:24PX;" > These two models in the personal reconstruction of the time also used, at that time is Mengmengdongdong, and now used again in cooperation, thinking clearly a lot, feel that these design patterns have inextricably linked, Kungfu still not home also have to slowly rationale, remember a brother Siang said " In the end, you will find that the design pattern is actually one, so work hard! First look at the application of these two patterns. </span></p><p><span style= "FONT-SIZE:24PX;" > Responsibility Chain: Determine consumption time </span></p><p><span style= "font-size:24px" > Strategy: Calculate the amount of consumption for different types of cards </span> </p><p><span style= "font-size:24px" > </span></p><p><span style= " FONT-SIZE:24PX "> Responsibility chain Mode </span></p><p></p><p><span style=" font-size:24px "> The benefits of the responsibility chain model are more straightforward to avoid the use of complex if statements, decoupling the requester and the processor, and the flexibility to increase the processor, will not violate the open closure principle. </span></p><p style= "Text-align:center" ></p><p><span style=" font-size:24.44444465637207px "> The use of the responsibility chain model, is done through the Onlinecount, which is then called by the Counttime () method. </span></p><p><span style= "font-size:24px" ></span></p><pre name= "code"Class=" CSharp ">public class Onlinetimecountbll {//<summary>///Calculate consumption time//</summary>///<param Name= "Enline" ></param>///<param name= "enbasicdata" ></param>public int CostTime (LineModel  Enline, Basicdatamodel enbasicdata) {//The decimal type is used in the entity, but is calculated using the int type in the class, so use (int) PREPARETIMEHANDLERBLL            Preparetime = new PREPARETIMEHANDLERBLL ((int) (enbasicdata.preparetime)); The class that instantiates the prep time and initializes it with the preparation time in the base data unittimehandlerbll unittime = new UNITTIMEHANDLERBLL ((int) (Enbasicdata.unittim            e)); Instantiates a class of unit time and initializes it with the unit time in the base data leasttimehandlerbll leasettime = new LEASTTIMEHANDLERBLL ((int) (Enbasicdata.lim            Time)); The class that instantiates the least time on the machine, and the successor who initializes//sets the preparation time with the least time on the computer in the basic data, and if the preparation time cannot be processed, it is processed with a minimum of on-machine time preparetime.se            Tcalculate (Leasettime);            Set the minimum time of the successor, if the minimum time can not be processed, then the unit on the Machine time processing leasettime.setcalculate (unittime); The method that calls the preparation time begins to process the return Preparetime.handletime ((int) enline.consumetime);}} 
Organize your logic with time series diagrams



Policy mode

The strategy pattern is to include different calculation methods for different situations, the user can not care about the specific invocation of the algorithm, can be determined by the countcontext. The calculation method can be increased flexibly.


The use of the policy pattern is done through Countcontext, using the Countcontext () method to determine which subclass to instantiate, and then invoke the method of the instantiated subclass in Countallcash.

    public class Countcontextbll {public COUNTSUPERBLL m_countsuperbll;            Public Countcontextbll (String strcardtype) {BASICDATABLL BASICDATABLL = new BASICDATABLL ();            list<basicdatamodel> myList = new list<basicdatamodel> ();            MyList = Basicdatabll.getdata ();                foreach (Basicdatamodel basicdata in myList) {decimal Decfix = basicdata.rate;//Get fixed user's charge standard                    Decimal dectemp = basicdata.temporaryrate;//Temporary user's Charge standard switch (strcardtype) { Case "Fixed user"://If it is a fixed user, the calculation of fixed user consumption amount M_COUNTSUPERBLL = new Fixusercashcountbll (DECF                    ix);//Initialize break;                        Case "Temporary user"://If it is a temporary user to instantiate the calculation of the fixed user consumption amount M_COUNTSUPERBLL = new TEMPUSERCASHCOUNTBLL (dectemp);//initialization                    Break                Default:break;  }            }      }////<summary>///For clients to invoke the method///</summary>//Machine time as a parameter, make a call to public D            Ecimal countallcash (int onlinetime) {return m_countsuperbll.cashcount (onlinetime); The method that actually called the subclass that was instantiated in Countcontext}}
Summarize

With these two patterns there is a feeling that the pattern is to write the method that we originally wrote in a class to the subclass, and then instantiate the subclass and invoke the method in the case of the use of the child class. So that when the client does not need to know the B-layer method is what to do, to avoid the use of too many if statements to judge, to a certain extent, to achieve the U-layer and B-layer decoupling.

Object-oriented knowledge to open a head, welcome everyone to advise!

Computer room cooperation--responsibility chain + Strategy mode

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.