模式定義
將抽象部分與它的實(shí)現(xiàn)部分分離鸦做,使它們都可以獨(dú)立地變化
/**
* Created by malei on 2016/12/5.
* 小米電視
*/
public class XiaoMiTV implements ITV{
@Override
public void on() {
System.out.println("打開電視");
}
@Override
public void setChannel(int a) {
System.out.println("選擇頻道 = "+a);
}
public static void main(String[] args){
ITV itv = new XiaoMiTV();
XiaoMiControl control = new XiaoMiControl(itv);
control.turnOn();
control.setChannelKey(1);
}
}
/**
* Created by malei on 2016/12/5.
* 小米遙控器
*/
public class XiaoMiControl extends AbstractControl{
public XiaoMiControl(ITV ITV) {
super(ITV);
}
public void setChannelKey(int channel) {
System.out.println("AbstractControlImpl設(shè)置頻道");
setChannel(channel);
}
}
/**
* Created by malei on 2016/12/5.
* 標(biāo)準(zhǔn)遙控器抽象
*/
public abstract class AbstractControl {
private final ITV mITV;
public AbstractControl(ITV ITV) {
mITV = ITV;
}
public void turnOn() {
mITV.on();
}
public void setChannel(int channel) {
mITV.setChannel(channel);
}
}
/**
* Created by malei on 2016/12/5.
* 標(biāo)準(zhǔn)電視機(jī)控制接口規(guī)范
*/
public interface ITV {
void on();
void setChannel(int a);
}