Go language Implementation design mode (a): Policy mode

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The policy pattern defines the algorithm family, which is not affected by the change of the algorithm when the algorithm family is called.

The following is rewritten with an example in "Big Talk design mode".

Example: Supermarkets often carry out promotional activities, promotion of promotional activities is a strategy, such as "full 100 minus 20", "hit 80 percent" and so on. Now implement the policy mode, use Cashcontext production strategy, and complete the policy call.


1. First define the interfaces for all policies.

Package Cash Type Cashsuper interface {    Acceptmoney (Money float64) Float64}


2. define three sub-classes to implement this interface

Package Cash//general case, no discount type cashnormal struct {} func newcashnormal () cashnormal {    instance: = new (Cashnormal)    R Eturn *instance} func (c cashnormal) Acceptmoney (Money float64) float64 {    return money}


Package cash//discounts, incoming discounted discounts, such as 0.8type cashrebate struct {    rebate float64/Discount} func newcashrebate (rebate float64) Cashre Bate {    instance: = new (Cashrebate)    instance. rebate = rebate    return *instance} func (c cashrebate) Acceptmoney (Money float64) float64 {    return money * c.rebate }

Package cash//direct rebate, such as full 100 return 20type cashreturn struct {    moneycondition float64    moneyreturn    float64} func Newcashreturn (moneycondition float64, Moneyreturn float64) Cashreturn {    instance: = new (Cashreturn)    instance. Moneycondition = Moneycondition    instance. Moneyreturn = Moneyreturn    return *instance} func (c Cashreturn) Acceptmoney (Money float64) float64 {    if > = c.moneycondition {        Moneyminus: = Int (money/c.moneycondition)        return Money-float64 (Moneyminus) *c. Moneyreturn    }    return Money}

3. The most important moment has come, define the cashcontext structure, used to do the policy screening

Package cash type Cashcontext struct {    strategy Cashsuper} func Newcashcontext (Cashtype string) cashcontext {    c: = New (Cashcontext)    //Here is actually a variant of the simple Factory mode, used to produce the strategy    switch Cashtype {case    "80 percent":        C.strategy = Newcashrebate ( 0.8) Case    "Full 100 Rebate":        c.strategy = Newcashreturn (100.0, 20.0)    default:        c.strategy = Newcashnormal ()    }    Return *c}//After the successful production of the strategy, we can invoke the function of the policy directly. Func (c cashcontext) Getmoney (Money float64) float64 {    return C.strategy.acceptmoney (Money)}

4. Call the test

Package main import (    "Cash", "    FMT") func main () {money    : = 100.0    cc: = cash. Newcashcontext ("Hit 80 percent") money    = cc. Getmoney (Money)    FMT. PRINTLN ("100 dozen 80 percent actual amount for", money) is     199    cc = cash. Newcashcontext ("Full 100 rebate") Money    = cc. Getmoney (Money)    FMT. Println ("199 full 100 Rebate 20 actual amount is", money) "     199    cc = cash." Newcashcontext ("No discount") Money    = cc. Getmoney (Money)    FMT. PRINTLN ("199 No discount actual amount for", money)}/*************************************** output: 100 dozen 80 percent actual amount is 80199 full 100 return 20 actual amount is 179199 no discount the actual amount is 199*/

Summary: The policy model unlocks the customer's perception of the policy, and all policies, even cashsuper, are private. You can generate a policy only if you need to expose cashcontext. Reduced coupling.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Related Article

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.