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

推荐订阅源

Recent Announcements
Recent Announcements
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
腾讯CDC
D
Docker
G
Google Developers Blog
D
DataBreaches.Net
雷峰网
雷峰网
Blog — PlanetScale
Blog — PlanetScale
S
SegmentFault 最新的问题
The Cloudflare Blog
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Stack Overflow Blog
Stack Overflow Blog
大猫的无限游戏
大猫的无限游戏
量子位
美团技术团队
aimingoo的专栏
aimingoo的专栏
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Engineering at Meta
Engineering at Meta
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 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