Design Pattern : Builder - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Design Pattern : Builder

Description:

Do nothing but construct. Why do we use Builder? In the previous example, ... Bad Encapsulation & Abstraction. So we make a concept, Director ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 13
Provided by: spa104
Learn more at: http://sparcs.org
Category:

less

Transcript and Presenter's Notes

Title: Design Pattern : Builder


1
Design Pattern Builder
  • SPARCS 04 ???

2
Main Concepts
  • One of Creational Patterns.
  • Process to construct Complex object in Abstract
    type.
  • Use the same creation process for different
    representation type.
  • Director controls all steps of Object creation.

3
Participants Collaborations
Constructs an Objectof Concrete type
(ConcreteBuilder Class)
Parameter of Abstract type(Builder Interface)
Controls all the Process to Construct an Instance
Product
Director
4
Why do we use Builder?
  • Common mannerto Create anInstance
  • Constructor!
  • Each Partsdetermined byParameter of
    theConstructor
  • public class Room
  • private int area
  • private int windows
  • public String purpose
  • Room()
  • Room(int newArea, int newWindows, String
    newPurpose)
  • area newArea
  • windows newWindows
  • purpose newPurpose

There are Only 2 different waysto Create an
Instance part-by-part.
5
Why do we use Builder?
  • In the previous example,
  • We can either determine all the arguments or
    determine nothing and just construct.We cant
    determine arguments partially.
  • We cant control whole process to Create an
    instance.
  • Restriction of ways to Create an Object
  • ? Bad Abstraction Flexibility

6
Why do we use Builder?
  • Another way to Create an Instance
  • Member Methods!!
  • Objects are initially constructed by Default
    Constructor.
  • Member Methods modify constructed
    Instancepart-by-part.

7
Why do we use Builder?
  • public class Room
  • private int area
  • private int windows
  • public String purpose
  • Room()
  • public setArea(int newArea)
  • area newArea
  • public setWindows(int newWindows)
  • windows newWindows
  • public setPurpose(String newPurpose)
  • purpose newPurpose
  • public class Client
  • //
  • public constructRoom ()
  • Room aRoom Room()
  • aRoom.setArea(5)
  • aRoom.setPurpose(baby)

Constructors Simplified.- Do nothing but
construct.
Partial determination of Arguments
8
Why do we use Builder?
  • In the previous example,
  • We can control whole process to Create an
    instance.
  • But then the Client is concerned about Concrete
    Process of Creating an Instance.? Bad
    Encapsulation Abstraction
  • So we make a concept, Director
  • which controls the fine process of Instance
    Creation
  • which separates client process, construction
    representation.

9
Collaborations
  • Builder
  • Provides the Abstract Interface for creating
    Parts of a Product object.
  • Information Director received.
  • ConcreteBuilder
  • Provides the Concrete type (class).
  • Actual participant which Constructs theParts of
    a Product object.
  • Information Client sends.

10
Collaborations
  • Client
  • Constructs Product Object by Default Constructor.
  • Determines the Concrete Type of Product.(Creates
    desired Builder object ConcreteBuilder)
  • Do nothing about setting the arguments.
  • Constructs Director Object and configures it
    with desired Builder Object.
  • Give Director empty Product Object by Parameter.
  • The Object is of Concrete Type, but Director
    gets an Object of Abstract Type.
  • Do nothing about the process of Product Creation.
  • Gets the final result Product (Instance)when
    Director finishes its work creating.

11
Collaborations
  • Director
  • Gets empty Object(to be a Product) by Parameter.
  • Whatever the Type of Product Client sent,
    Director receives by Abstract Interface.
  • Director doesnt have to concern about Concrete
    type of the Product Object.
  • Notifies the Builder whenever a Part of the
    Product should be built.
  • Control the whole Process to Create Product
    part-to-part.

12
Discussion
  • Uses Of Builder
  • Parsing Program(RTF converter)
  • GUI
Write a Comment
User Comments (0)
About PowerShow.com