vendredi 6 décembre 2019

Design Pattern - Decorator. Is correct used in my program?

I have a quesition. Is in this example did i asue correctly design pattern - decorator? I want to increase speed of plane after decorate. Does decorator always has to add new methods? Can only overwrite existing methods?

This is IPlane interface:

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

This is plane class:

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;
    }

}

This is abstract decorator:

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();
    }
}

And concrete decorator:

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();
    }
}

This decorator makes that plane moves 2 times faster (He performs the same method twice). Does it meet the rules of decorator pattern?

Aucun commentaire:

Enregistrer un commentaire