简体   繁体   中英

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

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

@Does each different type of container has to go and grab data from the model or is there a better approach for it?

Like in the GOF book described, this would a solide solution. So you got for every different type a own concrete type of builder.

A work around could be to provide specifications from the models so you could use a builder for a group of models. Be aware: In this way the models need to know the buidlers and thats not a clean way!

@Does each different type of container has to go and grab data from the model or is there a better approach for it?

In no case the container should know the models directly. The builder should do this. Thats waht the builder patern is about. So the builder should know how to get the data from the model. An collect the data in the case he is called to build a part.

First part of the builder patterns intention: "Seperate the construction of an complexe object from its representation so that the same construction process can create different representation"

@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?

Im not in to your project but i rather think you should not build copies of the master data thats usually arror prone. Therefore the builder should get the possiblity to exces the need data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM