dotSITE
Форумы Work in Murano Software. Учебник по C#
новости материалы решения форумы группы настройки/о проекте

Decorator

Decorator - паттерн, позволяющий динамически добавлять обязанности объекту, путум включения его в "конверт", обладающий совместимым интерфейсом

        namespace Decorator_DesignPattern
        {
            using System;

            abstract class Component
            {
                public abstract void Draw();        
            }

            class ConcreteComponent : Component
            {
                private string strName;
                public ConcreteComponent(string s)
                {
                    strName = s;            
                }

                public override void Draw()
                {
                    Console.WriteLine("ConcreteComponent - {0}", strName);          
                }       
            }

            abstract class Decorator : Component
            {
                protected Component ActualComponent;

                public void SetComponent(Component c)
                {
                    ActualComponent = c;
                }
                public override void Draw()
                {
                    if (ActualComponent != null)
                        ActualComponent.Draw();     
                }
            }

            class ConcreteDecorator : Decorator 
            {
                private string strDecoratorName;
                public ConcreteDecorator (string str)
                {
                    strDecoratorName = str; 
                }
                public override void Draw()
                {
                    CustomDecoration();
                    base.Draw();
                }
                void CustomDecoration()
                {
                    Console.WriteLine("In ConcreteDecorator: decoration goes here");
                    Console.WriteLine("{0}", strDecoratorName);
                }
            }

            public class Client
            {
                Component Setup() 
                {
                    ConcreteComponent c = new ConcreteComponent("This is the real component");

                    ConcreteDecorator d = new ConcreteDecorator("This is a decorator for the component");

                    d.SetComponent(c);

                    return d;
                }
                
                public static int Main(string[] args)
                {
                    Client client = new Client();
                    Component c = client.Setup();    

                    c.Draw();
                    
                    return 0;
                }
            }
        }
        

Back
Контакт Реклама на сайте Спонсорам Веб мастерам

Лицензионное соглашение - © 2000-2010 dotSITE
Хостинг .NET предоставлен PARKING.RU
Поддержку сайта осуществляет Murano Software Inc., Offshore software development