装饰器模式中的装饰器可以用来添加额外的方法吗?
我有一个名为 Sprite 的类:
public abstract class Sprite
{
protected Texture2D Image { get; set; }
protected Vector2 position;
public Sprite(Texture2D image, Vector2 position)
{
Image = image;
this.position = position;
}
public void Draw(SpriteBatch imageBatch)
{
spriteBatch.Draw(Image, position, Color.White);
}
}
从它继承的是 AnimatedSprite:
public abstract class AnimatedSprite : Sprite
{
protected Point SheetSize { get; set; }
protected Point CurrentFrame { get; set; }
protected Point FrameSize { get; set; }
protected int MillisecondsPerFrame { get; set; }
protected int TimeSinceLastFrame { get; set; }
public AnimatedSprite(Point sheetSize, Point currentFrame, Point frameSize,
int millisecondsPerFrame, int timeSinceLastFrame, Texture2D image, Vector2 position)
: base(image, position)
{
SheetSize = sheetSize;
CurrentFrame = currentFrame;
FrameSize = frameSize;
MillisecondsPerFrame = millisecondsPerFrame;
TimeSinceLastFrame = timeSinceLastFrame;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Image, position,
new Rectangle(CurrentFrame.X * FrameSize.X,
CurrentFrame.Y * FrameSize.Y,
FrameSize.X, FrameSize.Y), Color.White);
}
}
从 继承 是 Invader:
public class Invader : AnimatedSprite
{
public Invader(Point sheetSize, Point currentFrame, Point frameSize,
int millisecondsPerFrame, int timeSinceLastFrame, Texture2D image, Vector2 position)
: base(sheetSize, currentFrame, frameSize, millisecondsPerFrame, timeSinceLastFrame,
image, position)
{
}
}
我认为这将是实现装饰器模式的一个很好的选择。问题是,在装饰器模式的大多数示例中,我看到装饰器继承了根抽象类的方法,并且没有其他东西,而我确实有。
我可以在这里使用装饰器模式吗?我可以向抽象 AnimatedSprite 类添加其他内容吗?
I have a class, called Sprite:
public abstract class Sprite
{
protected Texture2D Image { get; set; }
protected Vector2 position;
public Sprite(Texture2D image, Vector2 position)
{
Image = image;
this.position = position;
}
public void Draw(SpriteBatch imageBatch)
{
spriteBatch.Draw(Image, position, Color.White);
}
}
Inheriting from it is AnimatedSprite:
public abstract class AnimatedSprite : Sprite
{
protected Point SheetSize { get; set; }
protected Point CurrentFrame { get; set; }
protected Point FrameSize { get; set; }
protected int MillisecondsPerFrame { get; set; }
protected int TimeSinceLastFrame { get; set; }
public AnimatedSprite(Point sheetSize, Point currentFrame, Point frameSize,
int millisecondsPerFrame, int timeSinceLastFrame, Texture2D image, Vector2 position)
: base(image, position)
{
SheetSize = sheetSize;
CurrentFrame = currentFrame;
FrameSize = frameSize;
MillisecondsPerFrame = millisecondsPerFrame;
TimeSinceLastFrame = timeSinceLastFrame;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Image, position,
new Rectangle(CurrentFrame.X * FrameSize.X,
CurrentFrame.Y * FrameSize.Y,
FrameSize.X, FrameSize.Y), Color.White);
}
}
Inhereting from that is Invader:
public class Invader : AnimatedSprite
{
public Invader(Point sheetSize, Point currentFrame, Point frameSize,
int millisecondsPerFrame, int timeSinceLastFrame, Texture2D image, Vector2 position)
: base(sheetSize, currentFrame, frameSize, millisecondsPerFrame, timeSinceLastFrame,
image, position)
{
}
}
I think this would be a good one to implement the decorator pattern. The problem is that in most examples of the decorator pattern I see a method from the root abstract class being inherited by the decorater, and no additional things, which I do have.
Can I use the decorator pattern here, and can I add additional stuff to the abstract AnimatedSprite class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
装饰器装饰实现 Component 接口的对象。从这个意义上说,您的子类不会装饰“根”对象;而是装饰“根”对象。它们不聚合接口,而是继承它(Invader < AnimatedSprite < Sprite )。
所以匹配这个场景的模式不是Decorator。据我所知,这是基本的专业化...
没有添加任何东西,因为组件接口的用户只看到了接口。您只能部分更改装饰组件的实现。
你能使用装饰器吗?我想你可以添加一个“发光”,例如:
Decorators decorate objects that implement the Component interface. In that sense, your subclasses don't decorate 'root' objects; they don't aggregate the interface, but inherit it (Invader < AnimatedSprite < Sprite ).
So the pattern matching this scenario is not Decorator. It's basic Specialization as far as I can tell...
Things are not being added because the user of the Component interface sees no more than the interface. You can only partly change the implementation of the decorated Component.
Could you use decorator? I guess you could, to add a 'glow', for instance: