public   class   Ring  :  Decorator

    {

        public override void Show()

        {

            Console.WriteLine("铃声功能 ");

            base.Show();

        }

    }

    public class Office : Decorator

    {

        public override void Show()

        {

            Console.WriteLine("Office拓展功能 ");

            base.Show();

        }

    }

    public class Video_Call : Decorator

    {

        public override void Show()

        {

            Console.WriteLine("视频电话功能 ");

            base.Show();

        }

    }

    public class Handwriting : Decorator

    {

        public override void Show()

        {

            Console.WriteLine("手写功能 ");

            base.Show();

        }

    }    public class Surfing : Decorator

    {

        public override void Show()

        {

            Console.WriteLine("上网功能 ");

            base.Show();

        }

    }

    public class QQ : Decorator

    {

        public override void Show()

        {

            Console.WriteLine("QQ功能 ");

            base.Show();

        }

    }

    public class Fetion : Decorator

    {

        public override void Show()

        {

            Console.WriteLine("飞信功能 ");

            base.Show();

        }

    }

    public class Wolf : Decorator

    {

        public override void Show()

        {

            Console.WriteLine("狼牙功能 ");

            base.Show();

        }

 }

  客户端代码如下:

   static void Main(string[] args)

        {

            Mobile mobile = new Mobile("Hoogle");

            Console.WriteLine("\n第一款全能装饰方式:");

            Game game = new Game();

            Touch touch = new Touch();

            Photo photo = new Photo();

            Memory memory = new Memory();

            MP3 mp3 = new MP3();

            Recording recording = new Recording();

            Ring ring = new Ring();

            Office office = new Office();

            Video_Call video_call = new Video_Call();

            Handwriting handwriting = new Handwriting();

            Surfing surfing = new Surfing();

            QQ qq = new QQ();

            Fetion fetion = new Fetion();

            Wolf wolf = new Wolf();

            game.expand(mobile);

            touch.expand(game);

            photo.expand(touch);

            memory.expand(photo);

            mp3.expand(memory);

            recording.expand(mp3);

            ring.expand(recording);

            office.expand(ring);

            video_call.expand(office);

            handwriting.expand(video_call);

            surfing.expand(handwriting);

            qq.expand(surfing);

            fetion.expand(qq);

            wolf.expand(fetion);

            wolf.Show();

            Console.WriteLine("\n另一种装饰方式:");

            Ring rings = new Ring();

            Office offices = new Office();

            Video_Call video_calls = new Video_Call();

            Handwriting handwritings = new Handwriting();

            Surfing surfings = new Surfing();

            QQ qqs = new QQ();

            Fetion fetions = new Fetion();

            Wolf wolfs = new Wolf();

            rings.expand(recording);

            offices.expand(ring);

            video_calls.expand(office);

            handwritings.expand(video_call);

            surfings.expand(handwriting);

            qqs.expand(surfing);

            fetions.expand(qq);

            wolfs.expand(fetion);

            wolfs.Show();

            Console.Read();

   }

显示结果:

第一款全能装饰方式:

游戏功能 触屏功能 照相功能 内存卡扩展功能 MP3功能 录音功能 铃声功能 Office拓展功能 视频电话功能 手写功能 上网功能 QQ功能 飞信功能 狼牙功能

另一种装饰方式:

录音功能 铃声功能 Office拓展功能 视频电话功能 手写功能 上网功能 QQ功能 飞信功能 狼牙功能

以上就是我用装饰模式设计手机的全过程,其中难免存在不足或漏洞,欢迎大家提出自己的意见和建议,谢谢!

曾祥瑚

2010.06.28  15:06