装饰模式

1.UML类图

2.核心代码

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace fd
 7 {
 8 
 9     public class GPS:Function
10     {
11         public string Localtion { get; set; }
12         
13         public GPS(MobilePhone mobilephone)
14             : base(mobilephone)
15         {
16             
17         }
18     }
19 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace fd
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             MobilePhone mobilephone = new ApplePhone();
13             mobilephone.SendMessage();
14             mobilephone.Call();
15 
16             Bluetooth bluetooth = new Bluetooth(mobilephone);
17             bluetooth.Connect();
18 
19             GPS gps = new GPS(bluetooth);
20             gps.Localtion = "678,898,90,56";
21 
22             Camera camera = new Camera(gps);
23 
24             camera.Call();
25 
26             Console.ReadLine();
27 
28         }
29     }
30 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace fd{
 7     public class ApplePhone:MobilePhone
 8     {
 9         public ApplePhone()
10         { }
11 
12         public override void SendMessage()
13         {
14             Console.WriteLine("发送短信");
15         }
16 
17         public override void Call()
18         {
19             Console.WriteLine("普通通信");
20         }
21     }
22 
23     public class MiPhone : MobilePhone
24     {
25         public MiPhone()
26         { }
27 
28         public override void SendMessage()
29         {
30             Console.WriteLine("发送短信");
31         }
32 
33         public override void Call()
34         {
35             Console.WriteLine("普通通信");
36         }
37 
38 
39     }
40 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace fd
 7 {
 8      public abstract class MobilePhone
 9      {
10          public abstract void SendMessage();  
11          public abstract void Call();         
12      }
13 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace fd
 7 {
 8     public class Function:MobilePhone
 9     {
10         private MobilePhone _mobilephone;
11 
12         public Function(MobilePhone mobilephone)
13         {
14             _mobilephone = mobilephone;
15         }
16 
17         public override void SendMessage()
18         {
19             _mobilephone.SendMessage();
20         }
21 
22         public override void Call()
23         {
24             _mobilephone.Call();
25         }
26 
27     }
28 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace fd
 7 {
 8     public class Camera:Function
 9     {
10         public Camera(MobilePhone mobilephone)
11             : base(mobilephone)
12         {
13        
14         }
15 
16         public override void Call()
17         {
18             Console.WriteLine("通信功能升级为带有视频");
19         }
20     }
21 }
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace fd
 7 {
 8  
 9     public class Bluetooth:Function
10     {
11         public Bluetooth(MobilePhone mobilephone)
12             : base(mobilephone)
13         {
14     
15         }
16 
17 
18         public void Connect()
19         {
20             Console.WriteLine("蓝牙正在连接");
21         }
22     }
23 }

 3.view on github

https://github.com/mengx8/test

posted @ 2015-12-31 14:30  mengx8  阅读(110)  评论(0)    收藏  举报