Royal_Bob

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

根据上述要求来用建造者模式设计程序。我将程序放在了https://github.com/RoyalBob/Decorator-Mode-Practice.git   欢迎访问。

 

以下是本程序的UML图以及代码:

1、创建抽象组件类MobilePhone。

1 public abstract class MobilePhone
2 {
3     public String phoneName;
4     public abstract void SendMessage();
5     public abstract void Call();
6 }

2、分别创建具体组件小米和苹果手机类,继承自MobilePhone。

 1 public class MiPhone extends MobilePhone{
 2 
 3     public MiPhone() {
 4         // TODO Auto-generated constructor stub
 5         phoneName="MiPhone";
 6     }
 7     @Override
 8     public void SendMessage() {
 9         // TODO Auto-generated method stub
10         System.out.println( phoneName+"'s SendMessage." );
11     }
12 
13     @Override
14     public void Call() {
15         // TODO Auto-generated method stub
16         System.out.println( phoneName+"'s Call.");
17     }
18 
19 }
MiPhone
 1 public class iPhone extends MobilePhone{
 2 
 3     public iPhone() {
 4         // TODO Auto-generated constructor stub
 5         phoneName="iPhone";
 6     }
 7     
 8     @Override
 9     public void SendMessage() {
10         // TODO Auto-generated method stub
11         System.out.println( phoneName+"'s SendMessage." );
12     }
13 
14     @Override
15     public void Call() {
16         // TODO Auto-generated method stub
17         System.out.println( phoneName+"'s Call.");
18     }
19 
20 }
iPhone

3、创建抽象装饰类Decorator,包含一个MobilePhone类型的私有变量。

 1 public class Decorator extends MobilePhone{
 2 
 3     private MobilePhone _mobilePhone;
 4     
 5     public Decorator(MobilePhone mobilePhone){
 6         _mobilePhone=mobilePhone;
 7         phoneName=mobilePhone.phoneName;
 8     }
 9     @Override
10     public void SendMessage() {
11         // TODO Auto-generated method stub
12         _mobilePhone.SendMessage();
13     }
14 
15     @Override
16     public void Call() {
17         // TODO Auto-generated method stub
18         _mobilePhone.Call();
19     }
20 
21 }

4、分别创建具体装饰类Bluetooth、GPS、Camera。

 1 public class Bluetooth extends Decorator{
 2 
 3     public Bluetooth(MobilePhone mobilePhone) {
 4         super(mobilePhone);
 5         // TODO Auto-generated constructor stub
 6     }
 7     
 8     public void Connect()
 9     {
10         System.out.println( phoneName+"增加蓝牙功能。" );
11     }
12 }
Bluetooth
1 public class GPS extends Decorator{
2 
3     public GPS(MobilePhone mobilePhone) {
4         super(mobilePhone);
5         // TODO Auto-generated constructor stub
6     }
7 
8     public String Location;
9 }
GPS
 1 public class Camera extends Decorator{
 2 
 3     public Camera(MobilePhone mobilePhone) {
 4         super(mobilePhone);
 5         // TODO Auto-generated constructor stub
 6     }
 7 
 8     public void VideoCall(){
 9         System.out.println(phoneName+"增加视频电话功能。");
10     }
11 }
Camera

5、书写主函数Main来分别创建小米手机和苹果手机,并且分别加上蓝牙功能、GPS功能和视频通话功能。

 1 public class Main {
 2 
 3     public static void main(String[] args) {
 4         // TODO Auto-generated method stub
 5         MiPhone miPhone=new MiPhone();
 6         iPhone iphone=new iPhone();
 7         
 8         Bluetooth miBluetooth=new Bluetooth(miPhone);
 9         miBluetooth.Connect();
10         GPS miGPS=new GPS(miPhone);
11         miGPS.Location="MiPhone的定位成功";
12         System.out.println(miGPS.Location);
13         Camera miCamera=new Camera(miPhone);
14         miCamera.VideoCall();
15         
16         Bluetooth iBluetooth=new Bluetooth(iphone);
17         iBluetooth.Connect();
18         GPS iGPS=new GPS(iphone);
19         miGPS.Location="iPhone的定位成功";
20         System.out.println(miGPS.Location);
21         Camera iCamera=new Camera(iphone);
22         iCamera.VideoCall();
23     }
24 
25 }

输出结果看是否符合要求:

posted on 2015-12-29 02:17  Royal_Bob  阅读(455)  评论(0编辑  收藏  举报