Java's strategy model (big talk design mode)

Source: Internet
Author: User

Temperature so know new, each reading design mode will have a different experience, adhere to a design pattern every day, until skilled use of design patterns.

The policy pattern defines many algorithms that do the same work, but with different implementations, it can invoke all the algorithms in the same way, reducing the coupling between the algorithm and the calling algorithm.

The following is the strategy class diagram of the big talk design pattern

/***/Publicclass  Context    {private  strategy strategy;      Public Context (Strategy strategy) {        this. Strategy = strategy;    }       Public Double GetResult () {        return  strategy.getresult ();    }}

/***/Publicinterface  strategy    {public  Double GetResult ();}

  

/***/Publicclassimplements     strategy{Private  Double money;      Public Strategya (Double money) {        the. Money = Money ;    }    @Override    public  Double getresult () {        return money ;    }}

/*** (Specific policy category) discount policy*/ Public classStrategybImplementsstrategy{PrivateDouble money; PrivateDouble Discount;  PublicStrategyb (double money, double discount) { This. Money =Money ;  This. Discount =discount; } @Override PublicDouble GetResult () {returnMoney *discount; }}

/*** (Specific policy Class) full reduction strategy*/ Public classStrategycImplementsstrategy{PrivateDouble money; PrivateDouble Submoney;  PublicSTRATEGYC (double money, double Submoney) { This. Money =Money ;  This. Submoney =Submoney; } @Override PublicDouble GetResult () {if(Money >Submoney) {            returnMoney-Submoney; }        returnMoney ; }}

/*** Client*/ Public classTest { Public Static voidMain (string[] args) {Context contexta=NewContext (NewStrategya (60d));        System.out.println (Contexta.getresult ()); Context CONTEXTB=NewContext (NewStrategyb (60d, 0.8d));        System.out.println (Contextb.getresult ()); Context CONTEXTC=NewContext (Newstrategyb (60d, 10d));    System.out.println (Contextc.getresult ()); }}

The above is a demo of the strategy mode, but now the client still needs to know which strategy to use, or whether there is a coupling, if the simple Factory mode and the policy model are combined

Thinking, we do not need to change the policy class, but want to change the client, so that the client does not go to know the specific new object and then we modify the client and the context, let the context help us choose the specific strategy.

After the context code has been transformed

/*** Context*/ Public classContext {Privatestrategy strategy; /**     * @paramtype Policy Types *@paramMoney Price *@paramSubmoney a full sale *@paramDiscount Discount*/     PublicContext (String type, double money, double Submoney, double discount) {Switch(type) { CaseDiscount: Strategy=Newstrategyb (money, discount);  Break;  Case"Full minus": Strategy=NewSTRATEGYC (Money, Submoney);  Break; default: Strategy=NewStrategya (Money);  Break; }    }     PublicDouble GetResult () {returnStrategy.getresult (); }}

Client code

/*** Client*/ Public classTest { Public Static voidMain (string[] args) {Context contexta=NewContext ("Original Price", 60d,NULL,NULL);        System.out.println (Contexta.getresult ()); Context CONTEXTB=NewContext ("Discount", 60d,NULL, 0.8d);        System.out.println (Contextb.getresult ()); Context CONTEXTC=NewContext ("Full minus", 60d, 10d,NULL);    System.out.println (Contextc.getresult ()); }}

This enables a combination of simple factory and policy patterns, where the client simply creates a context without needing to know which object to create. Many business models in the actual work can be abstracted into the strategy processing factory model, and the understanding of the different levels of the strategy pattern will be different.

Gradually will find that the big talk design pattern can not only be applied at the code level, but also can rise to the company's business model and so on!

The above hope can help learn strategy mode of small partners!

Java's strategy model (big talk design 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.