Thursday, May 5, 2011

[Tutorial] Common Design Patterns in C# 4.0
Part3: Builder Pattern

Pattern Name: 
Builder Pattern

Short Description:
Separate representation and  object construction

Usage: 
Rarely used, only useful if complex objects consisting of multiple parts need to be constructed (composite objects for example)

Complexity:
1 / 5

UML Class Diagram:

image

Explanation:

  • The director (ComputerShop) implements a method that is responsible for the sequence of steps of an object creation process. It takes an abstract builder class as input parameter and delegates the real creation to it.
  • The abstract builder class defines the interface that all inheriting concrete builders will use for object creation.

image

  • The concrete builder implementations contain the parts that are assembled and that build the objects.

image

image

image

  • The final object contains all different parts that get assembled by the concrete builder classes. Those may differ from each other depending on the implementations.
  • A method was added that prints out the characteristics of the different parts to be able to validate the design.

image

image

  • In the last step we add some code to test the software design and the Builder implementation.

image

  • When running the example you can see that everything is working as expected and that the correct classes are instantiated during runtime.

image

Source Code:
http://csharpdesignpatterns.codeplex.com/


Share/Save/Bookmark

No comments: