装饰器模式中的装饰器可以用来添加额外的方法吗?

发布于 2024-10-19 01:39:42 字数 1990 浏览 0 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜访吸血鬼 2024-10-26 01:39:42

装饰器装饰实现 Component 接口的对象。从这个意义上说,您的子类不会装饰“根”对象;而是装饰“根”对象。它们不聚合接口,而是继承它(Invader < AnimatedSprite < Sprite )。

所以匹配这个场景的模式不是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 ); //  
    }
}

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:

public class GlowingSprite : Sprite {
    private Sprite sprite;
    public override void Draw( SpriteBatch imageBatch ) {
       sprite.Draw(imageBatch); 

       // decoration happens vvv
       imageBatch.Overlay( GlowingOval, sprite.position ); //  
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文