static void Main(string[] args) { // 我买了个苹果手机 Phone phone = new ApplePhone(); // 现在想贴膜了 Decorator applePhoneWithSticker = new Sticker(phone); // 扩展贴膜行为 applePhoneWithSticker.Print(); Console.WriteLine("----------------------\n"); // 现在我想有挂件了 Decorator applePhoneWithAccessories = new Accessories(phone); // 扩展手机挂件行为 applePhoneWithAccessories.Print(); Console.WriteLine("----------------------\n"); // 现在我同时有贴膜和手机挂件了 Sticker sticker = new Sticker(phone); Accessories applePhoneWithAccessoriesAndSticker = new Accessories(sticker); applePhoneWithAccessoriesAndSticker.Print(); Console.ReadLine(); }
static void Main(string[] args) { Phone phone = new ApplePhone(); phone = new Sticker(phone); phone = new Mug(phone); phone.Print(); Console.ReadKey(); }
static void Main(string[] args) { Phone iPhone = new ApplePhone(); var iPhoneWithSticker = new Sticker(iPhone); iPhoneWithSticker.Print(); var iPhoneWithAccessory = new Accessory(iPhone); iPhoneWithAccessory.Print(); Console.ReadLine(); }
static void Main(string[] args) { Phone phone = new ApplePhone(); Decorate sticker = new Sticker(); Decorate accessories = new Accessories(); //将贴膜装到手机上。。。 sticker.SetDecorate(phone); //将配件装到贴膜手机上。。。 accessories.SetDecorate(sticker); //最后将完整的手机Show一下。。。 accessories.Show(); //输出: //我是iphone手机 //对手机进行了贴膜装饰 //对手机进行了配件装饰 }