CSC/ECE 517 Fall 2012/ch2a 2w18 as

From Expertiza_Wiki
Revision as of 22:44, 20 October 2012 by Sanarsal (talk | contribs) (ComputerBuilder Ruby example added)
Jump to navigation Jump to search

Abstract Factory and Builder Patterns

"Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice" - Christopher Alexander

"In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design".

A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.

Creational Patterns

Creational Patterns are Design Patterns that somehow control the mechanism by which objects are created. They generally help to provide the following capabilities <ref>Stelting, S. (2002). Creational patterns. In Applied java patterns (p. 5). Palo Alto, California: Sun Microsystems. Retrieved from www.pearsonhighered.com/samplechapter/0130935387.pdf</ref>:

  • Generic instantiation – This allows objects to be created in a system without having to identify a specific class type in code.
  • Simplicity – Some of the patterns make object creation easier, so callers will not have to write large, complex code to instantiate an object.
  • Creation constraints – Some patterns enforce constraints on the type or number of objects that can be created within a system. The Singleton Pattern is an example of this type.

Abstract Factory

The basic purpose of the Abstract Factory method Pattern is to "Provide an interface for creating families of related or dependent objects without specifying their concrete classes".<ref>^ [|Gamma, Erich]; Richard Helm, Ralph Johnson, John M. Vlissides (2009-10-23). "Design Patterns: Abstract Factory" (in English) (HTML). informIT. Archived from the original on 2009-10-23. Retrieved 2012-05-16. "Object Creational: Abstract Factory: Intent: Provide an interface for creating families of related or dependent objects without specifying their concrete classes."</ref>

Abstract Factory Patterns are used when a client needs to create an instance of a class from a group of similarly related classes. The client does not know (or care) which type of class the client gets an instance of. The type of class returned to the client depends on some condition (and is not of the client's choosing) which is usually stored in some sort of a configuration file. The abstract factory looks up this configuration file then returns the corresponding class to the client.

The client has no need to specify the type, since it has already been specified in the configuration file. In particular, this means:<ref>http://en.wikipedia.org/wiki/Abstract_factory_pattern</ref>

  • The client code has no knowledge whatsoever of the concrete type, not needing to include any header files or class declarations related to it. The client code deals only with the abstract type. Objects of a concrete type are indeed created by the factory, but the client code accesses such objects only through their abstract interface.
  • Adding new concrete types is done by modifying the client code to use a different factory, a modification that is typically one line in one file. (The different factory then creates objects of a different concrete type, but still returns a pointer of the same abstract type as before – thus insulating the client code from change.) This is significantly easier than modifying the client code to instantiate a new type, which would require changing every location in the code where a new object is created (as well as making sure that all such code locations also have knowledge of the new concrete type, by including for instance a concrete class header file). If all factory objects are stored globally in a singleton object, and all client code goes through the singleton to access the proper factory for object creation, then changing factories is as easy as changing the singleton object

Builder

The Builder Pattern is a design pattern that abstracts steps of construction of objects so that different implementations of these steps can construct different representations of objects<ref>http://en.wikipedia.org/wiki/Builder_pattern</ref>.

Complex applications require complex objects. Complex objects are usually made of sub-objects. These sub-objects generally have their own properties and configurations<ref>http://www.oodesign.com/builder-pattern.html</ref>. For example consider a Car object. The Car object is made of many sub-objects like Steering Wheel, Wheels and Transmission. The Steering Wheel could be hydraulic driven or electronic. The Wheels could be tubeless or tubed. The Transmission could be automatic or manual. So to configure a Car object you will first have to configure the individual components and then put them together to build the Car.

The Builder pattern allows you to build complex objects by specifying only its type and contents and not be worried about the implementation details. As a result different representations can be created using the same set of simple objects.

Suppose we have to build a Computer which consists of several components like a hard disk, motherboard, disk drives and display. Each of the components can be customized - hard disk can have some specified size, display could be Low-Def or Hi-Def, disk drives could be DVD or Blu-Ray. The motherboard itself has a CPU which could be Dual-Core or Quad-Core. It also has memory of some specified size. Lets define a Computer and other related objects:

class Computer
  attr_accessor :display
  attr_accessor :motherboard
  attr_reader   :drives
  def initialize(display=:low-def, motherboard=Motherboard.new, drives=[])
    @motherboard = motherboard
    @drives = drives
    @display = display
 end 
end

class CPU
  # CPU details...
end

class DualCoreCPU < CPU
  # DualCore CPU details...
end

class QuadCoreCPU < CPU
  # QuadCore CPU details...
end

class Motherboard
 attr_accessor :cpu
 attr_accessor :memory_size
 def initialize(cpu=DualCoreCPU.new, memory_size=1000)
    @cpu = cpu
    @memory_size = memory_size
 end
end

class Drive
 attr_reader :type # either :hard_disk, :blu_ray 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

We can then define a ComputerBuilder class that lets one specify the configuration of each new Computer object you create and assembles them together.

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_blu_ray(writer=false)
   @computer.drives << Drive.new(:blu_ray, 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

Now the ComputerBuilder object can be used to specify the configuration needed on the Computer:

builder = ComputerBuilder.new
builder.turbo
builder.add_blu_ray(true)
builder.add_hard_disk(100000)
computer = builder.computer

Comparison

References

<references />