Design patterns 装饰模式需要具体的类吗?(从decorator调用基类?)
以下是一个例子: 我对其进行了一些重新格式化,以便更好地进行压缩:Design patterns 装饰模式需要具体的类吗?(从decorator调用基类?),design-patterns,decorator,Design Patterns,Decorator,以下是一个例子: 我对其进行了一些重新格式化,以便更好地进行压缩: namespace DoFactory.GangOfFour.Decorator.Structural{ class MainApp{ static void Main(){ ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA
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.在第二个示例中,根本没有具体的组件,似乎只有一个组件和装饰器,仅此而已,对吗?还是装饰图案吗?对我来说似乎是这样。只是想澄清一些事情
谢谢
是的,它调用基类来执行基操作方法。这样做,装饰添加了一个行为。
是的,它仍然是一个装饰师
您可以编写设计模式的各种实现,重要的是要满足目的,在这种情况下,动态地将附加的职责附加到对象,您可以在C中编写N种方法中的2种
HTH我唯一的另一个问题是,为什么另一个示例不调用基类?它调用photo实例的抽屉方法,photo是在构造函数中传递的基类实例,所以它是相同的。