今天,学习了一个新的数据库,mongoDB,并在其shell界面进行增删改查,学习了c#的一些语法,也出现了一些的问题,同时,对于设计模式也没有落下,学习了外观模式:
外观模式
有两种角色:外观类和子类,
外观类:定义子类对象,集成处理子类方法以实现一系列操作
子类:每一个具体的类
主要就是简化操作,提供一个外观类进行子类的操作的统一处理。
子类:
public class Light {
    private String position;
    public Light(String position)
    {
        this.position = position;
    }
    public void on()
    {
        System.out.println(this.position + "灯打开");
    }
    public void off()
    {
        System.out.println(this.position + "灯关闭");
    }
}
public class Fan {
    public void on()
    {
        System.out.println("风扇打开");
    }
    public void off()
    {
        System.out.println("风扇关闭");
    }
}
public class AirConditioner {
    public void on()
    {
        System.out.println("空调打开");
    }
    public void off()
    {
        System.out.println("空调关闭");
    }
}
public class Television {
    public void on()
    {
        System.out.println("电视机打开");
    }
    public void off()
    {
        System.out.println("电视机关闭");
    }
}外观类:
public class GeneralSwitchFacade {
    // 定义子类对象
    private Light light[] = new Light[4];
    private Fan fan;
    private AirConditioner airConditioner;
    private Television television;
    // 对每个子类对象进行初始化
    public GeneralSwitchFacade()
    {
        light[0] = new Light("左前");
        light[1] = new Light("右前");
        light[2] = new Light("左后");
        light[3] = new Light("右后");
        fan = new Fan();
        airConditioner = new AirConditioner();
        television = new Television();
    }
    public void on()
    {
        light[0].on();
        light[1].on();
        light[2].on();
        light[3].on();
        fan.on();
        airConditioner.on();
        television.on();
    }
    public void off()
    {
        light[0].off();
        light[1].off();
        light[2].off();
        light[3].off();
        fan.off();
        airConditioner.off();
        television.off();
    }
}
客户类:
public class Client {
    public static void main(String[] args) {
        // 外观类 初始化
        GeneralSwitchFacade generalSwitchFacade = new GeneralSwitchFacade();
        // 进行方法调用
        generalSwitchFacade.on();
        generalSwitchFacade.off();
    }
}
 
                    
                     
                    
                 
                    
                 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号