vendredi 29 juillet 2016

Does this application apply Decorator Pattern?

I am new to Design Pattern and currently I am reading the Beginning SOLID Principles and Design Pattern and have reached to the Decorator Pattern section. I have already known that:

The decorator pattern allows you to dynamically attach additional functionality to a class.

The book has an example application that allow adding watermark to the original photo.

I have made my decision to implement the same application to archive the same goal but with a difference approach. The application behaves as expected but I am currently struggling how my approach different from the Decorator one (the example on the book) so I decided to ask. Any explanation would be appreciated. Here is the code:

public interface IRender
{
    void Render();
}

public class Photo
{
    IRender iRender;
    public void SetRender(IRender iRenderParam)
    {
        iRender = iRenderParam;
    }

    public void RenderPhoto()
    {
        iRender.Render();
    }
}

public class NormalRender : IRender
{
    public void Render()
    {
        Console.WriteLine("Normal Render");
    }
}

public class WaterMarkRender : IRender
{
    private void RenderWaterMark()
    {
        Console.WriteLine("Render WaterMark");
    }
    public void Render()
    {
        RenderWaterMark();
    }
}

public class Program
{
    static void Main(string[] args)
    {
        Photo photo = new Photo();
        photo.SetRender(new NormalRender());
        photo.RenderPhoto();

        photo.SetRender(new WaterMarkRender());
        photo.RenderPhoto();

        Console.ReadLine();
    }
}

Aucun commentaire:

Enregistrer un commentaire