Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Design patterns decorator模式中的decorator可以用于添加其他方法吗?_Design Patterns - Fatal编程技术网

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装饰实现组件接口的对象。从这个意义上讲,您的子类不会修饰“根”对象;它们不聚合接口,而是继承它(入侵者 因此,与此场景匹配的模式不是Decorator。就我所知,这是基本的专业化

没有添加内容,因为组件接口的用户只看到接口。您只能部分更改装饰组件的实现

你能用一下装饰器吗?我想您可以添加“发光”,例如:

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)本身没有任何变化。