Permalink
Browse files

Added the Builder pattern related code

  • Loading branch information...
1 parent 24e4e54 commit f0aa4e2500d0d97998a454c4f63aa8884374859d Attila Domokos committed Jan 1, 2010
Showing with 159 additions and 0 deletions.
  1. +32 −0 Builder/computer.rb
  2. +30 −0 Builder/computer_builder.rb
  3. +24 −0 Builder/desktop_builder.rb
  4. +27 −0 Builder/laptop_builder.rb
  5. +36 −0 Builder/parts.rb
  6. +10 −0 Builder/program.rb
View
@@ -0,0 +1,32 @@
+require 'parts'
+
+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 LaptopComputer < Computer
+end
+
+class DesktopComputer < Computer
+end
+
+=begin
+# 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)
+=end
@@ -0,0 +1,30 @@
+require 'computer'
+require 'parts'
+
+class ComputerBuilder
+ attr_reader :computer
+
+ def initialize
+ @computer = Computer.new
+ end
+
+ def memory_size(size_in_mb)
+ @computer.motherboard.memory_size = size_in_mb
+ end
+
+ def turbo(has_turbo_cpu=true)
+ @computer.motherboard.cpu = TurboCPU.new
+ end
+end
+
+=begin
+builder = ComputerBuilder.new
+builder.turbo
+builder.add_cd(true)
+builder.add_dvd
+builder.add_hard_disk(100000)
+
+computer = builder.computer
+=end
+
+# Try the laptop builder
@@ -0,0 +1,24 @@
+require 'computer_builder'
+
+class DesktopBuilder < ComputerBuilder
+
+ def initialize
+ @computer = DesktopComputer.new
+ end
+
+ def display=(display)
+ @computer.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
@@ -0,0 +1,27 @@
+require 'computer_builder'
+
+class LaptopBuilder < ComputerBuilder
+ def initialize
+ @computer = LaptopComputer.new
+ end
+
+ def reset
+ @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
View
@@ -0,0 +1,36 @@
+class CPU
+ # common CPU stuff
+end
+
+class BasicCPU < CPU
+ # Lots of not very fast CPU-related stuff...
+end
+
+class TurboCPU < CPU
+ # Lost of very fast CPU stuff
+end
+
+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, writeable)
+ @type = type
+ @size = size
+ @writable = writeable
+ end
+end
+
+class LaptopDrive < Drive
+end
View
@@ -0,0 +1,10 @@
+require 'computer_builder'
+require 'laptop_builder'
+
+builder = LaptopBuilder.new
+builder.add_hard_disk(100000)
+builder.turbo
+
+computer1 = builder.computer
+builder.reset
+computer2 = builder.computer

0 comments on commit f0aa4e2

Please sign in to comment.