标签:
需求:定义一个操作系统OS接口,安装Windows10操作系统,在上面安装虚拟机VMWare,虚拟机里装Linux;
然后在Linux中安装虚拟机VMware,再在虚拟机里安装MacOS操作系统。
实现:
1. 定义一个接口OS:
package DecoratorMode; public interface OS { public void start(); }
2. Windows10操作系统:
package DecoratorMode; public class WindowsOS implements OS { public void start() { System.out.println("windows10 启动....."); } }
3. Linux操作系统:
package DecoratorMode; public class LinuxOS implements OS { private OS os;//被装饰者 public LinuxOS(){ } public LinuxOS(OS os){ this.os = os; } public void start() { os.start(); System.out.println("Windows 的 VMWare 启动....."); System.out.println("Linux 启动....."); } }
4. Mac OS操作系统:
package DecoratorMode; public class MacOS implements OS{ private OS os; public MacOS(){ } public MacOS(OS os){ this.os = os; } public void start() { os.start(); System.out.println("Linux 的 VMWare 启动....."); System.out.println("Mac OS 启动....."); } }
5. 最后,在main函数中调用:
package DecoratorMode; public class DecoratorDemo { public static void main(String[] args) { OS osWin = new WindowsOS(); osWin.start(); System.out.println("----------------------------------"); OS osLinux = new LinuxOS(osWin); osLinux.start(); System.out.println("----------------------------------"); OS osMac = new MacOS(osLinux); osMac.start(); } }
效果:
windows10 启动.....
----------------------------------
windows10 启动.....
Windows 的 VMWare 启动.....
Linux 启动.....
----------------------------------
windows10 启动.....
Windows 的 VMWare 启动.....
Linux 启动.....
Linux 的 VMWare 启动.....
Mac OS 启动.....
标签:
原文地址:http://www.cnblogs.com/huoshaofeng/p/5874827.html