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.在第二个例子中,根本没有具体的组件,似乎只有一个组件和装饰器,就是这样,对吗?它仍然是装饰者模式?这似乎是对我来说。只是想澄清一些事情。
感谢
我的唯一的其他问题是,其他的例子怎么没有调用基类? – ra170 2009-08-27 18:19:16
它调用照片实例的Drawer方法,photo是在构造函数中传递的基本实例,所以它是相同的。 – ema 2009-08-29 15:30:11