[java design pattern] appearance pattern


Step 1: Create multiple classes with the same operation method. Disk Memory CPU

public class Disk {
    public void startup() {
     System.out.println("Disk startup!");
	}
	
    public void shutdown() {
        System.out.println("Disk shutdown!");
   	}
   	
}
public class Memory {
    public void startup() {
     System.out.println("Memory startup!");
	}
	
    public void shutdown() {
        System.out.println("Memory shutdown!");
   	}
   	
}
public class CPU {
    public void startup() {
     System.out.println("cpu startup!");
	}
	
    public void shutdown() {
        System.out.println("cpu shutdown!");
   	}
   	
}

Step 2: Create a class for unified management of these operations. Computer

public class Computer {
    private CPU cpu;
    private Memory memory;
    private Disk disk;
    public Computer() {
    	 cpu=new CPU();
    	 memory=new Memory();
    	 disk=new Disk();
    }
    
    
    public void startup() {
       System.out.println("Start the computer");
       cpu.startup();
       memory.startup();
       disk.startup();
       System.out.println("Complete starting the computer");
    }
    
    public void shutdown() {
    	 System.out.println("Turn off the computer");
    	 cpu.shutdown();
    	 memory.shutdown();
    	 disk.shutdown();
    	 System.out.println("Complete shutting down the computer");
    }
    
}


Step 3: Test

public class UserTest {
    public static void main(String[] args) {
		Computer computer=new Computer();
		computer.startup();
		computer.shutdown();
	}
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325799965&siteId=291194637