装饰模式是否需要具体类? (并从装饰器调用基类?)

时间:2009-08-27 16:15:49

标签: design-patterns decorator

这是来自dofactory

的示例

我重新格式化了一点,以便更好地适应(压缩):

namespace DoFactory.GangOfFour.Decorator.Structural{
class MainApp{

        static void Main(){   
          ConcreteComponent c = new ConcreteComponent();   
          ConcreteDecoratorA d1 = new ConcreteDecoratorA();    
          ConcreteDecoratorB d2 = new ConcreteDecoratorB();

          d1.SetComponent(c);    
          d2.SetComponent(d1);   
          d2.Operation();  
        }    
      }

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

      class ConcreteComponent : Component{    
        public override void Operation(){    
          Console.WriteLine("ConcreteComponent.Operation()");    
        }    
      }    

      abstract class Decorator : Component{    
        protected Component component;    

        public void SetComponent(Component component){    
          this.component = component;   
        }    

        public override void Operation(){    
          if (component != null){    
            component.Operation();    
          }    
        }    
      }     

      class ConcreteDecoratorA : Decorator{    
        public override void Operation(){    
          ****base.Operation();****    
          Console.WriteLine("ConcreteDecoratorA.Operation()");    
        }    
      }   

      class ConcreteDecoratorB : Decorator{    
        public override void Operation(){    
          **base.Operation();**    
          AddedBehavior();    
          Console.WriteLine("ConcreteDecoratorB.Operation()");   
        }    
        void AddedBehavior(){}    
      }    
    }

现在比较一下(来自C#Design patterns 3.0 - O'reilly):

namespace Given {   

      public class Photo : Form{
      Image image;
      public Photo () {
        image = new Bitmap("jug.jpg");
        this.Text = "Lemonade";
        this.Paint += new PaintEventHandler(Drawer);
      }

      public virtual void Drawer(Object source, PaintEventArgs e) {
        e.Graphics.DrawImage(image,30,20);
      }

      private void InitializeComponent(){
          this.SuspendLayout();
         this.ClientSize = new System.Drawing.Size(283, 250);
          this.Name = "Photo";
          this.ResumeLayout(false);
      }
    }
  }

  class DecoratorPatternExample {

    class BorderedPhoto : Photo {
      Photo photo;
      Color color;

      public BorderedPhoto (Photo p, Color c) {
        photo = p;
        color=c;
      }

      public override void Drawer(Object source, PaintEventArgs e) {
        photo.Drawer(source, e);
        e.Graphics.DrawRectangle(new Pen(color, 10),25,15,215,225);
      }
    }

    class TaggedPhoto : Photo {
       Photo photo;
       string tag;
       int number;
       static int count;
       List <string> tags = new List <string> ();

       public TaggedPhoto(Photo p, string t) {
          photo = p;
          tag = t;
          tags.Add(t);
          number = ++count;
       }

       public override void Drawer(Object source, PaintEventArgs e) {
          photo.Drawer(source,e);
          e.Graphics.DrawString(tag, 
          new Font("Arial", 16), 
          new SolidBrush(Color.Black), 
          new PointF(80,100+number*20));
       }

       public string ListTaggedPhotos() {
          string s = "Tags are: ";
          foreach (string t in tags) s +=t+" ";
          return s;
       }
    }

    static void Main () {    
      Photo photo;
      TaggedPhoto foodTaggedPhoto, colorTaggedPhoto, tag;
      BorderedPhoto composition;

      photo = new Photo();
      Application.Run(photo);
      foodTaggedPhoto = new TaggedPhoto (photo,"Food");
      colorTaggedPhoto = new TaggedPhoto (foodTaggedPhoto,"Yellow");
      composition = new BorderedPhoto(colorTaggedPhoto, Color.Blue);
      Application.Run(composition);
      Console.WriteLine(colorTaggedPhoto.ListTaggedPhotos());

      photo = new Photo();
      tag = new TaggedPhoto (photo,"Jug");
      composition = new BorderedPhoto(tag, Color.Yellow);
      Application.Run(composition);
      Console.WriteLine(tag.ListTaggedPhotos());
    }
  }

几个问题,  容易一个: 1.具体装饰器(如第一个例子中)是否必须调用基类? 2.在第二个例子中根本没有具体的组件,似乎只有一个组件和装饰器,这就是全部,我是对的吗?它仍然是装饰模式吗?这对我来说似乎是这样。只是想澄清一些事情。

由于

1 个答案:

答案 0 :(得分:0)

  1. 是的,它调用基类来执行基本的Operation方法。这样做装饰者会添加一种行为。
  2. 是的,它仍然是装饰者
  3. 你可以编写一个设计模式的各种实现,重要的是要满足意图,在这种情况下“动态地附加一个对象的附加职责”,然后你用C语言编写N方式中的2个。

    HTH