天天看點

JAVA設計模式之:指令模式

*通常情況下:行為請求者與實作者通常呈現一種高度耦合狀态。有時要對行為進行變更處理處理。高度耦合方式就顯得不合适。

* 将行為請求者與行為實作者解耦,将一組行為抽象為對象。實作二者之間的松耦合。

這就是指令模式(Command Pattern)。

* 指令模式通常由這幾部分組成:指令接口,詳細指令(實作指令接口),接受指令的對像,指令控制器等組成。

* 模式的優勢:

1.減少對象之間的耦合度。

2.新的指令能夠非常easy地增加到系統中及設計一個組合指令。

4.實作調用同樣方法來實作不同的功能。

模式的不足之處:

每個指令都要設計一個詳細的類,指令較多的話,會導緻類比較多,系統變得龐大,指令模式的使用效率可能減少。

/**
 * 運作指令的接口
 * @description:
 * @date 2016-1-19 上午10:26:04
 */
public interface OrderImpl {
    void execute();//運作方法
}
           
/**
 * 詳細的指令:開空調指令
 * @description:
 * @date 2016-1-19 上午10:28:18
 */
public class OnOrder implements OrderImpl {
    private AirCondition air;
    public OnOrder(AirCondition light) {
        this.air = light;
    }
    @Override
    public void execute() {
        air.open();
    }
}
           
/**
 * 詳細的指令:關空調指令
 * @description:
 * @author ldm
 * @date 2016-1-19 上午10:28:51
 */
public class OffOrder implements OrderImpl {
    private AirCondition light;
    public OffOrder(AirCondition light) {
        this.light = light;
    }
    @Override
    public void execute() {
        light.close();
    }
}
           
/**
 * 詳細指令:調節風速
 * @description:
 * @date 2016-1-19 上午11:04:50
 */
public class ChangeSpeedOrder implements OrderImpl {
    private AirCondition myTv;
    private int channel;

    public ChangeSpeedOrder(AirCondition tv, int channel) {
        myTv = tv;
        this.channel = channel;
    }

    public void execute() {
        myTv.updateWind(channel);
    }
}
           
/**
 * 指令控制器。相當于是遙控器:控制空調的開關。調節風速等
 * @description:
 * @date 2016-1-19 上午10:57:43
 */
public class Control {
    private OrderImpl onOrder; // 開
    private OrderImpl offOrder;// 關
    private OrderImpl updateChannel;// 調節風速

    public Control(OrderImpl onOrder, OrderImpl offOrder, OrderImpl updateChannel) {
        this.onOrder = onOrder;
        this.offOrder = offOrder;
        this.updateChannel = updateChannel;

    }

    public void turnOn() {
        onOrder.execute();
    }

    public void turnOff() {
        offOrder.execute();
    }

    public void changeChannel() {
        updateChannel.execute();
    }
}
           
/**
 * 指令接收者:空調為例
 * @description:
 * @date 2016-1-19 上午10:52:38
 */
public class AirCondition {
    public int currentChannel = ;//目前風速檔

    public void open() {
        System.out.println("打開空調!");
    }

    public void close() {
        System.out.println("關閉空調。");
    }

    public void updateWind(int channel) {
        this.currentChannel = channel;
        System.out.println("更換空調風速檔位:" + channel);
    }
}
           
public class Test {

    public static void main(String[] args) {
        // 指令接收者 (空調)
        AirCondition myAir = new AirCondition();
        // 開空調指令
        OnOrder on = new OnOrder(myAir);
        // 關空調指令
        OnOrder off = new OnOrder(myAir);
        // 換風速指令
        ChangeSpeedOrder speed = new ChangeSpeedOrder(myAir, );
        // 指令控制對象
        Control control = new Control(on, off, speed);
        // 開機
        control.turnOn();
        // 切換頻道
        control.changeChannel();
        // 關機
        control.turnOff();
    }
}
           

測試結果:

打開空調!

更換空調風速檔位:2

打開空調。