2009-08-27 31 views
0

这是一个例子: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#设计模式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.在第二个例子中,根本没有具体的组件,似乎只有一个组件和装饰器,就是这样,对吗?它仍然是装饰者模式?这似乎是对我来说。只是想澄清一些事情。

感谢

回答

0
  1. 是的,它调用基类来执行基本操作方法。做这个装饰器会增加一个行为。
  2. 是的,它仍然是一个装饰

您可以编写各种实现设计模式的,重要的是满足的意图,在这种情况下,“附加额外的职责动态对象”和你写2 N方式在C#中执行此操作。

HTH

+0

我的唯一的其他问题是,其他的例子怎么没有调用基类? – ra170 2009-08-27 18:19:16

+0

它调用照片实例的Drawer方法,photo是在构造函数中传递的基本实例,所以它是相同的。 – ema 2009-08-29 15:30:11