Why do we need builder design pattern instead of a model?
The below is my understanding of the builder design pattern. Let us say we have a Product class as shown below.
public class Product
{
public string Name { get; set; }
public string Description { get; set; }
public int NumberOfProducts { get; set; }
public decimal Price { get; set; }
public bool IsDurable { get; set; }
}
The builder interface:
public interface IBuilder
{
void SetName(string value);
void SetPrice(decimal value);
void SetIsDurable(bool value);
void SetNumberOfProducts(int value);
void SetDescription(string value);
Product GetProduct();
}
A concrete builder that has the setter methods and get the product object.
public class ConcreteBuilder : IBuilder
{
Product product = new Product();
public Product GetProduct()
{
return product;
}
public void SetDescription(string value)
{
product.Description = value;
}
public void SetIsDurable(bool value)
{
product.IsDurable = value;
}
public void SetName(string value)
{
product.Name = value;
}
public void SetNumberOfProducts(int value)
{
product.NumberOfProducts = value;
}
public void SetPrice(decimal value)
{
product.Price = value;
}
}
In a real-world scenario, the properties of the product should be populated from user inputs and not hardcoded like this. In that case, we need to send the product as well to construct the product object.
public Product ConstructProduct(IBuilder builder)
{
builder.SetDescription("P");
builder.SetIsDurable(false);
builder.SetName("My Name");
builder.SetPrice(10);
builder.SetNumberOfProducts(5);
return builder.GetProduct();
}
Use the builder object in the client as shown below:
public class Client
{
public void AddProduct()
{
ConcreteBuilder builder = new ConcreteBuilder();
var builtProduct = new Director().ConstructProduct(builder);
Console.WriteLine(builtProduct.Description);
}
}
Instead of using the builder pattern as shown above, why can't we use the Product model class itself, like below, when there are many properties to be added in the constructor(to avoid telescoping construction anti-pattern)? If there are any optional properties they can be made nullable.
public class Client
{
Product _prod;
public Client(Product prod)
{
_prod = prod;
}
public void AddProduct()
{
// Code to add product
Console.WriteLine(_prod.Description);
}
}
c# oop design-patterns constructor builder
add a comment |
The below is my understanding of the builder design pattern. Let us say we have a Product class as shown below.
public class Product
{
public string Name { get; set; }
public string Description { get; set; }
public int NumberOfProducts { get; set; }
public decimal Price { get; set; }
public bool IsDurable { get; set; }
}
The builder interface:
public interface IBuilder
{
void SetName(string value);
void SetPrice(decimal value);
void SetIsDurable(bool value);
void SetNumberOfProducts(int value);
void SetDescription(string value);
Product GetProduct();
}
A concrete builder that has the setter methods and get the product object.
public class ConcreteBuilder : IBuilder
{
Product product = new Product();
public Product GetProduct()
{
return product;
}
public void SetDescription(string value)
{
product.Description = value;
}
public void SetIsDurable(bool value)
{
product.IsDurable = value;
}
public void SetName(string value)
{
product.Name = value;
}
public void SetNumberOfProducts(int value)
{
product.NumberOfProducts = value;
}
public void SetPrice(decimal value)
{
product.Price = value;
}
}
In a real-world scenario, the properties of the product should be populated from user inputs and not hardcoded like this. In that case, we need to send the product as well to construct the product object.
public Product ConstructProduct(IBuilder builder)
{
builder.SetDescription("P");
builder.SetIsDurable(false);
builder.SetName("My Name");
builder.SetPrice(10);
builder.SetNumberOfProducts(5);
return builder.GetProduct();
}
Use the builder object in the client as shown below:
public class Client
{
public void AddProduct()
{
ConcreteBuilder builder = new ConcreteBuilder();
var builtProduct = new Director().ConstructProduct(builder);
Console.WriteLine(builtProduct.Description);
}
}
Instead of using the builder pattern as shown above, why can't we use the Product model class itself, like below, when there are many properties to be added in the constructor(to avoid telescoping construction anti-pattern)? If there are any optional properties they can be made nullable.
public class Client
{
Product _prod;
public Client(Product prod)
{
_prod = prod;
}
public void AddProduct()
{
// Code to add product
Console.WriteLine(_prod.Description);
}
}
c# oop design-patterns constructor builder
1
The builder pattern can give lots of positives, but also remember that Design Patterns should only be used when they make sense. In your case using the model might make more sense (I dont know for sure),but it isnt just a case of Design Pattern - GOOD, No Design Pattern - BAD
– Dave
Nov 23 '18 at 16:50
True. Could you then please give a real world example where it might be used and a model would not suffice.
– Priyanka
Nov 23 '18 at 16:55
Here is a good blog article, that coincidentally read this morning blog.ploeh.dk/2017/08/15/test-data-builders-in-c
– Dave
Nov 23 '18 at 16:57
add a comment |
The below is my understanding of the builder design pattern. Let us say we have a Product class as shown below.
public class Product
{
public string Name { get; set; }
public string Description { get; set; }
public int NumberOfProducts { get; set; }
public decimal Price { get; set; }
public bool IsDurable { get; set; }
}
The builder interface:
public interface IBuilder
{
void SetName(string value);
void SetPrice(decimal value);
void SetIsDurable(bool value);
void SetNumberOfProducts(int value);
void SetDescription(string value);
Product GetProduct();
}
A concrete builder that has the setter methods and get the product object.
public class ConcreteBuilder : IBuilder
{
Product product = new Product();
public Product GetProduct()
{
return product;
}
public void SetDescription(string value)
{
product.Description = value;
}
public void SetIsDurable(bool value)
{
product.IsDurable = value;
}
public void SetName(string value)
{
product.Name = value;
}
public void SetNumberOfProducts(int value)
{
product.NumberOfProducts = value;
}
public void SetPrice(decimal value)
{
product.Price = value;
}
}
In a real-world scenario, the properties of the product should be populated from user inputs and not hardcoded like this. In that case, we need to send the product as well to construct the product object.
public Product ConstructProduct(IBuilder builder)
{
builder.SetDescription("P");
builder.SetIsDurable(false);
builder.SetName("My Name");
builder.SetPrice(10);
builder.SetNumberOfProducts(5);
return builder.GetProduct();
}
Use the builder object in the client as shown below:
public class Client
{
public void AddProduct()
{
ConcreteBuilder builder = new ConcreteBuilder();
var builtProduct = new Director().ConstructProduct(builder);
Console.WriteLine(builtProduct.Description);
}
}
Instead of using the builder pattern as shown above, why can't we use the Product model class itself, like below, when there are many properties to be added in the constructor(to avoid telescoping construction anti-pattern)? If there are any optional properties they can be made nullable.
public class Client
{
Product _prod;
public Client(Product prod)
{
_prod = prod;
}
public void AddProduct()
{
// Code to add product
Console.WriteLine(_prod.Description);
}
}
c# oop design-patterns constructor builder
The below is my understanding of the builder design pattern. Let us say we have a Product class as shown below.
public class Product
{
public string Name { get; set; }
public string Description { get; set; }
public int NumberOfProducts { get; set; }
public decimal Price { get; set; }
public bool IsDurable { get; set; }
}
The builder interface:
public interface IBuilder
{
void SetName(string value);
void SetPrice(decimal value);
void SetIsDurable(bool value);
void SetNumberOfProducts(int value);
void SetDescription(string value);
Product GetProduct();
}
A concrete builder that has the setter methods and get the product object.
public class ConcreteBuilder : IBuilder
{
Product product = new Product();
public Product GetProduct()
{
return product;
}
public void SetDescription(string value)
{
product.Description = value;
}
public void SetIsDurable(bool value)
{
product.IsDurable = value;
}
public void SetName(string value)
{
product.Name = value;
}
public void SetNumberOfProducts(int value)
{
product.NumberOfProducts = value;
}
public void SetPrice(decimal value)
{
product.Price = value;
}
}
In a real-world scenario, the properties of the product should be populated from user inputs and not hardcoded like this. In that case, we need to send the product as well to construct the product object.
public Product ConstructProduct(IBuilder builder)
{
builder.SetDescription("P");
builder.SetIsDurable(false);
builder.SetName("My Name");
builder.SetPrice(10);
builder.SetNumberOfProducts(5);
return builder.GetProduct();
}
Use the builder object in the client as shown below:
public class Client
{
public void AddProduct()
{
ConcreteBuilder builder = new ConcreteBuilder();
var builtProduct = new Director().ConstructProduct(builder);
Console.WriteLine(builtProduct.Description);
}
}
Instead of using the builder pattern as shown above, why can't we use the Product model class itself, like below, when there are many properties to be added in the constructor(to avoid telescoping construction anti-pattern)? If there are any optional properties they can be made nullable.
public class Client
{
Product _prod;
public Client(Product prod)
{
_prod = prod;
}
public void AddProduct()
{
// Code to add product
Console.WriteLine(_prod.Description);
}
}
c# oop design-patterns constructor builder
c# oop design-patterns constructor builder
asked Nov 23 '18 at 16:46
PriyankaPriyanka
711211
711211
1
The builder pattern can give lots of positives, but also remember that Design Patterns should only be used when they make sense. In your case using the model might make more sense (I dont know for sure),but it isnt just a case of Design Pattern - GOOD, No Design Pattern - BAD
– Dave
Nov 23 '18 at 16:50
True. Could you then please give a real world example where it might be used and a model would not suffice.
– Priyanka
Nov 23 '18 at 16:55
Here is a good blog article, that coincidentally read this morning blog.ploeh.dk/2017/08/15/test-data-builders-in-c
– Dave
Nov 23 '18 at 16:57
add a comment |
1
The builder pattern can give lots of positives, but also remember that Design Patterns should only be used when they make sense. In your case using the model might make more sense (I dont know for sure),but it isnt just a case of Design Pattern - GOOD, No Design Pattern - BAD
– Dave
Nov 23 '18 at 16:50
True. Could you then please give a real world example where it might be used and a model would not suffice.
– Priyanka
Nov 23 '18 at 16:55
Here is a good blog article, that coincidentally read this morning blog.ploeh.dk/2017/08/15/test-data-builders-in-c
– Dave
Nov 23 '18 at 16:57
1
1
The builder pattern can give lots of positives, but also remember that Design Patterns should only be used when they make sense. In your case using the model might make more sense (I dont know for sure),but it isnt just a case of Design Pattern - GOOD, No Design Pattern - BAD
– Dave
Nov 23 '18 at 16:50
The builder pattern can give lots of positives, but also remember that Design Patterns should only be used when they make sense. In your case using the model might make more sense (I dont know for sure),but it isnt just a case of Design Pattern - GOOD, No Design Pattern - BAD
– Dave
Nov 23 '18 at 16:50
True. Could you then please give a real world example where it might be used and a model would not suffice.
– Priyanka
Nov 23 '18 at 16:55
True. Could you then please give a real world example where it might be used and a model would not suffice.
– Priyanka
Nov 23 '18 at 16:55
Here is a good blog article, that coincidentally read this morning blog.ploeh.dk/2017/08/15/test-data-builders-in-c
– Dave
Nov 23 '18 at 16:57
Here is a good blog article, that coincidentally read this morning blog.ploeh.dk/2017/08/15/test-data-builders-in-c
– Dave
Nov 23 '18 at 16:57
add a comment |
1 Answer
1
active
oldest
votes
When you create a class the primary goal is to reduce complexity of your program. You create a class to hide complexity so that you won’t need to think about it. Sure, you’ll need to think about it when you write the class. But after it’s written, you should be able to forget the details and use the class without any knowledge of its internal workings.
Obviously in the case you mentioned in your question, using the builder pattern increased the complexity and just using the model is the right choice. Because the process of creating your Product
object is quite simple.
In other cases creating an object could be a complex operation and you use the builder design pattern to manage this complexity.
Here's an example of real world usage of builder pattern: https://dzone.com/articles/builder-pattern-usage-real
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450355%2fwhy-do-we-need-builder-design-pattern-instead-of-a-model%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you create a class the primary goal is to reduce complexity of your program. You create a class to hide complexity so that you won’t need to think about it. Sure, you’ll need to think about it when you write the class. But after it’s written, you should be able to forget the details and use the class without any knowledge of its internal workings.
Obviously in the case you mentioned in your question, using the builder pattern increased the complexity and just using the model is the right choice. Because the process of creating your Product
object is quite simple.
In other cases creating an object could be a complex operation and you use the builder design pattern to manage this complexity.
Here's an example of real world usage of builder pattern: https://dzone.com/articles/builder-pattern-usage-real
add a comment |
When you create a class the primary goal is to reduce complexity of your program. You create a class to hide complexity so that you won’t need to think about it. Sure, you’ll need to think about it when you write the class. But after it’s written, you should be able to forget the details and use the class without any knowledge of its internal workings.
Obviously in the case you mentioned in your question, using the builder pattern increased the complexity and just using the model is the right choice. Because the process of creating your Product
object is quite simple.
In other cases creating an object could be a complex operation and you use the builder design pattern to manage this complexity.
Here's an example of real world usage of builder pattern: https://dzone.com/articles/builder-pattern-usage-real
add a comment |
When you create a class the primary goal is to reduce complexity of your program. You create a class to hide complexity so that you won’t need to think about it. Sure, you’ll need to think about it when you write the class. But after it’s written, you should be able to forget the details and use the class without any knowledge of its internal workings.
Obviously in the case you mentioned in your question, using the builder pattern increased the complexity and just using the model is the right choice. Because the process of creating your Product
object is quite simple.
In other cases creating an object could be a complex operation and you use the builder design pattern to manage this complexity.
Here's an example of real world usage of builder pattern: https://dzone.com/articles/builder-pattern-usage-real
When you create a class the primary goal is to reduce complexity of your program. You create a class to hide complexity so that you won’t need to think about it. Sure, you’ll need to think about it when you write the class. But after it’s written, you should be able to forget the details and use the class without any knowledge of its internal workings.
Obviously in the case you mentioned in your question, using the builder pattern increased the complexity and just using the model is the right choice. Because the process of creating your Product
object is quite simple.
In other cases creating an object could be a complex operation and you use the builder design pattern to manage this complexity.
Here's an example of real world usage of builder pattern: https://dzone.com/articles/builder-pattern-usage-real
answered Nov 25 '18 at 21:57
Mis94Mis94
1,11211022
1,11211022
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450355%2fwhy-do-we-need-builder-design-pattern-instead-of-a-model%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The builder pattern can give lots of positives, but also remember that Design Patterns should only be used when they make sense. In your case using the model might make more sense (I dont know for sure),but it isnt just a case of Design Pattern - GOOD, No Design Pattern - BAD
– Dave
Nov 23 '18 at 16:50
True. Could you then please give a real world example where it might be used and a model would not suffice.
– Priyanka
Nov 23 '18 at 16:55
Here is a good blog article, that coincidentally read this morning blog.ploeh.dk/2017/08/15/test-data-builders-in-c
– Dave
Nov 23 '18 at 16:57