Design patterns decorator模式中的decorator可以用于添加其他方法吗?
我有一门课叫雪碧:Design patterns decorator模式中的decorator可以用于添加其他方法吗?,design-patterns,Design Patterns,我有一门课叫雪碧: public abstract class Sprite { protected Texture2D Image { get; set; } protected Vector2 position; public Sprite(Texture2D image, Vector2 position) { Image = image; this.position = position; } publi
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);
}
}
这就是入侵者:
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)
{
}
}
我认为这将是实现decorator模式的一个好方法。问题是,在大多数decorator模式的示例中,我看到decorator继承了根抽象类中的一个方法,而没有其他东西,我确实有
我可以在这里使用decorator模式吗?我可以向抽象的AnimatedSprite类添加额外的内容吗?Decorators装饰实现组件接口的对象。从这个意义上讲,您的子类不会修饰“根”对象;它们不聚合接口,而是继承它(入侵者
public class GlowingSprite : Sprite {
private Sprite sprite;
public override void Draw( SpriteBatch imageBatch ) {
sprite.Draw(imageBatch);
// decoration happens vvv
imageBatch.Overlay( GlowingOval, sprite.position ); //
}
}
我知道,但我认为它可能是装饰图案的一个很好的候选者。还是真的?谢谢,我想我现在明白了。我所拥有的不是装饰器模式,因为精灵(imageBatch)本身没有任何变化。