Builder Pattern
Copyright 2016. Jyaasa Technologies. All Right Reserved
h
ttp://jyaasa.com
Sagun Shrestha
Sr. Software Engineer,
Jyaasa Technologies
What is Builder Pattern?
Copyright 2016. Jyaasa Technologies. All Right Reserved
h
ttp://jyaasa.com
● The builder pattern is an object creation software
design pattern.
● It is a pattern designed to help you configure complex
objects
● It helps separate the construction of a complex object
from its representation.
● So that the same construction process can create
different representations.
Let’s suppose we are building a system that
will support computer manufacturing
business
Copyright 2016. Jyaasa Technologies. All Right Reserved
h
ttp://jyaasa.com
class Computer
attr_accessor :display
attr_accessor :motherboard
Attr_reader :drives
def initialize(display=:crt, motherboard=Motherboard.new,drives=[ ])
@motherboard = motherboard
@drives = drives
@display = display
end
end
class CPU
# Common CPU stuff...
end
class BasicCPU < CPU
# Lots of not very fast CPU-related stuff...
end
class TurboCPU < CPU
# Lots of very fast CPU stuff...
End
# Computer’s motherboard and drive
class Motherboard
attr_accessor :cpu
attr_accessor :memory_size
def initialize(cpu=BasicCPU.new, memory_size=1000)
@cpu = cpu
@memory_size = memory_size
end
end
class Drive
attr_reader :type # either :hard_disk, :cd or :dvd
attr_reader :size # in MB
attr_reader :writable # true if this drive is writable
def initialize(type, size, writable)
@type = type
@size = size
@writable = writable
end
end
# Build a fast computer with lots of memory...
motherboard = Motherboard.new(TurboCPU.new, 4000)
# ...and a hard drive, a CD writer, and a DVD
drives = [ ]
drives = << Drive.new(:hard_drive, 200000, true)
drives << Drive.new(:cd, 760, true)
drives << Drive.new(:dvd, 4700, false)
computer = Computer.new(:lcd, motherboard, drives)
class ComputerBuilder
attr_reader :computer
def initialize
@computer = Computer.new
end
def turbo(has_turbo_cpu=true)
@computer.motherboard.cpu = TurboCPU.new
end
def display=(display)
@computer.display=display
end
def memory_size=(size_in_mb)
@computer.motherboard.memory_size = size_in_mb
end
def add_cd(writer=false)
@computer.drives << Drive.new(:cd, 760, writer)
end
def add_dvd(writer=false)
@computer.drives << Drive.new(:dvd, 4000, writer)
end
def add_hard_disk(size_in_mb)
@computer.drives << Drive.new(:hard_disk, size_in_mb, true)
end
end
builder = ComputerBuilder.new
builder.turbo
builder.add_cd(true)
builder.add_dvd
builder.add_hard_disk(100000)
# new computer
computer = builder.computer
class DesktopComputer < Computer
# Lots of interesting desktop details omitted...
End
class LaptopComputer < Computer
def initialize( motherboard=Motherboard.new, drives=[] )
super(:lcd, motherboard, drives)
end
# Lots of interesting laptop details omitted...
end
class ComputerBuilder
attr_reader :computer
def turbo(has_turbo_cpu=true)
@computer.motherboard.cpu = TurboCPU.new
end
def memory_size=(size_in_mb)
@computer.motherboard.memory_size = size_in_mb
end
end
class DesktopBuilder < ComputerBuilder
def initialize
@computer = DesktopComputer.new
end
def display=(display)
@display = display
end
def add_cd(writer=false)
@computer.drives << Drive.new(:cd, 760, writer)
end
def add_dvd(writer=false)
@computer.drives << Drive.new(:dvd, 4000,
writer)
end
def add_hard_disk(size_in_mb)
@computer.drives << Drive.new(:hard_disk,
size_in_mb, true)
end
end
class LaptopBuilder < ComputerBuilder
def initialize
@computer = LaptopComputer.new
end
def display=(display)
raise "Laptop display must be lcd" unless display ==
:lcd
end
def add_cd(writer=false)
@computer.drives << LaptopDrive.new(:cd, 760,
writer)
end
def add_dvd(writer=false)
@computer.drives << LaptopDrive.new(:dvd, 4000,
writer)
end
def add_hard_disk(size_in_mb)
@computer.drives << LaptopDrive.new(:hard_disk,
size_in_mb, true)
end
end
Structure
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.
○ Includes classes that define the constituent parts, including interfaces
for assembling the parts into the final result.
Collaboration
● 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 product from the builder.
def computer
raise "Not enough memory" if @computer.motherboard.memory_size < 250
raise "Too many drives" if @computer.drives.size > 4
hard_disk = @computer.drives.find {|drive| drive.type == :hard_disk}
raise "No hard disk." unless hard_disk
@computer
end
Building Sane Object
Thank you
Copyright 2016. Jyaasa Technologies. All Right Reserved
h
ttp://jyaasa.com

