Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Builder pattern
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Examples == A [[C Sharp (programming language)|C#]] example: <syntaxhighlight lang="csharp"> /// <summary> /// Represents a product created by the builder. /// </summary> public class Bicycle { public Bicycle(string make, string model, string colour, int height) { Make = make; Model = model; Colour = colour; Height = height; } public string Make { get; set; } public string Model { get; set; } public int Height { get; set; } public string Colour { get; set; } } /// <summary> /// The builder abstraction. /// </summary> public interface IBicycleBuilder { Bicycle GetResult(); string Colour { get; set; } int Height { get; set; } } /// <summary> /// Concrete builder implementation. /// </summary> public class GTBuilder : IBicycleBuilder { public Bicycle GetResult() { return Height == 29 ? new Bicycle("GT", "Avalanche", Colour, Height) : null; } public string Colour { get; set; } public int Height { get; set; } } /// <summary> /// The director. /// </summary> public class MountainBikeBuildDirector { private IBicycleBuilder _builder; public MountainBikeBuildDirector(IBicycleBuilder builder) { _builder = builder; } public void Construct() { _builder.Colour = "Red"; _builder.Height = 29; } public Bicycle GetResult() { return this._builder.GetResult(); } } public class Client { public void DoSomethingWithBicycles() { var director = new MountainBikeBuildDirector(new GTBuilder()); // Director controls the stepwise creation of product and returns the result. director.Construct(); Bicycle myMountainBike = director.GetResult(); } } </syntaxhighlight> A Java example:<syntaxhighlight lang="java" line="1"> public class Employee { // Required parameters private final String name; private final int id; // Optional parameters private final String department; private final double salary; // Private constructor private Employee(Builder builder) { this.name = builder.name; this.id = builder.id; this.department = builder.department; this.salary = builder.salary; } // Static nested Builder class public static class Builder { private final String name; private final int id; private String department = "General"; // default value private double salary = 0.0; // default value public Builder(String name, int id) { this.name = name; this.id = id; } public Builder department(String department) { this.department = department; return this; } public Builder salary(double salary) { this.salary = salary; return this; } public Employee build() { return new Employee(this); } } @Override public String toString() { return "Employee{name='" + name + "', id=" + id + ", department='" + department + "', salary=" + salary + '}'; } } public class Main { public static void main(String[] args) { Employee emp = new Employee.Builder("Alice", 101) .department("Engineering") .salary(90000.0) .build(); System.out.println(emp); } } </syntaxhighlight>The Director assembles a bicycle instance in the example above, delegating the construction to a separate builder object that has been given to the Director by the Client.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Builder pattern
(section)
Add topic