Core design patterns (13) strategy Strategy Mode

Source: Internet
Author: User
Vs 2008

A behavior of a class may have multiple implementation policies. You can extract this behavior and define it as an interface. Then, you can provide multiple implementations of this interface. These classes (Policies) can be replaced with each other without affecting the client.Code.

1. Mode UML diagram

2. Application

Examples of discounts for books are divided into general discount prices and gold discount prices. For book sellers, the two discounts are two strategies for selling books at a discount.

Idiscountstrategy. CS
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

Namespace Designpattern. Strategy. BLL {< br> Public interface idiscountstrategy {< br> double calculateprice ( double price );
}
}

Commondiscountstrategy. CS
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

Namespace Designpattern. Strategy. BLL {
Public   Class Commondiscountstrategy: idiscountstrategy {

Idiscountstrategy members # Region Idiscountstrategy members

Public   Double Calculateprice ( Double Price) {
ReturnPrice* 0.9;
}

# Endregion
}
}

Goldendiscountstrategy. CS
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

Namespace Designpattern. Strategy. BLL {
Public   Class Goldendiscountstrategy: idiscountstrategy {

Idiscountstrategy members # Region Idiscountstrategy members

Public   Double Calculateprice ( Double Price) {
ReturnPrice* 0.7;
}

# Endregion
}
}

Book. CS
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

Namespace Designpattern. Strategy. BLL {
Public   Class Book {

Private   Double Price;

Public Book ( Double Price) {
This. Price=Price;
}

Public   Double Getpriceafterdiscount (idiscountstrategy strategy) {
ReturnStrategy. calculateprice (This. Price );
}
}
}

Client
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using Designpattern. Strategy. BLL;

Namespace Designpattern. Strategy {
Class Program {
Static   Void Main ( String [] ARGs) {

Book book1 =   New Book ( 100 );
Double Commonprice = Book1.getpriceafterdiscount ( New Commondiscountstrategy ());
Console. writeline ( " Common price is {0} " , Commonprice. tostring ());

Double Goldenprice = Book1.getpriceafterdiscount ( New Goldendiscountstrategy ());
Console. writeline ( " Golden price is {0} " , Goldenprice. tostring ());
}
}
}

Output

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.