Chapter 6 Creational Patterns: Builder - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Chapter 6 Creational Patterns: Builder

Description:

Client wants to buy a new vehicle (product) He has a choice between mini-van, sports car and motorcycle. ... Product product = new Product(); public override ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 19
Provided by: brahimm
Category:

less

Transcript and Presenter's Notes

Title: Chapter 6 Creational Patterns: Builder


1
Chapter 6 -Creational Patterns Builder
CIS 476/566 Software Architecture and Design
Patterns
  • Dr. Brahim Medjahed
  • brahim_at_umich.edu

2
Design Patterns in GoF The Big Picture
3
Builder Pattern - Intent
  • Separate the construction of a complex object
    from its representation so that the same
    construction process can create different
    representations.
  • The client is shielded from the details of the
    objects construction.

4
Builder - Example
  • Client wants to buy a new vehicle (product)
  • He has a choice between mini-van, sports car and
    motorcycle.
  • Client isnt concerned about the building of his
    vehicle but has features (parts) that he wants.
  • Parts includes doors, tires, seats
  • They are constructed differently for each
    different vehicle.
  • Builder class constructs the vehicle by building
    each part and putting them all together.

5
Builder - Applicability
  • The algorithm for creating a complex object needs
    to be independent of the parts that make up the
    object.
  • The construction process must allow different
    representations for the object being built.

6
Builder UML Class Diagram
7
Builder Participants
  • Builder  
  • Specifies an abstract interface for creating
    parts of a Product object
  • ConcreteBuilder  
  • Constructs and assembles parts of the product by
    implementing the Builder interface
  • Defines and keeps track of the representation it
    creates
  • Provides an interface for retrieving the product
  • Director  
  • Constructs an object using the Builder interface
  • Product 
  • Represents the complex object under construction.
  • ConcreteBuilder builds the product's internal
    representation and defines the process by which
    it's assembled

8
Builder - Collaborations
  • The client creates the Director object and
    configures it with the desired Builder object.
  • Director notifies the builder whenever a part of
    the product should be built.
  • Builder handles requests from the director and
    adds parts to the product.
  • The client retrieves the final product from the
    builder.

9
Builder UML Sequence Diagram
aClient
aDirector
aConcreteBuilder
New ConcreteBuilder
New Director(aConcreteBuilder)
Construct()
BuildPartA()
BuildPartB()
BuildPartC()
GetResult()
10
Builder - Consequences
  • Allows for variations in a products internal
    representation since the Director uses an
    abstract interface.
  • Isolates code for construction and
    representation.
  • Gives finer control over the construction
    process. The director builds each product in a
    step-by-step manner.

11
Builder Example
  • Product has three possible parts Part1, Part2,
    and Part3 (all integer)
  • We have two concrete builders ConcreteBuilder1
    and ConcreteBuilder2
  • ConcreteBuilder1 assign 11, 12, and 13 tp Part1,
    Part2, and Part3 respectively
  • ConcreteBuilder2 assign 21, 22, and 23 tp Part1,
    Part2, and Part3 respectively

12
Example UML Class Diagram
  • To be done in class

13
Example - Implementation
  • // "Director"   class Director       //
    Builder uses a complex series of steps
        public void Construct(Builder
    builder)          builder.BuildPart1()      b
    uilder.BuildPart2()
  • builder.BuildPart3()
  •       

14
Example Implementation (contd)
  • // "Builder" abstract class Builder      publi
    c abstract void BuildPart1()    public abstract
    void BuildPart2()
  • public abstract void BuildPart3()
  •      public abstract Product
    GetResult()  
  • // "Product" class Product      public
    part1,part2, part3 integer
  •   

15
Example Implementation (contd)
  • // "ConcreteBuilder1"   class ConcreteBuilder1
    Builder      private Product product new
    Product()    public override void
    BuildPart1()          product.part1
    11       public override void
    BuildPart2()          product.part2
    12     public override void
    BuildPart3()          product.part3
    13        public override Product
    GetResult()          return product      

16
Example Implementation (contd)
  • // "ConcreteBuilder2"   class ConcreteBuilder2
    Builder      private Product product new
    Product()    public override void
    BuildPart1()          product.part1
    21       public override void
    BuildPart2()          product.part2
    22     public override void
    BuildPart3()          product.part3
    23        public override Product
    GetResult()          return product      

17
Example Implementation (contd)
  • public class Client      public static void
    Main()           // Create director and
    builders       Director mydirector new
    Director()      Builder b1 new
    ConcreteBuilder1()      Builder b2 new
    ConcreteBuilder2()      // Construct two
    products       mydirector.Construct(b1)      Pr
    oduct p1 b1.GetResult()            mydirector
    .Construct(b2)      Product p2
    b2.GetResult()       

18
Example UML Sequence Diagram
  • To be done in class
Write a Comment
User Comments (0)
About PowerShow.com