2. Rule Mode

Source: Internet
Author: User
Tags cmath

2. Rule Mode
Zookeeper

1. the core content of the policy model is:

A: Two Types of policy Abstraction

B: implementation of various policies

C: Policy controller. This policy controller is used to control calling policies.

2. The role of the Policy mode is to call different policies based on different passed parameters.

3. Policy mode description

Policy mode: This mode encapsulates an algorithm into an independent class with a common interface for a group of algorithms,

So that they can be replaced with each other. The policy mode allows the algorithm to be effective without affecting the client.

Changed. The policy module separates the behavior from the environment. The environment class is responsible for maintaining and querying behavior classes,

Various algorithms are provided in specific policy classes. Because the algorithm and environment are independent, the increase and decrease of the algorithm,

Modifications do not affect the environment and client.

Different policies should be used for appointment with different types of MM. It is better to have a movie,

Some of them have a good effect on snacks, and some of them are the most romantic to go to the beach, but they are all for the purpose of getting the attention of MM,

There are a lot of Strategy in my chasing MM tips.

Policy abstract class, interface, and abstract class pointer can access all subclass objects (pure virtual function)

The implementation classes of various policies and policies must inherit abstract classes.

Policy setting interface class, set different policies

4. Rule mode class diagram

5. Code:

# Include

# Include

# Include

Usingnamespacestd;

// Policy mode: the Policy mode encapsulates an algorithm into an independent class with a common interface for a group of algorithms,

// So that they can be replaced with each other. The policy mode allows the algorithm to be effective without affecting the client.

// Changed. The policy module separates the behavior from the environment. The environment class is responsible for maintaining and querying behavior classes,

// Various algorithms are provided in specific policy classes. Because the algorithm and environment are independent, the increase and decrease of the algorithm,

// Modifications do not affect the environment and client.

//

// Different policies should be used for dating with different types of MM. It is better to have a movie,

// Some snacks are good, and some are romantic at the seaside, but the purpose is to get the attention of MM,

// There are a lot of strategies in my MM tips.

// Policy abstract class, interface, and class pointer can access all subclass objects (pure virtual function)

// All implementation policies and implementation classes of various policies must inherit abstract classes

// Interface class for setting policies and setting different policies

// The money receiving abstract class

ClassCashSuper

{

Public:

// Pure virtual function for money collection

VirtualdoubleacceptMoney (doublemoney) = 0;

};

// Normal money collection policy

ClassCashNormal: publicCashSuper

{

Public:

// Charge normally

DoubleacceptMoney (doublemoney)

{

Returnmoney;

}

};

// Discount strategy

ClassCashRebate: publicCashSuper

{

Private:

Doublediscount;

Public:

// Discount

CashRebate (doubledis)

{

Discount = dis;

}

// Collect money

DoubleacceptMoney (doublemoney)

{

// Discount

Returnmoney * discount;

}

};

ClassCashReturn: publicCashSuper

{

Private:

DoublemoneyCondition;

DoublemoneyReturn;

Public:

// How much is spent and how much is returned

CashReturn (doublemc, doublemr)

{

MoneyCondition = mc;

MoneyReturn = mr;

}

// Collect and refund

DoubleacceptMoney (doublemoney)

{

Doubleresult = money;

If (money> = moneyCondition)

{

Result = money-floor (money/moneyCondition) * moneyReturn;

}

Returnresult;

}

};

Class CashContext

{

Private:

CashSuper * cs;

Public:

CashContext (stringstr) // sets the policy

{

If (str = "normal ")

{

Cs = newCashNormal ();

}

Elseif (str = "discounted ")

{

Cs = newCashRebate (0.9 );

}

Elseif (str = "1000 to 200 ")

{

Cs = newCashReturn (1000,200 );

}

}

DoublegetResult (doublemoney)

{

Returncs-> acceptMoney (money );

}

};

Intmain ()

{

Doublemoney = 1000;

// You can change the values in the following brackets. If the following sentence is used, the running result is 800.

// CashContext * cc = new CashContext ("send 1000" when the bandwidth reaches 200 ");

// If the following statement is used, 1000 is displayed.

// CashContext * cc = new CashContext ("normal ");

// The following statement indicates a 900 discount. The running result is :.

CashContext * cc = newCashContext (" 9 9 9 ");

Cout < GetResult (money );

Cin. get ();

Return 0;

}

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.