Hi I'm stuck with a problem.
I want to implement the builder pattern to make creating my objects easier. The problem I face has to do with nested object. The object I would like to create has a list of other objects in it, and I don't really have an idea on how to tackle it.
I want to be able to do the following (Simpler objects for example):
Receipt RestaurantReceipt = new ReceiptBuilder()
.withDate("value")
.withName("value")
.AddItem("value")
.WithIngredients("value")
.WithType("value")
.AddItem("value")
.WithIngredients("value")
.WithType("value")
.build();
Or something like:
Receipt RestaurantReceipt = new ReceiptBuilder()
.withDate("value")
.withName("value")
.AddItem("value", item => {
.WithIngredients("value")
.WithType("value")
})
.AddItem("value", item => {
.WithIngredients("value")
.WithType("value")
})
.build();
Example should be representative for my situation, although if got more than one type of nested object.
ReceiptBuilder
look like? What is the structure of the items you;re trying to add withAddItem
? – WonkyReceipt
class – McdougallReceipt
object does not implement the pattern itself? Instead of the builder, would extension methods not be preferable? – Cadaverine