0% found this document useful (0 votes)
4 views4 pages

Builder Design Pattern Explained

The Builder design pattern is a creational pattern, similar to the Factory pattern (Factory Method, Abstract Factory).

Uploaded by

arsham
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Builder Design Pattern Explained

The Builder design pattern is a creational pattern, similar to the Factory pattern (Factory Method, Abstract Factory).

Uploaded by

arsham
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

▶️ Want to learn more about design patterns? Learn more from NimblePros on YouTube!

Check it out » Hide

Builder Design Pattern


INTRODUCTION O N T H I S PA G E

🔍 SEARCH A simple example in C#

See Also
▲ D E S I G N PAT T E R N S
References

Overview Learn More

Abstract Factory

Adapter

Builder

Chain of Responsibility

CQRS

Decorator

Domain Events Pattern

Facade
Better Software, Faster
Factory Method NimblePros helps development teams deliver better
software, faster. Find out how.
Guard Clause
Sponsored
Mediator

Memento The Builder design pattern is a creational pattern, similar to the Factory pattern (Factory Method,
Abstract Factory). Unlike the Factory pattern, which typically only offers one method for creating
Null Object
an object, the Builder pattern offers multiple methods that can be used to gradually define the
Object Mother characteristics of the type to be created. This provides a more flexible interface than a single
method with a large number of parameters or a complex parameter object.

Observer ▶️ Want to learn more about design patterns? Learn more from NimblePros on YouTube! Check it out » Hide

A typical builder design (to create SomeType) has the following characteristics:

Named SomeTypeBuilder to communicate the use of the pattern


Initializes a simple (or default) private instance of SomeType upon construction
Exposes methods that set properties on the private SomeType instance
Each method returns this, the SomeTypeBuilder instance
Exposes a Build (or Create) method that simply returns the private SomeType instance
In some cases where creating SomeType is expensive, construction may be deferred to this
step
Optionally, expose (static) methods for getting known common configurations of SomeType

A simple example in C#
Copy

public class AddressBuilder


{
private Address _address = new Address();

public AddressBuilder WithStreet(string street)



{
_address.Street = street;
▶️ Want to learn more about design patterns? Learn more from NimblePros on YouTube! Check it out » Hide
return this;
}

public AddressBuilder WithState(string state)


{
_address.State = state;
return this;
}

// additional WithWhatever methods

public Address Build()


{
return _address;
}
}

See Also
Testing - Overview

References
Design Patterns Library (Pluralsight)
Applying the Builder Pattern to an Angular Service (Typescript)
Using Builder in C# Unit Tests Kata
Flexible and Expressive Unit Tests with the Builder Pattern

Learn More
On-Demand Webinar: Exploring Design Patterns for Testing
Edit this page on GitHub
▶️ Want to learn more about design patterns? Learn more from NimblePros on YouTube! Check it out » Hide

PREV NEXT
Adapter Chain of Responsibility

You might also like