Builder pattern

  • 1.
    Builder Pattern Copyright 2016.Jyaasa Technologies. All Right Reserved h ttp://jyaasa.com Sagun Shrestha Sr. Software Engineer, Jyaasa Technologies
  • 2.
    What is BuilderPattern? Copyright 2016. Jyaasa Technologies. All Right Reserved h ttp://jyaasa.com
  • 3.
    ● The builderpattern is an object creation software design pattern. ● It is a pattern designed to help you configure complex objects ● It helps separate the construction of a complex object from its representation. ● So that the same construction process can create different representations.
  • 4.
    Let’s suppose weare building a system that will support computer manufacturing business Copyright 2016. Jyaasa Technologies. All Right Reserved h ttp://jyaasa.com
  • 5.
    class Computer attr_accessor :display attr_accessor:motherboard Attr_reader :drives def initialize(display=:crt, motherboard=Motherboard.new,drives=[ ]) @motherboard = motherboard @drives = drives @display = display end end
  • 6.
    class CPU # CommonCPU stuff... end class BasicCPU < CPU # Lots of not very fast CPU-related stuff... end class TurboCPU < CPU # Lots of very fast CPU stuff... End
  • 7.
    # Computer’s motherboardand drive class Motherboard attr_accessor :cpu attr_accessor :memory_size def initialize(cpu=BasicCPU.new, memory_size=1000) @cpu = cpu @memory_size = memory_size end end class Drive attr_reader :type # either :hard_disk, :cd or :dvd attr_reader :size # in MB attr_reader :writable # true if this drive is writable def initialize(type, size, writable) @type = type @size = size @writable = writable end end
  • 8.
    # Build afast computer with lots of memory... motherboard = Motherboard.new(TurboCPU.new, 4000) # ...and a hard drive, a CD writer, and a DVD drives = [ ] drives = << Drive.new(:hard_drive, 200000, true) drives << Drive.new(:cd, 760, true) drives << Drive.new(:dvd, 4700, false) computer = Computer.new(:lcd, motherboard, drives)
  • 9.
    class ComputerBuilder attr_reader :computer definitialize @computer = Computer.new end def turbo(has_turbo_cpu=true) @computer.motherboard.cpu = TurboCPU.new end def display=(display) @computer.display=display end def memory_size=(size_in_mb) @computer.motherboard.memory_size = size_in_mb end def add_cd(writer=false) @computer.drives << Drive.new(:cd, 760, writer) end def add_dvd(writer=false) @computer.drives << Drive.new(:dvd, 4000, writer) end def add_hard_disk(size_in_mb) @computer.drives << Drive.new(:hard_disk, size_in_mb, true) end end
  • 10.
  • 11.
    class DesktopComputer <Computer # Lots of interesting desktop details omitted... End class LaptopComputer < Computer def initialize( motherboard=Motherboard.new, drives=[] ) super(:lcd, motherboard, drives) end # Lots of interesting laptop details omitted... end class ComputerBuilder attr_reader :computer def turbo(has_turbo_cpu=true) @computer.motherboard.cpu = TurboCPU.new end def memory_size=(size_in_mb) @computer.motherboard.memory_size = size_in_mb end end
  • 12.
    class DesktopBuilder <ComputerBuilder def initialize @computer = DesktopComputer.new end def display=(display) @display = display end def add_cd(writer=false) @computer.drives << Drive.new(:cd, 760, writer) end def add_dvd(writer=false) @computer.drives << Drive.new(:dvd, 4000, writer) end def add_hard_disk(size_in_mb) @computer.drives << Drive.new(:hard_disk, size_in_mb, true) end end class LaptopBuilder < ComputerBuilder def initialize @computer = LaptopComputer.new end def display=(display) raise "Laptop display must be lcd" unless display == :lcd end def add_cd(writer=false) @computer.drives << LaptopDrive.new(:cd, 760, writer) end def add_dvd(writer=false) @computer.drives << LaptopDrive.new(:dvd, 4000, writer) end def add_hard_disk(size_in_mb) @computer.drives << LaptopDrive.new(:hard_disk, size_in_mb, true) end end
  • 14.
  • 15.
    Participants ● Builder ○ Specifiesan 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. ○ Includes classes that define the constituent parts, including interfaces for assembling the parts into the final result.
  • 16.
    Collaboration ● The clientcreates 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 product from the builder.
  • 18.
    def computer raise "Notenough memory" if @computer.motherboard.memory_size < 250 raise "Too many drives" if @computer.drives.size > 4 hard_disk = @computer.drives.find {|drive| drive.type == :hard_disk} raise "No hard disk." unless hard_disk @computer end Building Sane Object
  • 19.
    Thank you Copyright 2016.Jyaasa Technologies. All Right Reserved h ttp://jyaasa.com