When to use builder design pattern

381 views Asked by At

Is builder design pattern is the right one to create itemBrowsePanel object? this object needs 2 others objects (iconBrowsePanel,textBrowsePanel) in order to be ready to use, as follows:

    HTMLPanel itemBrowsePanel=new HTMLPanel("");

    HTMLPanel iconBrowsePanel=new HTMLPanel("");        

    HTMLPanel textBrowsePanel=new HTMLPanel("");

    HTMLPanel titlePanel=new HTMLPanel(titlePanelText);     

    HTMLPanel subTitlePanel=new HTMLPanel(subTitlePanelText+" "+panelName);

    textBrowsePanel.add(titlePanel);
    textBrowsePanel.add(subTitlePanel);

    itemBrowsePanel.add(iconBrowsePanel);
    itemBrowsePanel.add(textBrowsePanel);

if builder design pattern is the right one, is it applicable to pass arguments to any object on creation in order to construct other objects ?

2

There are 2 answers

0
Lightman On

this object needs 2 others objects (iconBrowsePanel, textBrowsePanel) in order to be ready to use

  1. If you want to ensure that iconBrowsePanel and textBrowsePanel are added to itemBrowsePanel before it is used, then it is better to create an ItemBrowsePanel class with a specific constructor which takes these parameters and ensures the invariant

Is builder design pattern is the right one to create itemBrowsePanel object?

  1. If you want to reuse the existing construction algorithm elsewhere, it is better to wrap in into a method which takes parameters like titlePanelText, subTitlePanelText or panelName and returns a fully constructed HTMLPanel, which represents an itemBrowsePanel

  2. If there are too many parameters and / or parameter combinations to create itemBrowsePanel, then yes, it may be appropriate to use a builder

0
Ekk On

Composite Pattern is better in this case because you are building an aggregated object. It also offer more flexibility, for example, it's easier to change the panel to accept 3 instead of 2 child objects.

First, you create a base class for all UI elements in your system.

public abstract class UIElement
{
    public string Name { get; set; }

    protected UIElement(string name)
    {
        this.Name = name;
    }

    public abstract void Add(UIElement element);
}

Then you add HTMLPanel and override Add to do whatever you want. In this case, you want to limit its child objects to two.

public class HTMLPanel
    : UIElement
{
    private List<UIElement> children = new List<UIElement>();

    // constructor here

    public override void Add(UIElement element)
    {
        if (this.children.Count > 2)
        {
            throw new Exception("");
        }

        this.children.Add(element);
    }
}

To construct the object, it's similar or exactly the same as what you did in your question.

In the future, you might want to show labels in panels. You can achieve that easily by adding a new class.

public class Label
    : UIElement
{
    public override void Add(UIElement element)
    {
        throw new NotSupportedException("Labal cannot have a child element.");
    }
}

And you can just add it to the panel.

HTMLPanel subTitlePanel = new HTMLPanel(subTitlePanelText+" "+panelName);

Label yourLabelObject = new Label("myLabel");
textBrowsePanel.add(yourLabelObject);