Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Builder Design Pattern : Accessing/Passing model data to/in Concrete classes

Here is my builder interface:

public interface IContainerBuilder {
  void SetSections();
  void SetSides();
  void SetArea();
  WebControl GetContainer();
}

And this is my ConcreteBuilder:

public class SingleContainerBuilder:BaseProperties, IContainerBuilder {
  Panel panel = new Panel();
  public void SetSections() {
    //panel.Height = value coming from model
    //panel.Width = value coming from model
  }
  public void SetSides() {
    //panel.someproperty = //value coming from model,
    //panel.someotherproperty = //value coming from model,
  }
  public void SetArea() {
    throw new NotImplementedException();
  }
  public System.Web.UI.WebControls.WebControl GetContainer() {
    return panel;
  }
}

And the Director is here:

public class ContainerBuilder {
  private readonly IContainerBuilder objBuilder;
  public Builder(IContainerBuilder conBuilder) {
    objBuilder = conBuilder;
  }
  public void BuildContainer() {
    objBuilder.SetArea();
    objBuilder.SetSections();
    objBuilder.SetSides();
  }
  public WebControl GetContainer() {
    return this.objBuilder.GetContainer();
  }
}

And that's how I am calling it from default page:

var conBuilder = new ContainerBuilder(new SingleContainerBuilder());
conBuilder.BuildContainer();
var container = conBuilder.GetContainer();

Now the problem / confusion I am having is how do I pass the Model Data to the concrete classes? The reason I am confused/stuck is there could be number of different containers (could be more than 20-30). Does each different type of container has to go and grab data from the model or is there a better approach for it?

The second thing I am confused with is, my Model is in a different Library. Do I need to create a copy of the model in my web project and populate my local model from that master model, or should I directly query the Master Model for Concrete Class Properties? As you can see, my SingleContainer contains BaseProperties, which are local declaration of the properties that are already in the Master Model. I don't see or understand the point of local Model and I am not sure I am right here or not.

Sorry I am new to Design Patterns.

Any help will be really appreciated,

Thanks

like image 525
Angloos Avatar asked Aug 22 '15 18:08

Angloos


People also ask

What are the consequences of applying the builder design pattern?

Here are key consequences of the Builder pattern: It lets you vary a product's internal representation. The Builder object provides the director with an abstract interface for constructing the product. The interface lets the builder hide the representation and internal structure of the product.

When would you apply the builder design pattern?

Builder pattern aims to “Separate the construction of a complex object from its representation so that the same construction process can create different representations.” It is used to construct a complex object step by step and the final step will return the object.

What is the advantages of builder design pattern?

The main advantages of Builder Pattern are as follows: It provides clear separation between the construction and representation of an object. It provides better control over construction process. It supports to change the internal representation of objects.

What is meant by Builder design pattern?

The builder pattern is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming. The intent of the Builder design pattern is to separate the construction of a complex object from its representation. It is one of the Gang of Four design patterns.


1 Answers

I have created a library that is called before the initialization of builder, i am sending the 'Key' of the container i need to the library static method and library gives me back the data i need based on the 'Key' and then i pass that object to the builder and and do stuff there. Earlier i had to pass multiple objects to build what was needed to be build but now instead of passing multiple 'Data Objects' to concrete classes and make concrete classes depend on other class, concrete classes just need one 'Data Object' that contains all information i need.

So my call looks like this now

var data = ContainerData.Getdata(key);//Call to library 

and then the final call to builder is

var conBuilder = new ContainerBuilder(new SingleContainerBuilder(data));

Ofcourse the constructor of concrete class has been changed to accept 'data' type

like image 125
Angloos Avatar answered Nov 05 '22 10:11

Angloos