惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
J
Java Code Geeks
IT之家
IT之家
Last Week in AI
Last Week in AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog RSS Feed
P
Proofpoint News Feed
博客园 - Franky
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 【当耐特】
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
B
Blog
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
博客园 - 聂微东
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
U
Unit 42
aimingoo的专栏
aimingoo的专栏

博客园 - Tristan(GuoZhijian)

Core Design Patterns(16) Chain of Responsibility 职责链模式 Core Design Patterns(15) Template Method 模版方法模式 Core Design Patterns(14) State 状态模式 Core Design Patterns(12) Builder 建造者模式 Core Design Patterns(11) Abstract Factory 抽象工厂模式 Core Design Patterns(10) Singleton 单例模式 Core Design Patterns(9) Factory Method 工厂方法模式 Core Design Patterns(8) Prototype 原型模式 Core Design Patterns(7) Facade 外观模式 Core Design Patterns(6) Adapter 适配器模式 Core Design Patterns(5) Flyweight 享元模式 Core Design Patterns(4) Composite 组合模式 Core Design Patterns(3) Bridge 桥接模式 Core Design Patterns(2) Proxy 代理模式 Core Design Patterns(1) Decorator 装饰模式 老调重弹:插件式框架开发的一个简单应用 Behavior模型应用:可拖动的div容器 Microsoft Asp.Net Ajax框架入门(13) PageRequestManager Microsoft Asp.Net Ajax框架入门(12) 了解异步通信层
Core Design Patterns(13) Strategy 策略模式
Tristan(GuoZhijian) · 2008-03-16 · via 博客园 - Tristan(GuoZhijian)

VS 2008

一个类的某个行为可能有多种实现策略,可以将这个行为提取出来定义为接口,那么就可以提供这个接口的多个实现。这些类(策略)可以相互替换而不影响客户端代码。

1. 模式UML图

2. 应用

    考虑图书折扣出售的例子,分为普通折扣价,和黄金折扣价。对于售书商来说,这两种折扣就是图书打折出售的两种策略。

IDiscountStrategy.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Strategy.BLL {
    
public interface IDiscountStrategy {

        
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
    }

}

GoldenDiscountStrategy.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Strategy.BLL {
    
public class GoldenDiscountStrategy : IDiscountStrategy {

        
IDiscountStrategy Members
    }

}

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) {
            
return strategy.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