【问题标题】:Design Pattern - Decorator. Is correct used in my program?设计模式 - 装饰器。在我的程序中使用正确吗?
【发布时间】:2020-03-31 21:43:13
【问题描述】:

我有一个问题。 在这个例子中我是否正确地使用了设计模式 - 装饰器? 我想在装修后提高飞机的速度。 装饰器总是必须添加新方法吗?只能覆盖现有方法吗?

这是IPlane接口:

interface IPlane
{
    void MoveLeft();
    void MoveRight();
    Rectangle GetPlaneBody();
    int GetHealthPoints();
    int GetSpeed();
    Bullet Shot();
    void SetSpeed(int speed);
}

这是平面类:

public class Plane : IPlane
{
    private Rectangle PlaneBody = new Rectangle();
    private int HealthPoint;
    private int x=200;
    private int speed;
    private Bullet Amunition = new Bullet();
    private static Plane instance;
    private Plane() { }

    public static Plane Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new Plane();
                instance.PlaneBody.Height = 125;
                instance.PlaneBody.Width = 115;
                instance.PlaneBody.X = 200;
                instance.PlaneBody.Y =600;
                instance.HealthPoint = 1000;
                instance.speed = 10;
            }
            return instance;
        }
    }

    public void MoveLeft()
    {
        if(x>-10)
            x -= speed;
        PlaneBody.X = x;
    }
    public void MoveRight()
    {
        if(x<1165)
            x += speed;
        PlaneBody.X = x;
    }

    public Rectangle GetPlaneBody()
    {
        return PlaneBody;
    }
    public int GetHealthPoints()
    {
        return HealthPoint;
    }

    public int GetSpeed()
    {
        return speed;
    }

    public Bullet Shot()
    {
        Bullet bullet = new Bullet();
        bullet.SetDamage(Amunition.GetDamage());
        bullet.SetSpeed(Amunition.GetSpeed());
        bullet.SetCoordinates(x+10, this.PlaneBody.Y - 30);
        return bullet;
    }
    public void SetSpeed( int speed)
    {
        this.speed = speed;
    }

}

这是抽象装饰器:

class AbstractPlaneDecorator : IPlane
{
    protected IPlane plane;

    public AbstractPlaneDecorator(IPlane plane)
    {
        this.plane = plane;
    }

    public int GetHealthPoints()
    {
        return plane.GetHealthPoints();
    }

    public Rectangle GetPlaneBody()
    {
        return plane.GetPlaneBody();
    }

    public int GetSpeed()
    {
        return plane.GetSpeed();
    }

    public virtual void MoveLeft()
    {
        plane.MoveLeft();
    }

    public virtual void MoveRight()
    {
        plane.MoveRight();
    }

    public void SetSpeed(int speed)
    {
        plane.SetSpeed(speed);
    }

    public Bullet Shot()
    {
        return plane.Shot();
    }
}

具体装饰者:

class IncreaseSpeedDecorator : AbstractPlaneDecorator
{
    public IncreaseSpeedDecorator(IPlane plane) : base(plane)
    {

    }
    public override void MoveLeft()
    {
        plane.MoveLeft(); // move two times faster
        plane.MoveLeft();
    }
    public override void MoveRight()
    {
        plane.MoveRight(); // move two times faster
        plane.MoveRight();
    }
}

这个装饰器使飞机的移动速度提高了 2 倍(他执行了两次相同的方法)。是否符合装饰器模式的规则?

【问题讨论】:

    标签: c# design-patterns decorator


    【解决方案1】:

    我会说实现很好。

    我不知道您的代码的确切上下文,但是如果您向装饰器询问飞机的速度,它会返回飞机的基本速度,但飞机的移动速度是原来的两倍,这可能有点奇怪。我认为装饰器更适合描述其功能性的名称是DoubleMoveDecorator 或类似的名称,因为它实际上对飞机的速度没有任何作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多