static void Main(string[] args)
        {
            // 建立手機實體
            Phone phone = new ApplePhone();

            phone.CPU   = "台積電";
            phone.Price = 5000;
            Console.WriteLine(phone.Print());
            Console.WriteLine("CPU : " + phone.CPU);
            Console.WriteLine("成本價 : " + phone.Price);
            Console.WriteLine("售價 : " + phone.SellPrice());
            Console.WriteLine("----------------------\n");

            // 加上貼膜
            PhoneDecorator applePhoneWithSticker = new Sticker(phone);

            Console.WriteLine(applePhoneWithSticker.Print());
            Console.WriteLine("CPU : " + applePhoneWithSticker.CPU);
            Console.WriteLine("成本價 : " + applePhoneWithSticker.Price);
            Console.WriteLine("售價 : " + applePhoneWithSticker.SellPrice());
            Console.WriteLine("----------------------\n");

            // 加上配飾
            PhoneDecorator applePhoneWithAccessories = new Accessories(phone);

            Console.WriteLine(applePhoneWithAccessories.Print());
            Console.WriteLine("CPU : " + applePhoneWithAccessories.CPU);
            Console.WriteLine("成本價 : " + applePhoneWithAccessories.Price);
            Console.WriteLine("售價 : " + applePhoneWithAccessories.SellPrice());
            Console.WriteLine("----------------------\n");

            // 同時加上貼膜和配飾
            Sticker        sticker = new Sticker(phone);
            PhoneDecorator applePhoneWithAccessoriesAndSticker = new Accessories(sticker);

            Console.WriteLine(applePhoneWithAccessoriesAndSticker.Print());
            Console.WriteLine("CPU : " + applePhoneWithAccessoriesAndSticker.CPU);
            Console.WriteLine("成本價 : " + applePhoneWithAccessoriesAndSticker.Price);
            Console.WriteLine("售價 : " + applePhoneWithAccessoriesAndSticker.SellPrice());
            Console.Read();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            //普通手机
            Console.WriteLine("----------------");
            Phone phone = new ApplePhone();

            phone.Print();
            Console.WriteLine("----------------");
            //想贴个膜
            Decorator stickerDecorator = new StickerPhone(phone);

            stickerDecorator.Print();
            Console.WriteLine("----------------");
            //又想挂个件
            Decorator accessoriesDecorator = new AccessoriesPhone(stickerDecorator);

            accessoriesDecorator.Print();
            Console.WriteLine("----------------");

            Console.ReadKey();
        }