即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

外观设计模式1

编程语言 linan_pin 47℃ 0评论
/**
 * 外观模式 -- 将多个相互依赖的类的依赖关系放在一个类中
 * 避免类相互持有,实现松耦合
 */
public class Computer {
    private CPU cpu;
    private Memory memory;
    private Disk disk;

    public Computer() {
        this.cpu = new CPU();
        this.memory = new Memory();
        this.disk = new Disk();
    }

    public void startUp(){
        System.out.println("Start computer ...");
        cpu.startUp();
        memory.startUp();
        disk.startUp();
        System.out.println("Computer started.");
    }

    public void shutdpwn(){
        System.out.println("Shutdown computer...");
        disk.shutdown();
        memory.shutdown();
        cpu.shutdown();
        System.out.println("...");
    }
}


public class CPU {
    public void startUp(){
        System.out.println("CPU start up.");
    }

    public void shutdown(){
        System.out.println("CPU shutdown.");
    }
}


public class Memory {
    public void startUp(){
        System.out.println("Memory start up.");
    }
    public void shutdown(){
        System.out.println("Memory shutdown.");
    }
}


public class Disk {
    public void startUp(){
        System.out.println("Disk start up.");
    }
    public void shutdown(){
        System.out.println("Disk shutdown.");
    }
}


public class User {
    public static void main(String[] args) {
        Computer computer = new Computer();
        computer.startUp();
        System.out.println();
        computer.shutdpwn();
    }
}





转载请注明:CodingBlog » 外观设计模式1

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情