标签:
MobilePhone(抽象手机类)
abstract class Mobilephone { public string name; public abstract void SendMessage(); public abstract void Call(); }
MiPhone(具体手机,小米手机)
class MiPhone:Mobilephone { public MiPhone() { name = "Mi Mobilephone"; } public override void SendMessage() { Console.Write(name+" can send messages.\n"); } public override void Call() { Console.Write(name+" can call.\n"); } }
ApplePhone(具体手机,苹果手机)
class ApplePhone:Mobilephone { public ApplePhone() { name = "Apple Mobilephone"; } public override void SendMessage() { Console.Write(name+" can send messages.\n"); } public override void Call() { Console.Write(name + " can call.\n"); } }
Decarator(抽象装饰类,继承了抽象手机类)
abstract class Decarator : Mobilephone { protected Mobilephone mp; public Decarator(Mobilephone mp) { this.mp = mp; name = mp.name; } public override void SendMessage() { if(mp!=null) mp.SendMessage(); } public override void Call() { if (mp != null) mp.Call(); } }
Bluetooth(具体装饰类)
class Bluetooth:Decarator { public Bluetooth(Mobilephone mp):base(mp) { } public void Connect() { Console.Write(name+" add Blutooth function.\n"); } }
GPS(具体装饰类)
class GPS:Decarator { public GPS(Mobilephone mobilrphone):base(mobilrphone) { } public string Location; }
Camera(具体装饰类)
class Camera:Decarator { public Camera(Mobilephone mp):base(mp) { } public void Load_Video() { Console.Write(name+" add Camera function.\n"); } }
Client(测试类)
class Client { static void Main(string[] args) { MiPhone mp = new MiPhone(); ApplePhone ap = new ApplePhone(); //小米手机装饰 Bluetooth d1 =new Bluetooth(mp); d1.Connect(); GPS gps1 = new GPS(d1); gps1.Location="10.22"; Console.Write("GPS定位:"+gps1.Location+"\n"); Camera c1 = new Camera(gps1); c1.Load_Video(); //苹果手机装饰 Bluetooth d2 = new Bluetooth(ap); d2.Connect(); GPS gps2 = new GPS(d2); gps2.Location = "10.22"; Console.Write("GPS定位:" + gps2.Location + "\n"); Camera c2 = new Camera(gps2); c2.Load_Video(); Console.Read(); }
测试输出:
标签:
原文地址:http://www.cnblogs.com/qwertydf/p/5097096.html