模式定义
将抽象部分与它的实现部分分离,使它们都可以独立地变化
/**
* 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设置频道");
setChannel(channel);
}
}
/**
* Created by malei on 2016/12/5.
* 标准遥控器抽象
*/
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.
* 标准电视机控制接口规范
*/
public interface ITV {
void on();
void setChannel(int a);
}
网友评论