I come, I see, I conquer

                    —Gaius Julius Caesar

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

1. 策略(Strategy)

   购物时的付款方式:可采用现金也可用信用卡

   策略模式:定义一系列算法,将各个算法封装起来,使算法可以相互替换。使算法独立于客户而变化。

   方法:利用OO的多态性,动态绑定的特点

   付款策略的实现:

using System;

//抽象的付款策略
abstract class PayMethod
{
    
abstract public void Pay();
}

//具体的付款策略 - 现金支付
class PayByCash : PayMethod
{
    
public override void Pay()
    {
        Console.WriteLine(
"Pay by cash!");
    }
}

//具体的付款策略 - 信用卡支付
class PayByCreditCard : PayMethod
{
    
public override void Pay()
    {
        Console.WriteLine(
"Pay by credit card!");
    }
}

//对策略进行设置和调用的上下文
class Context
{
    
private PayMethod paymethod;

    
//设置付款策略
    public void SetPayMethod(PayMethod paymethod)
    {
        
this.paymethod = paymethod;
    }

    
//调用付款策略
    public void Pay()
    {
        paymethod.Pay();
    }
}

//策略模式的应用
public class Client
{
    
public static void Main(string[] args)
    {
        Context context 
= new Context();
        context.SetPayMethod(
new PayByCreditCard());
        context.Pay();

        
//context.SetPayMethod(new PayByCash()); //此处体现了策略模式中,各策略的可替换性
        
//context.Pay();

        Console.Read();
    }
}

 

 

2. 命令(Command)

   命令模式:将请求封装成对象。。。。。

   角色:抽象命令类,具体命令类,客户端,命令调用者,命令的接收者。

   实例(总开关统一给电灯和风扇发出开或关的命令):

using System;

//命令的接收者:风扇
class Fan
{
    
public void startRotate()
    {
        System.Console.WriteLine(
"Fan is rotating");
    }
    
public void stopRotate()
    {
        System.Console.WriteLine(
"Fan is not rotating");
    }
}

//命令的接收者:电灯
class Light
{
    
public void turnOn()
    {
        System.Console.WriteLine(
"Light is on ");
    }
    
public void turnOff()
    {
        System.Console.WriteLine(
"Light is off");
    }
}

//命令的调用者:总开关
class Switch
{
    
private Command UpCommand, DownCommand;
    
public Switch(Command Up, Command Down)
    {
        UpCommand 
= Up; // concrete Command registers itself with the invoker 
        DownCommand = Down;
    }
    
public void flipUp()
    { 
// invoker calls back concrete Command, which executes the Command on the receiver 
        UpCommand.execute();


    }
    
public void flipDown()
    {
        DownCommand.execute();
    }
}

//具体命令:抽象的命令
public interface Command
{
    
void execute();
}

//具体命令:开灯命令
class LightOnCommand : Command
{
    
private Light myLight;
    
public LightOnCommand(Light L)
    {
        myLight 
= L;
    }
    
public void execute()
    {
        myLight.turnOn();
    }
}

//具体命令:关灯命令
class LightOffCommand : Command
{
    
private Light myLight;
    
public LightOffCommand(Light L)
    {
        myLight 
= L;
    }
    
public void execute()
    {
        myLight.turnOff();
    }
}

//具体命令:开风扇命令
class FanOnCommand : Command
{
    
private Fan myFan;
    
public FanOnCommand(Fan F)
    {
        myFan 
= F;
    }
    
public void execute()
    {
        myFan.startRotate();
    }
}

//具体命令:关风扇命令
class FanOffCommand : Command
{
    
private Fan myFan;

    
public FanOffCommand(Fan F)
    {
        myFan 
= F;
    }
    
public void execute()
    {
        myFan.stopRotate();
    }
}

//客户端
class CommandClient
{

    
static void Main(string[] args)
    {
        Light testLight 
= new Light();//创建命令的接收者 - 电灯

        
//创建用于电灯的各种具体命令
        LightOnCommand testLOC = new LightOnCommand(testLight);
        LightOffCommand testLFC 
= new LightOffCommand(testLight);

        Switch testSwitch 
= new Switch(testLOC, testLFC);//创建命令的调用者 - 总开关
        testSwitch.flipUp();
        testSwitch.flipDown();

        Fan testFan 
= new Fan();//创建命令的接收者 - 风扇

        
//创建用于风扇的各种具体命令
        FanOnCommand foc = new FanOnCommand(testFan);
        FanOffCommand ffc 
= new FanOffCommand(testFan);

        Switch ts 
= new Switch(foc, ffc);//创建命令的调用者 - 总开关
        ts.flipUp();
        ts.flipDown();
    }
}

 

 

posted on 2008-09-21 10:48  jcsu  阅读(578)  评论(0)    收藏  举报