public interface BasFunc {
public void startup(String message);
public void shutdown(String message);
}
public class BasFunImp implements BasFunc{
private String startInfo;
private String shutdownInfo;
@Override
public void startup(String message) {
// TODO Auto-generated method stub
startInfo = message;
System.out.println(startInfo);
}@Override
public void shutdown(String message) {
// TODO Auto-generated method stub
shutdownInfo = message;
System.out.println(shutdownInfo);
}
}
public class CPU implements BasFunc{
//根据业务需要进行添加是否更加源或者细粒度的动作实现
private BasFunc core;
public CPU() {
this.core = new BasFunImp();
}
@Override
public void startup(String message) {
// TODO Auto-generated method stub
core.startup(message);
}@Override
public void shutdown(String message) {
// TODO Auto-generated method stub
core.shutdown(message);
}
}
//
public class Disk implements BasFunc{
//根据业务需要进行添加是否更加源或者细粒度的动作实现
private BasFunc core;
public Disk() {
this.core = new BasFunImp();
}
@Override
public void startup(String message) {
// TODO Auto-generated method stub
core.startup(message);
}@Override
public void shutdown(String message) {
// TODO Auto-generated method stub
core.shutdown(message);
}
}
//
public class Memory implements BasFunc{
//根据业务需要进行添加是否更加源或者细粒度的动作实现
private BasFunc core;
public Memory() {
this.core = new BasFunImp();
}
@Override
public void startup(String message) {
// TODO Auto-generated method stub
core.startup(message);
}@Override
public void shutdown(String message) {
// TODO Auto-generated method stub
core.shutdown(message);
}
}
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 Computer(CPU cpu, Memory memory, Disk disk) {
super();
this.cpu = cpu;
this.memory = memory;
this.disk = disk;
}
public void startUp() {
cpu.startup("cpu start up");
memory.startup("memory start up");
disk.startup("disk start up");
}
public void shutdown() {
cpu.startup("cpu shut down");
memory.startup("memory shut down");
disk.startup("disk shut down");
}
}//
public class Demo {
public static void main(String[] args) {
Computer computer = new Computer();
computer.startUp();
computer.shutdown();
}
}