首页 > 其他 > 详细

[20-05-30][Design Model 4]FACTORY

时间:2020-05-30 14:23:29      阅读:36      评论:0      收藏:0      [点我收藏+]
1 package test_23_1;
2 
3 public interface Console {
4 
5     void calculate();
6 }

 

 1 package test_23_1;
 2 
 3 public class Ps4 implements Console {
 4     
 5     private int usb;
 6     
 7     public Ps4(int usb) {
 8         this.usb = usb;
 9     }
10 
11     @Override
12     public void calculate() {
13         
14         System.out.println("ps4 usb type is " + usb);
15     }
16 
17 }

 

 1 package test_23_1;
 2 
 3 public class Switch implements Console {
 4 
 5     private int usb;
 6 
 7     public Switch(int usb) {
 8         this.usb = usb;
 9     }
10 
11     @Override
12     public void calculate() {
13 
14         System.out.println("switch usb type is " + usb);
15     }
16 
17 }

 

1 package test_23_1;
2 
3 public interface Controller {
4 
5     void installConsole();
6 }

 

 1 package test_23_1;
 2 
 3 public class Ps4Controller implements Controller {
 4 
 5     private int controllerUsb;
 6     
 7     public Ps4Controller(int controllerUsb) {
 8         this.controllerUsb = controllerUsb;
 9     }
10     
11     
12     @Override
13     public void installConsole() {
14         
15         System.out.println("ps4 controller usb type is " + controllerUsb);
16     }
17 
18     
19 
20 }

 

 1 package test_23_1;
 2 
 3 public class SwitchController implements Controller {
 4 
 5     private int controllerUsb;
 6     
 7     public SwitchController(int controllerUsb) {
 8         this.controllerUsb = controllerUsb;
 9     }
10     
11     @Override
12     public void installConsole() {
13         
14         System.out.println("switch controller usb type is " + controllerUsb);
15     }
16 
17 }

 

1 package test_23_1;
2 
3 public interface ConsoleFactory {
4 
5     Console createConsole();
6     
7     Controller createController();
8 }

 

 1 package test_23_1;
 2 
 3 public class Ps4Factory implements ConsoleFactory {
 4 
 5     @Override
 6     public Console createConsole() {
 7         
 8         return new Ps4(2);
 9     }
10 
11     @Override
12     public Controller createController() {
13         
14         return new Ps4Controller(2);
15     }
16 
17 }

 

 1 package test_23_1;
 2 
 3 public class SwitchFactory implements ConsoleFactory {
 4 
 5     @Override
 6     public Console createConsole() {
 7         
 8         return new Switch(3);
 9     }
10 
11     @Override
12     public Controller createController() {
13         // TODO Auto-generated method stub
14         return new SwitchController(3);
15     }
16 
17 }

 

 1 package test_23_1;
 2 
 3 public class Maker {
 4 
 5     private Console console = null;
 6     
 7     private Controller controller = null;
 8     
 9     public void make(ConsoleFactory cf) {
10         
11         prepare(cf);
12     }
13 
14     private void prepare(ConsoleFactory cf) {
15 
16         this.console = cf.createConsole();
17         this.controller = cf.createController();
18         
19         this.console.calculate();
20         this.controller.installConsole();
21     }
22 }

 

 1 package test_23_1;
 2 
 3 public class FactoryTest {
 4 
 5     public static void main(String[] args) {
 6         
 7         Maker mc = new Maker();
 8         Ps4Factory ps4 = new Ps4Factory();
 9         
10         mc.make(ps4);
11     }
12 }

 

结果如下:

ps4 usb type is 2
ps4 controller usb type is 2

[20-05-30][Design Model 4]FACTORY

原文:https://www.cnblogs.com/mirai3usi9/p/12992048.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!