Builder pattern
-
Upload
jyaasa-technologies -
Category
Software
-
view
829 -
download
0
Transcript of Builder pattern
Builder PatternCopyright 2016. Jyaasa Technologies. All Right Reserved
http://jyaasa.com
Sagun ShresthaSr. Software Engineer, Jyaasa Technologies
What is Builder Pattern?
Copyright 2016. Jyaasa Technologies. All Right Reserved http://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 http://jyaasa.com
class Computerattr_accessor :displayattr_accessor :motherboardAttr_reader :drives
def initialize(display=:crt, motherboard=Motherboard.new,drives=[ ])
@motherboard = motherboard@drives = drives@display = display
endend
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 Motherboardattr_accessor :cpuattr_accessor :memory_size
def initialize(cpu=BasicCPU.new, memory_size=1000)@cpu = cpu@memory_size = memory_size
endend
class Driveattr_reader :type # either :hard_disk, :cd or :dvdattr_reader :size # in MBattr_reader :writable # true if this drive is writable
def initialize(type, size, writable)@type = type@size = size@writable = writable
endend
# 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) endend
builder = ComputerBuilder.newbuilder.turbobuilder.add_cd(true)builder.add_dvdbuilder.add_hard_disk(100000)
# new computercomputer = 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 endend
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) endend
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) endend
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 @computerend
Building Sane Object
Thank you
Copyright 2016. Jyaasa Technologies. All Right Reserved http://jyaasa.com