Computer Science Design Patterns/Print version

From Wikibooks, open books for an open world
Jump to: navigation, search

Contents

Abstract Factory

The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes. In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part of the theme. The client does not know (or care) which concrete objects it gets from each of these internal factories, since it uses only the generic interfaces of their products. This pattern separates the details of implementation of a set of objects from their general usage and relies on object composition, as object creation is implemented in methods exposed in the factory interface. An example of this would be an abstract factory class DocumentCreator that provides interfaces to create a number of products (e.g. createLetter() and createResume()). The system would have any number of derived concrete versions of the DocumentCreator class like FancyDocumentCreator or ModernDocumentCreator, each with a different implementation of createLetter() and createResume() that would create a corresponding object like FancyLetter or ModernResume. Each of these products is derived from a simple abstract class like Letter or Resume of which the client is aware. The client code would get an appropriate instance of the DocumentCreator and call its factory methods. Each of the resulting objects would be created from the same DocumentCreator implementation and would share a common theme (they would all be fancy or modern objects). The client would only need to know how to handle the abstract Letter or Resume class, not the specific version that it got from the concrete factory.

A factory is the location of a concrete class in the code at which objects are constructed. The intent in employing the pattern is to insulate the creation of objects from their usage and to create families of related objects without having to depend on their concrete classes. This allows for new derived types to be introduced with no change to the code that uses the base class.

Use of this pattern makes it possible to interchange concrete implementations without changing the code that uses them, even at runtime. However, employment of this pattern, as with similar design patterns, may result in unnecessary complexity and extra work in the initial writing of code. Additionally, higher levels of separation and abstraction can result in systems which are more difficult to debug and maintain. Therefore, as in all software designs, the trade-offs must be carefully evaluated.

Definition

The essence of the Abstract Factory Pattern is to "Provide an interface for creating families of related or dependent objects without specifying their concrete classes".

Usage

The factory determines the actual concrete type of object to be created, and it is here that the object is actually created (in C++, for instance, by the new operator). However, the factory only returns an abstract pointer to the created concrete object.

This insulates client code from object creation by having clients ask a factory object to create an object of the desired abstract type and to return an abstract pointer to the object.

As the factory only returns an abstract pointer, the client code (that requested the object from the factory) does not know — and is not burdened by — the actual concrete type of the object that was just created. However, the type of a concrete object (and hence a concrete factory) is known by the abstract factory; for instance, the factory may read it from a configuration file. The client has no need to specify the type, since it has already been specified in the configuration file. In particular, this means:

  • 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.

Structure

Class diagram

Abstract factory.svg

The method createButton on the GuiFactory interface returns objects of type Button. What implementation of Button is returned depends on which implementation of GuiFactory is handling the method call.

Note that, for conciseness, this class diagram only shows the class relations for creating one type of object.

Lepus3 chart (legend)

Abstract Factory in LePUS3 vector.svg

UML diagram

Abstract factory UML.svg

Implementations

The output should be either "I'm a WinButton" or "I'm an OSXButton" depending on which kind of factory was used. Note that the Application has no idea what kind of GUIFactory it is given or even what kind of Button that factory creates.

Implementation in C#
/* GUIFactory example -- */
 
using System;
using System.Configuration;
 
namespace AbstractFactory {
    public interface IButton {
        void Paint();
    }
 
    public interface IGUIFactory {
        IButton CreateButton();
    }
 
    public class OSXButton : IButton { // Executes fourth if OS:OSX
        public void Paint() {
            System.Console.WriteLine("I'm an OSXButton");
        }
    }
 
    public class WinButton : IButton { // Executes fourth if OS:WIN
        public void Paint() {
            System.Console.WriteLine("I'm a WinButton");
        }
    }
 
    public class OSXFactory : IGUIFactory { // Executes third if OS:OSX
        IButton IGUIFactory.CreateButton() {
            return new OSXButton();
        }
    }
 
    public class WinFactory : IGUIFactory { // Executes third if OS:WIN
        IButton IGUIFactory.CreateButton() {
            return new WinButton();
        }
    }
 
    public class Application {
        public Application(IGUIFactory factory) {
            IButton button = factory.CreateButton();
            button.Paint();
        }
    }
 
    public class ApplicationRunner {
        static IGUIFactory CreateOsSpecificFactory() { // Executes second
            // Contents of App.Config associated with this C# project
            //<?xml version="1.0" encoding="utf-8" ?>
            //<configuration>
            //  <appSettings>
            //    <!-- Uncomment either Win or OSX OS_TYPE to test -->
            //    <add key="OS_TYPE" value="Win" />
            //    <!-- <add key="OS_TYPE" value="OSX" /> -->
            //  </appSettings>
            //</configuration>
            string sysType = ConfigurationSettings.AppSettings["OS_TYPE"];
            if (sysType == "Win") {
                return new WinFactory();
            } else {
                return new OSXFactory();
            }
        }
 
        static void Main() { // Executes first
            new Application(CreateOsSpecificFactory());
            Console.ReadLine();
        }
    }
}
Implementation in C++
/* GUIFactory example */
 
#include <iostream>
 
class Button {
public:
    virtual void paint() = 0;
    virtual ~Button() { }
};
 
class WinButton : public Button {
public:
    void paint() {
        std::cout << "I'm a WinButton";
    }
};
 
class OSXButton : public Button {
public:
    void paint() {
        std::cout << "I'm an OSXButton";
    }
};
 
class GUIFactory {
public:
    virtual Button* createButton() = 0;
    virtual ~GUIFactory() { }
};
 
class WinFactory : public GUIFactory {
public:
    Button* createButton() {
        return new WinButton();
    }
 
    ~WinFactory() { }
};
 
class OSXFactory : public GUIFactory {
public:
    Button* createButton() {
        return new OSXButton();
    }
 
    ~OSXFactory() { }
};
 
class Application {
public:
    Application(GUIFactory* factory) {
        Button* button = factory->createButton();
        button->paint();
        delete button;
        delete factory;
    }
};
 
GUIFactory* createOsSpecificFactory() {
    int sys;
    std::cout << "Enter OS type (0: Windows, 1: MacOS X): ";
    std::cin >> sys;
 
    if (sys == 0) {
        return new WinFactory();
    } else {
        return new OSXFactory();
    }
}
 
int main() {
    Application application(createOsSpecificFactory());
 
    return 0;
}
Implementation in Java
/* GUIFactory example -- */
 
interface Button {
    void paint();
}
 
interface GUIFactory {
    Button createButton();
}
 
class WinFactory implements GUIFactory {
    public Button createButton() {
        return new WinButton();
    }
}
 
class OSXFactory implements GUIFactory {
    public Button createButton() {
        return new OSXButton();
    }
}
 
class WinButton implements Button {
    public void paint() {
        System.out.println("I'm a WinButton");
    }
}
 
class OSXButton implements Button {
    public void paint() {
        System.out.println("I'm an OSXButton");
    }
}
 
class Application {
    public Application(GUIFactory factory) {
        Button button = factory.createButton();
        button.paint();
    }
}
 
public class ApplicationRunner {
    public static void main(String[] args) {
        new Application(createOsSpecificFactory());
    }
 
    public static GUIFactory createOsSpecificFactory() {
        int sys = readFromConfigFile("OS_TYPE");
        if (sys == 0) return new WinFactory();
        else return new OSXFactory();
    }
}
Implementation in Objective-C
/* GUIFactory example -- */
 
#import <Foundation/Foundation.h>
 
@protocol Button <NSObject>
- (void)paint;
@end
 
@interface WinButton : NSObject <Button>
@end
 
@interface OSXButton : NSObject <Button>
@end
 
@protocol GUIFactory
- (id<Button>)createButton;
@end
 
@interface WinFactory : NSObject <GUIFactory>
@end
 
@interface OSXFactory : NSObject <GUIFactory>
@end
 
@interface Application : NSObject
- (id)initWithGUIFactory:(id)factory;
+ (id)createOsSpecificFactory:(int)type;
@end
 
@implementation WinButton
- (void)paint {
    NSLog(@"I am a WinButton.");
}
@end
 
@implementation OSXButton
- (void)paint {
    NSLog(@"I am a OSXButton.");
}
@end
 
@implementation WinFactory
- (id<Button>)createButton {
    return [[[WinButton alloc] init] autorelease];
}
@end
 
@implementation OSXFactory
- (id<Button>)createButton {
    return [[[OSXButton alloc] init] autorelease];
}
@end
 
@implementation Application
- (id)initWithGUIFactory:(id<GUIFactory>)factory {
    if (self = [super init]) {
        id button = [factory createButton];
        [button paint];
    }
    return self;
}
+ (id<GUIFactory>)createOsSpecificFactory:(int)type {
    if (type == 0) {
        return [[[WinFactory alloc] init] autorelease];
    } else {
        return [[[OSXFactory alloc] init] autorelease];
    }
}
@end
 
int main(int argc, char* argv[]) {
    @autoreleasepool {
        [[Application alloc] initWithGUIFactory:[Application createOsSpecificFactory:0]];// 0 is WinButton
    }
    return 0;
}
Implementation in Lua
--[[
    Because Lua is a highly dynamic Language, an OOP scheme is implemented by the programmer.
    The OOP scheme implemented here implements interfaces using documentation.
 
 
    A Factory Supports:
     - factory:CreateButton()
 
    A Button Supports:
     - button:Paint()
]]
 
-- Create the OSXButton Class
do
    OSXButton = {}
    local mt = { __index = OSXButton }
 
    function OSXButton:new()
        local inst = {}
        setmetatable(inst, mt)
        return inst
    end
 
    function OSXButton:Paint()
        print("I'm a fancy OSX button!")
    end
end
 
-- Create the WinButton Class
do
    WinButton = {}
    local mt = { __index = WinButton }
 
    function WinButton:new()
        local inst = {}
        setmetatable(inst, mt)
        return inst
    end
 
    function WinButton:Paint()
        print("I'm a fancy Windows button!")
    end
end
 
-- Create the OSXGuiFactory Class
do
    OSXGuiFactory = {}
    local mt = { __index = OSXGuiFactory }
 
    function OSXGuiFactory:new()
        local inst = {}
        setmetatable(inst, mt)
        return inst
    end
 
    function OSXGuiFactory:CreateButton()
        return OSXButton:new()
    end
end
 
-- Create the WinGuiFactory Class
do
    WinGuiFactory = {}
    local mt = { __index = WinGuiFactory }
 
    function WinGuiFactory:new()
        local inst = {}
        setmetatable(inst, mt)
        return inst
    end
 
    function WinGuiFactory:CreateButton()
        return WinButton:new()
    end
end
 
-- Table to keep track of what GuiFactories are available
GuiFactories = {
    ["Win"] = WinGuiFactory,
    ["OSX"] = OSXGuiFactory,
}
 
--[[ Inside an OS config script ]]
OS_VERSION = "Win"
 
--[[ Using the Abstract Factory in some the application script ]]
 
-- Selecting the factory based on OS version
MyGuiFactory = GuiFactories[OS_VERSION]:new()
 
-- Using the factory
osButton = MyGuiFactory:CreateButton()
osButton:Paint()
Implementation in PHP

This abstract factory creates books.

/*
 * BookFactory classes
*/
abstract class AbstractBookFactory {
    abstract function makePHPBook();
    abstract function makeMySQLBook();
}
 
class OReillyBookFactory extends AbstractBookFactory {
    private $context = "OReilly";
    function makePHPBook() {
        return new OReillyPHPBook;
    }
    function makeMySQLBook() {
        return new OReillyMySQLBook;
    }
}
 
class SamsBookFactory extends AbstractBookFactory {
    private $context = "Sams";
    function makePHPBook() {
        return new SamsPHPBook;
    }
    function makeMySQLBook() {
        return new SamsMySQLBook;
    }
}
 
/*
 *   Book classes
*/
abstract class AbstractBook {
    abstract function getAuthor();
    abstract function getTitle();
}
 
abstract class AbstractMySQLBook {
    private $subject = "MySQL";
}
 
class OReillyMySQLBook extends AbstractMySQLBook {
    private $author;
    private $title;
    function __construct() {
        $this->author = 'George Reese, Randy Jay Yarger, and Tim King';
        $this->title = 'Managing and Using MySQL';
    }
    function getAuthor() {
        return $this->author;
    }
    function getTitle() {
        return $this->title;
    }
}
 
class SamsMySQLBook extends AbstractMySQLBook {
    private $author;
    private $title;
    function __construct() {
        $this->author = 'Paul Dubois';
        $this->title = 'MySQL, 3rd Edition';
    }
    function getAuthor() {
        return $this->author;
    }
    function getTitle() {
        return $this->title;
    }
}
 
abstract class AbstractPHPBook {
    private $subject = "PHP";
}
 
class OReillyPHPBook extends AbstractPHPBook {
    private $author;
    private $title;
    private static $oddOrEven = 'odd';
    function __construct() {
        //alternate between 2 books
        if ('odd' == self::$oddOrEven) {
            $this->author = 'Rasmus Lerdorf and Kevin Tatroe';
            $this->title = 'Programming PHP';
            self::$oddOrEven = 'even';
        } else {
            $this->author = 'David Sklar and Adam Trachtenberg';
            $this->title = 'PHP Cookbook';
            self::$oddOrEven = 'odd';
        }
    }
    function getAuthor() {
        return $this->author;
    }
    function getTitle() {
        return $this->title;
    }
}
 
class SamsPHPBook extends AbstractPHPBook {
    private $author;
    private $title;
    function __construct() {
        //alternate randomly between 2 books
        mt_srand((double)microtime() * 10000000);
        $rand_num = mt_rand(0, 1);
 
        if (1 > $rand_num) {
            $this->author = 'George Schlossnagle';
            $this->title = 'Advanced PHP Programming';
        }
        else {
            $this->author = 'Christian Wenz';
            $this->title = 'PHP Phrasebook';
        }
    }
    function getAuthor() {
        return $this->author;
    }
    function getTitle() {
        return $this->title;
    }
}
 
/*
 *   Initialization
*/
 
writeln('BEGIN TESTING ABSTRACT FACTORY PATTERN');
writeln('');
 
writeln('testing OReillyBookFactory');
$bookFactoryInstance = new OReillyBookFactory;
testConcreteFactory($bookFactoryInstance);
writeln('');
 
writeln('testing SamsBookFactory');
$bookFactoryInstance = new SamsBookFactory;
testConcreteFactory($bookFactoryInstance);
 
writeln("END TESTING ABSTRACT FACTORY PATTERN");
writeln('');
 
function testConcreteFactory($bookFactoryInstance) {
    $phpBookOne = $bookFactoryInstance->makePHPBook();
    writeln('first php Author: '.$phpBookOne->getAuthor());
    writeln('first php Title: '.$phpBookOne->getTitle());
 
    $phpBookTwo = $bookFactoryInstance->makePHPBook();
    writeln('second php Author: '.$phpBookTwo->getAuthor());
    writeln('second php Title: '.$phpBookTwo->getTitle());
 
    $mySqlBook = $bookFactoryInstance->makeMySQLBook();
    writeln('MySQL Author: '.$mySqlBook->getAuthor());
    writeln('MySQL Title: '.$mySqlBook->getTitle());
}
 
function writeln($line_in) {
    echo $line_in."<br/>";
}
Implementation in Scala

This abstract factory creates musicians.

// concrete implementation
class DrummerFactory extends MusicianAbstractFactory {
  def create(): Musician = new Drummer()
}
 
class GuitarPlayerFactory extends MusicianAbstractFactory {
  def create(): Musician = new GuitarPlayer()
}
 
class GuitarPlayer extends Musician {
  def play(song: String) {
    println(s"I'm guitar player and I play $song")
  }
}
class Drummer extends Musician {
  def play(song: String) {
    println(s"I'm drummer and I play '$song'")
  }
}
 
object FactoryCreator {
  def getFactory(config: Boolean): MusicianAbstractFactory =
    if (config) {
      new DrummerFactory
    } else {
      new GuitarPlayerFactory
    }
}
// interfaces to import
trait Musician {
  def play(song: String)
}
 
trait MusicianAbstractFactory {
  def create(): Musician
}
import implementation.FactoryCreator
 
object AbstractFactoryTest {
  def readFromConfig(): Boolean = true
 
  def main(args: Array[String]) {
    val factory = FactoryCreator.getFactory(readFromConfig())
    val musician = factory.create()
    musician.play("Highway to hell")
  }
}
Implementation in Python
import platform
 
class GuiFactory():
    """Abstract Factory"""
 
    @classmethod
    def getFactory(Class):
        if platform.system() == "Windows":
            return WinFactory()
        elif platform.system == "OSX":
            return OSXFactory()
 
    class Button:
        """ Abstract Product"""
        def paint(self):
            raise NotImplementedError
 
    @classmethod
    def getButton(Class):
        return Class.Button()
 
class WinFactory(GuiFactory):
    """Concrete Factory"""
 
    class Button(GuiFactory.Button):
        """Concrete Product"""
        def paint(self):
            print "I am a Windows button"
 
class OSXFactory(GuiFactory):
    """Concrete Factory"""
 
    class Button(GuiFactory.Button):
        """Concrete Product"""
        def paint(self):
            print "I am a OSX button"
 
def main():
    """Application"""
    factory = GuiFactory.getFactory()
    factory.getButton().paint()
 
if __name__ == "__main__":
    main()

Adapter

The adapter pattern is used when a client class has to call an incompatible provider class. Let's imagine a MailingClient class that needs to call a method on the LegacyEmployee class:

MailingClient
+firstName: String
+lastName: String
 
IEmployee
+mailEmployee(firstName: String, lastName: String)
 
LegacyEmployee
+mailEmployee(fullName: String)

MailingClient already calls classes that implement the IEmployee interface but the LegacyEmployee doesn't implement it. We could add a new method to LegacyEmployee to implement the IEmployee interface but LegacyEmployee is legacy code and can't be changed. We could modify the MailingClient class to call LegacyEmployee but it needs to change every call. The formatting code would be duplicated everywhere. Moreover, MailingClient won't be able to call other provider class that implement the IEmployee interface any more.

So the solution is to code the formatting code in another independent class, an adapter, also called a wrapper class:

IEmployee
+mailEmployee(firstName: String, lastName: String)
UML Open arrow up.svg
MailingClient
+firstName: String
+lastName: String
Pfeil2.svg
EmployeeAdapter
+mailEmployee(firstName: String, lastName: String)
Pfeil2.svg
LegacyEmployee
+mailEmployee(fullName: String)

EmployeeAdapter implements the IEmployee interface. MailingClient calls EmployeeAdapter. EmployeeAdapter formats the data and calls LegacyEmployee. This type of adapter is called an object adapter. The other type of adapter is the class adapter.

Clipboard

To do:
Describe the class adapter.

Examples

The WebGL-2D is a JavaScript library that implements the adapter pattern. This library is used for the HTML5 canvas element. The canvas element has two interfaces: 2d and WebGL. The first one is very simple to use and the second is much more complex but optimized and faster. The WebGL-2D 'adapts' the WebGL interface to the 2d interface, so that the client calls the 2d interface only.

Cost

Think twice before implementing this pattern. This pattern should not be planned at design time. If you plan to use it for a project from scratch, this means that you don't understand this pattern. It should be used only with legacy code. It is the least bad solution.

Creation

Its implementation is easy but can be expensive. You should not have to refactor the code as the client and the provider should not be able to work together yet.

Maintenance

This is the worst part. Most of the code has redundancies (but less than without the pattern). The modern interface should always provide as much information as the legacy interface needs to work. If one information is missing on the modern interface, it can call the pattern into question.

Removal

This pattern can be easily removed as automatic refactoring operations can easily remove its existence.

Advices

  • Put the adapter term in the name of the adapter class to indicate the use of the pattern to the other developers.

Implementation

Object Adapter

Implementation in Java

Our company has been created by a merger. One list of employees is available in a database you can access via the CompanyAEmployees class:

  1. /**
    
  2.  * Employees of the Company A.
    
  3.  */
    
  4. public class CompanyAEmployees {
    
  5.   /**
    
  6.    * Retrieve the employee information from the database.
    
  7.    *
    
  8.    * @param sqlQuery The SQL query.
    
  9.    * @return The employee object.
    
  10.    */
    
  11.   public Employee getEmployee(String sqlQuery) {
    
  12.       Employee employee = null;
    
  13.  
    
  14.       // Execute the request.
    
  15.  
    
  16.       return employee;
    
  17.     }
    
  18. }
    

One list of employees is available in a LDAP you can access via the CompanyBEmployees class:

  1. /**
    
  2.  * Employees of the Company B.
    
  3.  */
    
  4. public class CompanyBEmployees {
    
  5.   /**
    
  6.    * Retrieve the employee information from the LDAP.
    
  7.    *
    
  8.    * @param sqlQuery The SQL query.
    
  9.    * @return The employee object.
    
  10.    */
    
  11.   public Employee getEmployee(String distinguishedName) {
    
  12.       Employee employee = null;
    
  13.  
    
  14.       // Call the LDAP.
    
  15.  
    
  16.       return employee;
    
  17.     }
    
  18. }
    


To access both to the former employees of the company A and the former employees of the company B, we define an interface that will be used by two adapters, EmployeeBrowser:

  1. /**
    
  2.  * Retrieve information about the employees.
    
  3.  */
    
  4. interface EmployeeBrowser {
    
  5.   /**
    
  6.    * Retrieve the employee information.
    
  7.    *
    
  8.    * @param direction The employee direction.
    
  9.    * @param division The employee division.
    
  10.    * @param department The employee departement.
    
  11.    * @param service The employee service.
    
  12.    * @param firstName The employee firstName.
    
  13.    * @param lastName The employee lastName.
    
  14.    *
    
  15.    * @return The employee object.
    
  16.    */
    
  17.   Employee getEmployee(String direction, String division, String department, String service, String firstName, String lastName);
    
  18. }
    

We create an adapter for the code of the former company A, CompanyAAdapter:

  1. /**
    
  2.  * Adapter for the company A legacy code.
    
  3.  */
    
  4. public class CompanyAAdapter implements EmployeeBrowser {
    
  5.   /**
    
  6.    * Retrieve the employee information.
    
  7.    *
    
  8.    * @param direction The employee direction.
    
  9.    * @param division The employee division.
    
  10.    * @param department The employee department.
    
  11.    * @param service The employee service.
    
  12.    * @param firstName The employee firstName.
    
  13.    * @param lastName The employee lastName.
    
  14.    *
    
  15.    * @return The employee object.
    
  16.    */
    
  17.   public Employee getEmployee(String direction, String division, String department, String service, String firstName, String lastName) {
    
  18.     String distinguishedName = "SELECT *"
    
  19.       + " FROM t_employee as employee"
    
  20.       + " WHERE employee.company= 'COMPANY A'"
    
  21.       + " AND employee.direction = " + direction
    
  22.       + " AND employee.division = " + division
    
  23.       + " AND employee.department = " + department
    
  24.       + " AND employee.service = " + service
    
  25.       + " AND employee.firstName = " + firstName
    
  26.       + " AND employee.lastName = " + lastName;
    
  27.  
    
  28.     CompanyAEmployees companyAEmployees = new CompanyAEmployees();
    
  29.     return companyAEmployees.getEmployee(distinguishedName);
    
  30.   }
    
  31. }
    

We create an adapter for the code of the former company B, CompanyBAdapter:

  1. /**
    
  2.  * Adapter for the company B legacy code.
    
  3.  */
    
  4. public class CompanyBAdapter implements EmployeeBrowser {
    
  5.   /**
    
  6.    * Retrieve the employee information.
    
  7.    *
    
  8.    * @param direction The employee direction.
    
  9.    * @param division The employee division.
    
  10.    * @param department The employee department.
    
  11.    * @param service The employee service.
    
  12.    * @param firstName The employee firstName.
    
  13.    * @param lastName The employee lastName.
    
  14.    *
    
  15.    * @return The employee object.
    
  16.    */
    
  17.   public Employee getEmployee(String direction, String division, String department, String service, String firstName, String lastName) {
    
  18.     String distinguishedName = "ov1 = " + direction
    
  19.       + ", ov2 = " + division
    
  20.       + ", ov3 = " + department
    
  21.       + ", ov4 = " + service
    
  22.       + ", cn = " + firstName + lastName;
    
  23.  
    
  24.     CompanyBEmployees companyBEmployees = new CompanyBEmployees();
    
  25.     return companyBEmployees.getEmployee(distinguishedName);
    
  26.   }
    
  27. }
    
Implementation in Ruby

Ruby

class Adaptee
  def specific_request
    # do something
  end
end
 
class Adapter
  def initialize(adaptee)
    @adaptee = adaptee
  end
 
  def request
    @adaptee.specific_request
  end
end
 
client = Adapter.new(Adaptee.new)
client.request
Implementation in Python
class Adaptee:
    def specific_request(self):
        return 'Adaptee'
 
class Adapter:
    def __init__(self, adaptee):
        self.adaptee = adaptee
 
    def request(self):
        return self.adaptee.specific_request()
 
client = Adapter(Adaptee())
print client.request()
Implementation in Scala
trait Socket220V {
  def plug220()
}
 
trait Socket19V {
  def plug19()
}
 
class Laptop extends Socket19V {
  def plug19() {
    println("Charging....")
  }
}
 
class LaptopAdapter(laptop: Laptop) extends Socket220V {
  def plug220() {
    println("Transform1...")
    laptop.plug19()
  }
}
 
object Test {
  def main(args: Array[String]) {
    //you can do it like this:
    new LaptopAdapter(new Laptop).plug220()
 
    //or like this (doesn't need LaptopAdapter)
    new Laptop with Socket220V {
      def plug220() {
        println("Transform2...")
        this.plug19()
      }
    } plug220()
  }
}

Class Adapter

Implementation in Python
class Adaptee1:
    def __init__(self, *args, **kw):
        pass
 
    def specific_request(self):
        return 'Adaptee1'
 
class Adaptee2:
    def __init__(self, *args, **kw):
        pass
 
    def specific_request(self):
        return 'Adaptee2'
 
class Adapter(Adaptee1, Adaptee2):
    def __init__(self, *args, **kw):
        Adaptee1.__init__(self, *args, **kw)
        Adaptee2.__init__(self, *args, **kw)
 
    def request(self):
        return Adaptee1.specific_request(self), Adaptee2.specific_request(self)
 
client = Adapter()
print client.request()

Bridge

Bridge pattern is useful when a code often changes for an implementation as well as for a use of code. In your application, you should have provider classes and client classes:

Client1
-provider: Provider
 
Client2
-provider: Provider
...
ClientN
-provider: Provider
◊—
Provider
+doProcess()
UML Open arrow up.svg
UML Open arrow up.svg
...
UML Open arrow up.svg
Provider1
+doProcess()
Provider2
+doProcess()
...
ProviderN
+doProcess()

Each client class can interact with each provider class. However, if the implementation changes, the method signatures of the Provider interface may change and all the client classes have to change. In the same way, if the client classes need a new interface, we need to rewrite all the providers. The solution is to add a bridge, that is to say a class that will be called by the clients, that contains a reference to the providers and forward the client call to the providers.

Client1
-bridge: Bridge
 
Client2
-bridge: Bridge
...
ClientN
-bridge: Bridge
◊—
Bridge
-provider: Provider
+doProcessForClient()
◊—
Provider
+doProcess()
UML Open arrow up.svg
UML Open arrow up.svg
...
UML Open arrow up.svg
Provider1
+doProcess()
Provider2
+doProcess()
...
ProviderN
+doProcess()

In the future, the two interfaces (client/bridge and bridge/provider) may change independently and the bridge may trans-code the call order.

Examples

It's hard to find an example in a library as this pattern is designed for versatile specifications and a library does not change constantly between two versions.

Cost

The cost of this pattern is the same as the adapter. It can be planned at design time but the best way to decide to add it is the experience feedback. Implement it when you have frequently changed an interface in a short time.

Creation

Its implementation is easy but can be expensive. It depends on the complexity of the interface. The more you have methods, the more it's expensive.

Maintenance

If you always update the client/bridge interface and the bridge/provider interface the same way, it would be more expensive than if you do not implement the design pattern.

Removal

This pattern can be easily removed as automatic refactoring operations can easily remove its existence.

Advices

  • Put the bridge term in the name of the bridge class to indicate the use of the pattern to the other developers.

Implementation

Implementation in Java

The following Java (SE 6) program illustrates the bridge pattern.

  1. /**
    
  2.  * Implementor
    
  3.  */
    
  4. interface DrawingAPI {
    
  5.     public void drawCircle(double x, double y, double radius);
    
  6. }
    
  1. /**
    
  2.  * ConcreteImplementor  1/2
    
  3.  */
    
  4. class DrawingAPI1 implements DrawingAPI {
    
  5.    public void drawCircle(double x, double y, double radius) {
    
  6.         System.out.printf("API1.circle at %f:%f radius %f\n", x, y, radius);
    
  7.    }
    
  8. }
    
  1. /**
    
  2.  * ConcreteImplementor 2/2
    
  3.  */
    
  4. class DrawingAPI2 implements DrawingAPI {
    
  5.    public void drawCircle(double x, double y, double radius) {
    
  6.         System.out.printf("API2.circle at %f:%f radius %f\n", x, y, radius);
    
  7.    }
    
  8. }
    
  1. /**
    
  2.  * Abstraction
    
  3.  */
    
  4. abstract class Shape {
    
  5.    protected DrawingAPI drawingAPI;
    
  6.  
    
  7.    protected Shape(DrawingAPI drawingAPI) {
    
  8.       this.drawingAPI = drawingAPI;
    
  9.    }
    
  10.  
    
  11.    public abstract void draw();                             // low-level
    
  12.    public abstract void resizeByPercentage(double pct);     // high-level
    
  13. }
    
  1. /**
    
  2.  * Refined Abstraction
    
  3.  */
    
  4. class CircleShape extends Shape {
    
  5.    private double x, y, radius;
    
  6.    public CircleShape(double x, double y, double radius, DrawingAPI drawingAPI) {
    
  7.       super(drawingAPI);
    
  8.       this.x = x;
    
  9.       this.y = y;
    
  10.       this.radius = radius;
    
  11.    }
    
  12.  
    
  13.    // low-level i.e. Implementation specific
    
  14.    public void draw() {
    
  15.         drawingAPI.drawCircle(x, y, radius);
    
  16.    }
    
  17.  
    
  18.    // high-level i.e. Abstraction specific
    
  19.    public void resizeByPercentage(double pct) {
    
  20.         radius *= pct;
    
  21.    }
    
  22. }
    
  1. /**
    
  2.  * Client
    
  3.  */
    
  4. class BridgePattern {
    
  5.    public static void main(String[] args) {
    
  6.        Shape[] shapes = new Shape[] {
    
  7.            new CircleShape(1, 2, 3, new DrawingAPI1()),
    
  8.            new CircleShape(5, 7, 11, new DrawingAPI2()),
    
  9.        };
    
  10.  
    
  11.        for (Shape shape : shapes) {
    
  12.            shape.resizeByPercentage(2.5);
    
  13.            shape.draw();
    
  14.        }
    
  15.    }
    
  16. }
    

It will output:

API1.circle at 1.000000:2.000000 radius 7.5000000
API2.circle at 5.000000:7.000000 radius 27.500000
Implementation in PHP
interface DrawingAPI {
    function drawCircle($dX, $dY, $dRadius);
}
 
class DrawingAPI1 implements DrawingAPI {
 
    public function drawCircle($dX, $dY, $dRadius) {
        echo "API1.circle at ".$dX.":".$dY." radius ".$dRadius."<br/>";
    }
}
 
class DrawingAPI2 implements DrawingAPI {
 
    public function drawCircle($dX, $dY, $dRadius) {
        echo "API2.circle at ".$dX.":".$dY." radius ".$dRadius."<br/>";
    }
}
 
abstract class Shape {
 
    protected $oDrawingAPI;
 
    public abstract function draw();
    public abstract function resizeByPercentage($dPct);
 
    protected function __construct(DrawingAPI $oDrawingAPI) {
        $this->oDrawingAPI = $oDrawingAPI;
    }
}
 
class CircleShape extends Shape {
 
    private $dX;
    private $dY;
    private $dRadius;
 
    public function __construct(
            $dX, $dY,
            $dRadius,
            DrawingAPI $oDrawingAPI
    ) {
        parent::__construct($oDrawingAPI);
        $this->dX = $dX;
        $this->dY = $dY;
        $this->dRadius = $dRadius;
    }
 
    public function draw() {
        $this->oDrawingAPI->drawCircle(
                $this->dX,
                $this->dY,
                $this->dRadius
        );
    }
 
    public function resizeByPercentage($dPct) {
        $this->dRadius *= $dPct;
    }
}
 
class Tester {
 
    public static function main()  {
        $aShapes = array(
            new CircleShape(1, 3, 7,  new DrawingAPI1()),
            new CircleShape(5, 7, 11, new DrawingAPI2()),
        );
 
        foreach ($aShapes as $shapes) {
            $shapes->resizeByPercentage(2.5);
            $shapes->draw();
        }
    }
}
 
Tester::main();

Output:

   API1.circle at 1:3 radius 17.5
   API2.circle at 5:7 radius 27.5
Implementation in C#

C#

The following C# program illustrates the "shape" example given above and will output:

API1.circle at 1:2 radius 7.5
API2.circle at 5:7 radius 27.5
 using System;
 
 /** "Implementor" */
 interface IDrawingAPI {
    void DrawCircle(double x, double y, double radius);
 }
 
 /** "ConcreteImplementor" 1/2 */
 class DrawingAPI1 : IDrawingAPI {
    public void DrawCircle(double x, double y, double radius)
    {
        System.Console.WriteLine("API1.circle at {0}:{1} radius {2}", x, y, radius);
    }
 }
 
 /** "ConcreteImplementor" 2/2 */
 class DrawingAPI2 : IDrawingAPI
 {
    public void DrawCircle(double x, double y, double radius)
    {
        System.Console.WriteLine("API2.circle at {0}:{1} radius {2}", x, y, radius);
    }
 }
 
 /** "Abstraction" */
 interface IShape {
    void Draw();                             // low-level (i.e. Implementation-specific)
    void ResizeByPercentage(double pct);     // high-level (i.e. Abstraction-specific)
 }
 
 /** "Refined Abstraction" */
 class CircleShape : IShape {
    private double x, y, radius;
    private IDrawingAPI drawingAPI;
    public CircleShape(double x, double y, double radius, IDrawingAPI drawingAPI)
    {
        this.x = x;  this.y = y;  this.radius = radius;
        this.drawingAPI = drawingAPI;
    }
    // low-level (i.e. Implementation-specific)
    public void Draw() { drawingAPI.DrawCircle(x, y, radius); }
    // high-level (i.e. Abstraction-specific)
    public void ResizeByPercentage(double pct) { radius *= pct; }
 }
 
 /** "Client" */
 class BridgePattern {
    public static void Main(string[] args) {
        IShape[] shapes = new IShape[2];
        shapes[0] = new CircleShape(1, 2, 3, new DrawingAPI1());
        shapes[1] = new CircleShape(5, 7, 11, new DrawingAPI2());
 
        foreach (IShape shape in shapes) {
            shape.ResizeByPercentage(2.5);
            shape.Draw();
        }
    }
 }

C# using generics

The following C# program illustrates the "shape" example given above and will output:

API1.circle at 1:2 radius 7.5
API2.circle at 5:7 radius 27.5
 using System;
 
 /** "Implementor" */
 interface IDrawingAPI {
    void DrawCircle(double x, double y, double radius);
 }
 
 /** "ConcreteImplementor" 1/2 */
 struct DrawingAPI1 : IDrawingAPI {
    public void DrawCircle(double x, double y, double radius)
    {
        System.Console.WriteLine("API1.circle at {0}:{1} radius {2}", x, y, radius);
    }
 }
 
 /** "ConcreteImplementor" 2/2 */
 struct DrawingAPI2 : IDrawingAPI
 {
    public void DrawCircle(double x, double y, double radius)
    {
        System.Console.WriteLine("API2.circle at {0}:{1} radius {2}", x, y, radius);
    }
 }
 
 /** "Abstraction" */
 interface IShape {
    void Draw();                             // low-level (i.e. Implementation-specific)
    void ResizeByPercentage(double pct);     // high-level (i.e. Abstraction-specific)
 }
 
 /** "Refined Abstraction" */
 class CircleShape<T> : IShape
    where T : struct, IDrawingAPI
 {
    private double x, y, radius;
    private IDrawingAPI drawingAPI = new T();
    public CircleShape(double x, double y, double radius)
    {
        this.x = x;  this.y = y;  this.radius = radius;
    }
    // low-level (i.e. Implementation-specific)
    public void Draw() { drawingAPI.DrawCircle(x, y, radius); }
    // high-level (i.e. Abstraction-specific)
    public void ResizeByPercentage(double pct) { radius *= pct; }
 }
 
 /** "Client" */
 class BridgePattern {
    public static void Main(string[] args) {
        IShape[] shapes = new IShape[2];
        shapes[0] = new CircleShape<DrawingAPI1>(1, 2, 3);
        shapes[1] = new CircleShape<DrawingAPI2>(5, 7, 11);
 
        foreach (IShape shape in shapes) {
            shape.ResizeByPercentage(2.5);
            shape.Draw();
        }
    }
 }
Implementation in Python

The following Python program illustrates the "shape" example given above and will output:

API1.circle at 1:2 7.5
API2.circle at 5:7 27.5
# Implementor
class DrawingAPI:
    def drawCircle(x, y, radius):
        pass
 
# ConcreteImplementor 1/2
class DrawingAPI1(DrawingAPI):
    def drawCircle(self, x, y, radius):
        print "API1.circle at %f:%f radius %f" % (x, y, radius)
 
# ConcreteImplementor 2/2
class DrawingAPI2(DrawingAPI):
    def drawCircle(self, x, y, radius):
        print "API2.circle at %f:%f radius %f" % (x, y, radius)
 
# Abstraction
class Shape:
    # low-level
    def draw(self):
        pass
 
    # high-level
    def resizeByPercentage(self, pct):
        pass
 
# Refined Abstraction
class CircleShape(Shape):
    def __init__(self, x, y, radius, drawingAPI):
        self.__x = x
        self.__y = y
        self.__radius = radius
        self.__drawingAPI = drawingAPI
 
    # low-level i.e. Implementation specific
    def draw(self):
        self.__drawingAPI.drawCircle(self.__x, self.__y, self.__radius)
 
    # high-level i.e. Abstraction specific
    def resizeByPercentage(self, pct):
        self.__radius *= pct
 
def main():
    shapes = [
        CircleShape(1, 2, 3, DrawingAPI1()),
        CircleShape(5, 7, 11, DrawingAPI2())
    ]
 
    for shape in shapes:
        shape.resizeByPercentage(2.5)
        shape.draw()
 
if __name__ == "__main__":
    main()
Implementation in Ruby

An example in Ruby.

class Abstraction
  def initialize(implementor)
    @implementor = implementor
  end
 
  def operation
    raise 'Implementor object does not respond to the operation method' unless @implementor.respond_to?(:operation)
    @implementor.operation
  end
end
 
class RefinedAbstraction < Abstraction
  def operation
    puts 'Starting operation... '
    super
  end
end
 
class Implementor
  def operation
    puts 'Doing necessary stuff'
  end
end
 
class ConcreteImplementorA < Implementor
  def operation
    super
    puts 'Doing additional stuff'
  end
end
 
class ConcreteImplementorB < Implementor
  def operation
    super
    puts 'Doing other additional stuff'
  end
end
 
normal_with_a = Abstraction.new(ConcreteImplementorA.new)
normal_with_a.operation
# Doing necessary stuff
# Doing additional stuff
 
normal_with_b = Abstraction.new(ConcreteImplementorB.new)
normal_with_b.operation
# Doing necessary stuff
# Doing other additional stuff
 
refined_with_a = RefinedAbstraction.new(ConcreteImplementorA.new)
refined_with_a.operation
# Starting operation...
# Doing necessary stuff
# Doing additional stuff
 
refined_with_b = RefinedAbstraction.new(ConcreteImplementorB.new)
refined_with_b.operation
# Starting operation...
# Doing necessary stuff
# Doing other additional stuff
Implementation in Scala

A Scala implementation of the Java drawing example with the same output.

  /** "Implementor" */
  trait DrawingAPI {
    def drawCircle(x:Double, y:Double, radius:Double)
  }
 
  /** "ConcreteImplementor" 1/2 */
  class DrawingAPI1 extends DrawingAPI {
    def drawCircle(x:Double, y:Double, radius:Double) {
      printf("API1.circle at %f:%f radius %f\n", x, y, radius)
    }
  }
 
  /** "ConcreteImplementor" 2/2 */
  class DrawingAPI2 extends DrawingAPI {
    def drawCircle(x:Double, y:Double, radius:Double) {
      printf("API2.circle at %f:%f radius %f\n", x, y, radius)
    }
  }
 
  /** "Abstraction" */
  trait Shape {
     def draw()                             // low-level
     def resizeByPercentage(pct:Double)     // high-level
  }
 
  /** "Refined Abstraction" */
  class CircleShape(var x:Double, var y:Double,
    var radius:Double, val drawingAPI:DrawingAPI) extends Shape {
 
    // low-level i.e. Implementation specific
    def draw() = drawingAPI.drawCircle(x, y, radius)
 
    // high-level i.e. Abstraction specific
    def resizeByPercentage(pct:Double) = radius *= pct
  }
 
  /** "Client" */
  val shapes = List(
    new CircleShape(1, 2, 3, new DrawingAPI1),
    new CircleShape(5, 7, 11, new DrawingAPI2)
  )
 
  shapes foreach { shape =>
    shape.resizeByPercentage(2.5)
    shape.draw()
  }
Implementation in D

An example in D.

import std.stdio;
 
/** "Implementor" */
interface DrawingAPI {
    void drawCircle(double x, double y, double radius);
}
 
/** "ConcreteImplementor" 1/2 */
class DrawingAPI1: DrawingAPI {
    void drawCircle(double x, double y, double radius) {
        writefln("\nAPI1.circle at %f:%f radius %f", x, y, radius);
    }
}
 
/** "ConcreteImplementor" 2/2 */
class DrawingAPI2: DrawingAPI {
    void drawCircle(double x, double y, double radius) {
        writefln("\nAPI2.circle at %f:%f radius %f", x, y, radius);
    }
}
 
/** "Abstraction" */
interface Shape {
    void draw(); // low-level
    void resizeByPercentage(double pct); // high-level
}
 
/** "Refined Abstraction" */
class CircleShape: Shape {
    this(double x, double y, double radius, DrawingAPI drawingAPI) {
        this.x = x;
        this.y = y;
        this.radius = radius;
        this.drawingAPI = drawingAPI;
    }
 
    // low-level i.e. Implementation specific
    void draw() {
        drawingAPI.drawCircle(x, y, radius);
    }
    // high-level i.e. Abstraction specific
    void resizeByPercentage(double pct) {
        radius *= pct;
    }
private:
    double x, y, radius;
    DrawingAPI drawingAPI;
}
 
int main(string[] argv) {
    auto api1 = new DrawingAPI1();
    auto api2 = new DrawingAPI2();
 
    auto c1 = new CircleShape(1, 2, 3, api1);
    auto c2 = new CircleShape(5, 7, 11, api2);
 
    Shape[4] shapes;
    shapes[0] = c1;
    shapes[1] = c2;
 
    shapes[0].resizeByPercentage(2.5);
    shapes[0].draw();
    shapes[1].resizeByPercentage(2.5);
    shapes[1].draw();
 
    return 0;
}
Implementation in Perl

This example in Perl uses the MooseX::Declare module.

# Implementor
role Drawing::API {
    requires 'draw_circle';
}
 
# Concrete Implementor 1
class Drawing::API::1 with Drawing::API {
    method draw_circle(Num $x, Num $y, Num $r) {
        printf "API1.circle at %f:%f radius %f\n", $x, $y, $r;
    }
}
 
# Concrete Implementor 2
class Drawing::API::2 with Drawing::API {
    method draw_circle(Num $x, Num $y, Num $r) {
        printf "API2.circle at %f:%f radius %f\n", $x, $y, $r;
    }
}
 
# Abstraction
role Shape {
    requires qw( draw resize );
}
 
# Refined Abstraction
class Shape::Circle with Shape {
    has $_  => ( is => 'rw', isa  => 'Any' ) for qw( x y r );
    has api => ( is => 'ro', does => 'Drawing::API' );
 
    method draw() {
        $self->api->draw_circle( $self->x, $self->y, $self->r );
    }
 
    method resize(Num $percentage) {
        $self->{r} *= $percentage;
    }
}
 
my @shapes = (
    Shape::Circle->new( x=>1, y=>2, r=>3,  api => Drawing::API::1->new ),
    Shape::Circle->new( x=>5, y=>7, r=>11, api => Drawing::API::2->new ),
)
 
$_->resize( 2.5 ) and $_->draw for @shapes;

Builder

The builder pattern is useful to avoid a huge list of constructors for a class. Let's imagine a class that store the mode of transport (of an employee for instance). Here is the constructor that takes a MetroLine object as parameter:

modeOfTransport(MetroLine)

Now we need a constructor for the one that uses the car, the bus or the train:

new modeOfTransport(MetroLine)

new modeOfTransport()
new modeOfTransport(BusLine)

new modeOfTransport(Train)

For some of them, we need to indicate a travel allowance:

new modeOfTransport(MetroLine)

new modeOfTransport(MetroLine, Integer)
new modeOfTransport()
new modeOfTransport(Integer)
new modeOfTransport(BusLine)
new modeOfTransport(BusLine, Integer)
new modeOfTransport(Train)

new modeOfTransport(Train, Integer)

We have employees that have several modes of transport to go to work. We realize that the list of constructors is coming to a mess. Each new parameter leads to an exponent duplication of constructors. If some parameters have the same type, it becomes very confusing. A solution to this issue would be to first build a fake object and then fill it calling methods on it:

new modeOfTransport(MetroLine)

modeOfTransport.setMetroLine(MetroLine)
modeOfTransport.setCarTravel()
modeOfTransport.setBusLine(BusLine)
modeOfTransport.setTrain(Train)

modeOfTransport.setTravelAllowance(Integer)

The list of methods is no more exponent but the state of the object may be inconsistent. A better solution would be to set all the parameters to an object of another class, a pre-constructor, and then pass this object to the constructor of ModeOfTransport:

modeOfTransportPreConstructor.setMetroLine(MetroLine)

modeOfTransportPreConstructor.setCarTravel()
modeOfTransportPreConstructor.setBusLine(BusLine)
modeOfTransportPreConstructor.setTrain(Train)
modeOfTransportPreConstructor.setTravelAllowance(Integer)

new modeOfTransport(ModeOfTransportPreConstructor)

This solution is even better. We only have a valid ModeOfTransport object. However, the ModeOfTransport constructor can be called with a half-filled pre-constructor. So the pre-constructor should be a builder and should have a method that returns the ModeOfTransport object. This method will only return an object if the builder has been correctly filled, otherwise it returns null:

modeOfTransportBuilder.setMetroLine(MetroLine)

modeOfTransportBuilder.setCarTravel()
modeOfTransportBuilder.setBusLine(BusLine)
modeOfTransportBuilder.setTrain(Train)
modeOfTransportBuilder.setTravelAllowance(Integer)

modeOfTransport := modeOfTransportBuilder.getResult()

So the solution is to use a builder class. Let's see the structure of the code on the UML class diagram:

Builder Structure
  • Builder: Abstract interface for creating objects (product).
  • Concrete Builder: Provides implementation for Builder. It is an object able to construct other objects. Constructs and assembles parts to build the objects.

The builder pattern can be used for objects that contain flat data (html code, SQL query, X.509 certificate...), that is to say data that can't be easily edited. This type of data can not be edited step by step and must be edited at once. The best way to construct such an object is to use a builder class.

Examples

In Java, the StringBuffer and StringBuilder classes follow the builder design pattern. They are used to build String objects.

Cost

You can easily decide to use it. At worst, the pattern is useless.

Creation

Starting from a plain old class with a public constructor, implementing the design pattern is not very expensive. You can create the builder class aside your code. Then you will have to remove the existing constructor calls to use the builder instead. The refactoring is hardly automatic.

Maintenance

This design pattern has only small drawbacks. It may lead to small redundancies between the class and the builder structures but the both class have usually different structures. However, it is expensive to evolve to an abstract factory.

Removal

The refactoring is hardly automatic. It should be done manually.

Advices

  • Put the builder term in the name of the builder class to indicate the use of the pattern to the other developers.
  • Build your builder class as a fluent interface. It would increase the pattern interest.
  • If the target class contains flat data, your builder class can be constructed as a Composite that implements the Interpreter pattern.

Implementations

Implementation in Java

Building a car.

  1. /**
    
  2.  * Can have GPS, trip computer and a various number of seaters. Can be a city car, a sport car or a cabriolet.
    
  3.  */
    
  4. public class Car {
    
  5.   /**
    
  6.    * The description of the car.
    
  7.    */
    
  8.   private String description = null;
    
  9.  
    
  10.   /**
    
  11.    * Construct and return the car.
    
  12.    * @param aDescription The description of the car.
    
  13.    */
    
  14.   public Car(String aDescription) {
    
  15.       description = aDescription;
    
  16.     }
    
  17.  
    
  18.   /**
    
  19.    * Print the car.
    
  20.    * @return A flat representation.
    
  21.    */
    
  22.   public String toString() {
    
  23.       return description;
    
  24.     }
    
  25. }
    
  1. /**
    
  2.  *
    
  3.  */
    
  4. public class CarBuilder {
    
  5.   /**
    
  6.    * Sport car.
    
  7.    */
    
  8.   private static final int SPORT_CAR = 1;
    
  9.  
    
  10.   /**
    
  11.    * City car.
    
  12.    */
    
  13.   private static final int CITY_CAR = 2;
    
  14.  
    
  15.   /**
    
  16.    * Cabriolet.
    
  17.    */
    
  18.   private static final int CABRIOLET = 3;
    
  19.  
    
  20.   /**
    
  21.    * The type of the car.
    
  22.    */
    
  23.   private int carType;
    
  24.  
    
  25.   /**
    
  26.    * True if the car has a trip computer.
    
  27.    */
    
  28.   private boolean hasTripComputer;
    
  29.  
    
  30.   /**
    
  31.    * True if the car has a GPS.
    
  32.    */
    
  33.   private boolean hasGPS;
    
  34.  
    
  35.   /**
    
  36.    * The number of seaters the car may have.
    
  37.    */
    
  38.   private int seaterNumber;
    
  39.  
    
  40.   /**
    
  41.    * Construct and return the car.
    
  42.    * @return a Car with the right options.
    
  43.    */
    
  44.   public Car getResult() {
    
  45.       return new Car((carType == CITY_CAR) ? "A city car" : ((carType == SPORT_CAR) ? "A sport car" : "A cabriolet")
    
  46.         + " with " + seaterNumber + " seaters"
    
  47.         + (hasTripComputer ? " with a trip computer" : "")
    
  48.         + (hasGPS ? " with a GPS" : "")
    
  49.         + ".");
    
  50.     }
    
  51.  
    
  52.   /**
    
  53.    * Tell the builder the number of seaters.
    
  54.    * @param number the number of seaters the car may have.
    
  55.    */
    
  56.   public void setSeaters(int number) {
    
  57.       seaterNumber = number;
    
  58.     }
    
  59.  
    
  60.   /**
    
  61.    * Make the builder remember that the car is a city car.
    
  62.    */
    
  63.   public void setCityCar() {
    
  64.       carType = CITY_CAR;
    
  65.     }
    
  66.  
    
  67.   /**
    
  68.    * Make the builder remember that the car is a cabriolet.
    
  69.    */
    
  70.   public void setCabriolet() {
    
  71.       carType = CABRIOLET;
    
  72.     }
    
  73.  
    
  74.   /**
    
  75.    * Make the builder remember that the car is a sport car.
    
  76.    */
    
  77.   public void setSportCar() {
    
  78.       carType = SPORT_CAR;
    
  79.     }
    
  80.  
    
  81.   /**
    
  82.    * Make the builder remember that the car has a trip computer.
    
  83.    */
    
  84.   public void setTripComputer() {
    
  85.       hasTripComputer = true;
    
  86.     }
    
  87.  
    
  88.   /**
    
  89.    * Make the builder remember that the car has not a trip computer.
    
  90.    */
    
  91.   public void unsetTripComputer() {
    
  92.       hasTripComputer = false;
    
  93.     }
    
  94.  
    
  95.   /**
    
  96.    * Make the builder remember that the car has a global positioning system.
    
  97.    */
    
  98.   public void setGPS() {
    
  99.       hasGPS = true;
    
  100.     }
    
  101.  
    
  102.   /**
    
  103.    * Make the builder remember that the car has not a global positioning system.
    
  104.    */
    
  105.   public void unsetGPS() {
    
  106.       hasGPS = false;
    
  107.     }
    
  108. }
    
  1. /**
    
  2.  * Construct a CarBuilder called carBuilder and build a car.
    
  3.  */
    
  4. public class Director {
    
  5.   public static void main(String[] args) {
    
  6.     CarBuilder carBuilder = new CarBuilder();
    
  7.     carBuilder.setSeaters(2);
    
  8.     carBuilder.setSportCar();
    
  9.     carBuilder.setTripComputer();
    
  10.     carBuilder.unsetGPS();
    
  11.     Car car = carBuilder.getResult();
    
  12.  
    
  13.     System.out.println(car);
    
  14.   }
    
  15. }
    

It will produce:

A sport car with 2 seaters with a trip computer.


Building a pizza.

  1.  /** "Product" */
    
  2. class Pizza {
    
  3.  private String dough = "";
    
  4.  private String sauce = "";
    
  5.  private String topping = "";
    
  6.  
    
  7.  public void setDough(final String dough) {
    
  8.   this.dough = dough;
    
  9.  }
    
  10.  
    
  11.  public void setSauce(final String sauce) {
    
  12.   this.sauce = sauce;
    
  13.  }
    
  14.  
    
  15.  public void setTopping(final String topping) {
    
  16.   this.topping = topping;
    
  17.  }
    
  18. }
    
  1.  /** "Abstract Builder" */
    
  2. abstract class PizzaBuilder {
    
  3.  protected Pizza pizza;
    
  4.  
    
  5.  public abstract void buildDough();
    
  6.  public abstract void buildSauce();
    
  7.  public abstract void buildTopping();
    
  8.  
    
  9.  public void createNewPizzaProduct() {
    
  10.   this.pizza = new Pizza();
    
  11.  }
    
  12.  
    
  13.  public Pizza getPizza() {
    
  14.   return this.pizza;
    
  15.  }
    
  16. }
    
  1.  /** "ConcreteBuilder" */
    
  2. class HawaiianPizzaBuilder extends PizzaBuilder {
    
  3.  @Override public void buildDough() {
    
  4.   this.pizza.setDough("cross");
    
  5.  }
    
  6.  
    
  7.  @Override public void buildSauce() {
    
  8.   this.pizza.setSauce("mild");
    
  9.  }
    
  10.  
    
  11.  @Override public void buildTopping() {
    
  12.   this.pizza.setTopping("ham+pineapple");
    
  13.  }
    
  14. }
    
  1.  /** "ConcreteBuilder" */
    
  2. class SpicyPizzaBuilder extends PizzaBuilder {
    
  3.  @Override public void buildDough() {
    
  4.   this.pizza.setDough("pan baked");
    
  5.  }
    
  6.  
    
  7.  @Override public void buildSauce() {
    
  8.   this.pizza.setSauce("hot");
    
  9.  }
    
  10.  
    
  11.  @Override public void buildTopping() {
    
  12.   this.pizza.setTopping("pepperoni+salami");
    
  13.  }
    
  14. }
    
  1.  /** "Director" */
    
  2. class Waiter {
    
  3.  private PizzaBuilder pizzaBuilder;
    
  4.  
    
  5.  public void setPizzaBuilder(final PizzaBuilder pb) {
    
  6.   this.pizzaBuilder = pb;
    
  7.  }
    
  8.  
    
  9.  public Pizza getPizza() {
    
  10.   return this.pizzaBuilder.getPizza();
    
  11.  }
    
  12.  
    
  13.  public void constructPizza() {
    
  14.   this.pizzaBuilder.createNewPizzaProduct();
    
  15.   this.pizzaBuilder.buildDough();
    
  16.   this.pizzaBuilder.buildSauce();
    
  17.   this.pizzaBuilder.buildTopping();
    
  18.  }
    
  19. }
    
  1.  /** A customer ordering a pizza. */
    
  2. class BuilderExample {
    
  3.  
    
  4.  public static void main(final String[] args) {
    
  5.  
    
  6.   final Waiter waiter = new Waiter();
    
  7.  
    
  8.   final PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder();
    
  9.   final PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();
    
  10.  
    
  11.   waiter.setPizzaBuilder(hawaiianPizzaBuilder);
    
  12.   waiter.constructPizza();
    
  13.  
    
  14.   final Pizza pizza = waiter.getPizza();
    
  15.  
    
  16.   waiter.setPizzaBuilder(spicyPizzaBuilder);
    
  17.   waiter.constructPizza();
    
  18.  
    
  19.   final Pizza anotherPizza = waiter.getPizza();
    
  20.  }
    
  21. }
    
Implementation in C#
using System;
 
namespace BuilderPattern
{
  // Builder - abstract interface for creating objects (the product, in this case)
  abstract class PizzaBuilder
  {
    protected Pizza pizza;
 
    public Pizza GetPizza()
    {
      return pizza;
    }
 
    public void CreateNewPizzaProduct()
    {
      pizza = new Pizza();
    }
 
    public abstract void BuildDough();
    public abstract void BuildSauce();
    public abstract void BuildTopping();
  }
  // Concrete Builder - provides implementation for Builder; an object able to construct other objects.
  // Constructs and assembles parts to build the objects
  class HawaiianPizzaBuilder : PizzaBuilder
  {
    public override void BuildDough()
    {
      pizza.dough = "cross";
    }
 
    public override void BuildSauce()
    {
      pizza.sauce = "mild";
    }
 
    public override void BuildTopping()
    {
      pizza.topping = "ham+pineapple";
    }
  }
  // Concrete Builder - provides implementation for Builder; an object able to construct other objects.
  // Constructs and assembles parts to build the objects
  class SpicyPizzaBuilder : PizzaBuilder
  {
    public override void BuildDough()
    {
      pizza.dough = "pan baked";
    }
 
    public override void BuildSauce()
    {
      pizza.sauce = "hot";
    }
 
    public override void BuildTopping()
    {
      pizza.topping = "pepperoni + salami";
    }
  }
 
  // Director - responsible for managing the correct sequence of object creation.
  // Receives a Concrete Builder as a parameter and executes the necessary operations on it.
  class Cook
  {
    private PizzaBuilder _pizzaBuilder;
 
    public void SetPizzaBuilder(PizzaBuilder pb)
    {
      _pizzaBuilder = pb;
    }
 
    public Pizza GetPizza()
    {
      return _pizzaBuilder.GetPizza();
    }
 
    public void ConstructPizza()
    {
      _pizzaBuilder.CreateNewPizzaProduct();
      _pizzaBuilder.BuildDough();
      _pizzaBuilder.BuildSauce();
      _pizzaBuilder.BuildTopping();
    }
  }
 
  // Product - The final object that will be created by the Director using Builder
  public class Pizza
  {
    public string dough = string.Empty;
    public string sauce = string.Empty;
    public string topping = string.Empty;
  }
 
  class Program
  {
    static void Main(string[] args)
    {
      PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder();
      Cook cook = new Cook();
      cook.SetPizzaBuilder(hawaiianPizzaBuilder);
      cook.ConstructPizza();
      // create the product
      Pizza hawaiian = cook.GetPizza();
 
      PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();
      cook.SetPizzaBuilder(spicyPizzaBuilder);
      cook.ConstructPizza();
      // create another product
      Pizza spicy = cook.GetPizza();
    }
  }
 
}
Implementation in C++
#include <string>
#include <iostream>
using namespace std;
 
// "Product"
class Pizza {
public:
        void dough(const string& dough) {
                dough_ = dough;
        }
 
        void sauce(const string& sauce) {
                sauce_ = sauce;
        }
 
        void topping(const string& topping) {
                topping_ = topping;
        }
 
        void open() const {
                cout << "Pizza with " << dough_ << " dough, " << sauce_ << " sauce and "
                        << topping_ << " topping. Mmm." << endl;
        }
 
private:
        string dough_;
        string sauce_;
        string topping_;
};
 
// "Abstract Builder"
class PizzaBuilder {
public:
       // Chinmay Mandal : This default constructor may not be required here
        PizzaBuilder()
        {
           // Chinmay Mandal : Wrong syntax
           // pizza_ = new Pizza;
        }
        const Pizza& pizza() {
                return pizza_;
        }
 
        virtual void buildDough() = 0;
        virtual void buildSauce() = 0;
        virtual void buildTopping() = 0;
 
protected:
        Pizza pizza_;
};
 
class HawaiianPizzaBuilder : public PizzaBuilder {
public:
        void buildDough() {
                pizza_.dough("cross");
        }
 
        void buildSauce() {
                pizza_.sauce("mild");
        }
 
        void buildTopping() {
                pizza_.topping("ham+pineapple");
        }
};
 
class SpicyPizzaBuilder : public PizzaBuilder {
public:
        void buildDough() {
                pizza_.dough("pan baked");
        }
 
        void buildSauce() {
                pizza_.sauce("hot");
        }
 
        void buildTopping() {
                pizza_.topping("pepperoni+salami");
        }
};
 
class Cook {
public:
        Cook()
                : pizzaBuilder_(NULL/*nullptr*/)//Chinmay Mandal : nullptr replaced with NULL.
        {       }
 
        ~Cook() {
                if (pizzaBuilder_)
                        delete pizzaBuilder_;
        }
 
        void pizzaBuilder(PizzaBuilder* pizzaBuilder) {
                if (pizzaBuilder_)
                        delete pizzaBuilder_;
 
                pizzaBuilder_ = pizzaBuilder;
        }
 
        const Pizza& getPizza() {
                return pizzaBuilder_->pizza();
        }
 
        void constructPizza() {
                pizzaBuilder_->buildDough();
                pizzaBuilder_->buildSauce();
                pizzaBuilder_->buildTopping();
        }
 
private:
        PizzaBuilder* pizzaBuilder_;
};
 
int main() {
        Cook cook;
        cook.pizzaBuilder(new HawaiianPizzaBuilder);
        cook.constructPizza();
 
        Pizza hawaiian = cook.getPizza();
        hawaiian.open();
 
        cook.pizzaBuilder(new SpicyPizzaBuilder);
        cook.constructPizza();
 
        Pizza spicy = cook.getPizza();
        spicy.open();
}
Implementation in PHP
<?php
/** "Product" */
class Pizza {
 
    protected $dough;
    protected $sauce;
    protected $topping;
 
    public function setDough($dough) {
        $this->dough = $dough;
    }
 
    public function setSauce($sauce) {
        $this->sauce = $sauce;
    }
 
    public function setTopping($topping) {
        $this->topping = $topping;
    }
 
    public function showIngredients() {
        echo "Dough   : ".$this->dough."<br/>";
        echo "Sauce   : ".$this->sauce."<br/>";
        echo "Topping : ".$this->topping."<br/>";
    }
}
 
/** "Abstract Builder" */
abstract class PizzaBuilder {
 
    protected $pizza;
 
    public function getPizza() {
        return $this->pizza;
    }
 
    public function createNewPizzaProduct() {
        $this->pizza = new Pizza();
    }
 
    public abstract function buildDough();
    public abstract function buildSauce();
    public abstract function buildTopping();
}
 
/** "ConcreteBuilder" */
class HawaiianPizzaBuilder extends PizzaBuilder {
    public function buildDough() {
        $this->pizza->setDough("cross");
    }
 
    public function buildSauce() {
        $this->pizza->setSauce("mild");
    }
 
    public function buildTopping() {
        $this->pizza->setTopping("ham + pineapple");
    }
}
 
/** "ConcreteBuilder" */
class SpicyPizzaBuilder extends PizzaBuilder {
 
    public function buildDough() {
        $this->pizza->setDough("pan baked");
    }
 
    public function buildSauce() {
        $this->pizza->setSauce("hot");
    }
 
    public function buildTopping() {
        $this->pizza->setTopping("pepperoni + salami");
    }
}
 
/** "Director" */
class Waiter {
 
    protected $pizzaBuilder;
 
    public function setPizzaBuilder(PizzaBuilder $pizzaBuilder) {
        $this->pizzaBuilder = $pizzaBuilder;
    }
 
    public function getPizza() {
        return $this->pizzaBuilder->getPizza();
    }
 
    public function constructPizza() {
        $this->pizzaBuilder->createNewPizzaProduct();
        $this->pizzaBuilder->buildDough();
        $this->pizzaBuilder->buildSauce();
        $this->pizzaBuilder->buildTopping();
    }
}
 
class Tester {
 
    public static function main() {
 
        $oWaiter               = new Waiter();
        $oHawaiianPizzaBuilder = new HawaiianPizzaBuilder();
        $oSpicyPizzaBuilder    = new SpicyPizzaBuilder();
 
        $oWaiter->setPizzaBuilder($oHawaiianPizzaBuilder);
        $oWaiter->constructPizza();
 
        $pizza = $oWaiter->getPizza();
        $pizza->showIngredients();
 
        echo "<br/>";
 
        $oWaiter->setPizzaBuilder($oSpicyPizzaBuilder);
        $oWaiter->constructPizza();
 
        $pizza = $oWaiter->getPizza();
        $pizza->showIngredients();
    }
}
 
Tester::main();

output :

   Dough : cross
   Sauce : mild
   Topping : ham + pineapple
   Dough : pan baked
   Sauce : hot
   Topping : pepperoni + salami
Implementation in Ruby
# Product
class Pizza
  attr_accessor :dough, :sauce, :topping
end
 
# Abstract Builder
class PizzaBuilder
  def get_pizza
    @pizza
  end
 
  def create_new_pizza_product
    @pizza = Pizza.new
  end
 
  def build_dough; end
  def build_sauce; end
  def build_topping; end
 
end
 
# ConcreteBuilder
class HawaiianPizzaBuilder < PizzaBuilder
  def build_dough
    @pizza.dough = 'cross'
  end
 
  def build_sauce
    @pizza.sauce = 'mild'
  end
 
  def build_topping
    @pizza.topping = 'ham+pineapple'
  end
end
 
# ConcreteBuilder
class SpicyPizzaBuilder < PizzaBuilder
  def build_dough
    @pizza.dough = 'pan baked'
  end
 
  def build_sauce
    @pizza.sauce = 'hot'
  end
 
  def build_topping
    @pizza.topping = 'pepperoni+salami'
  end
end
 
# Director
class Waiter
  attr_accessor :pizza_builder
 
  def get_pizza
    pizza_builder.get_pizza
  end
 
  def construct_pizza
    pizza_builder.create_new_pizza_product
    pizza_builder.build_dough
    pizza_builder.build_sauce
    pizza_builder.build_topping
  end
end
 
# A customer ordering a pizza.
class BuilderExample
  def main(args = [])
    waiter = Waiter.new
    hawaiian_pizza_builder = HawaiianPizzaBuilder.new
    spicy_pizza_builder = SpicyPizzaBuilder.new
 
    waiter.pizza_builder = hawaiian_pizza_builder
    waiter.construct_pizza
 
    pizza = waiter.get_pizza
  end
end
 
puts BuilderExample.new.main.inspect
Implementation in Python
#coding: utf-8
class Animal:
    """
    Abstract Animal
    """
    legs = 0
    tail = False
    roar = ''
 
class Mutator:
    """
    Mutator
    """
    def mutate(self):
        self.animal = Animal()
 
class Cat(Mutator):
    """
    Cat
    """
    def create_legs(self):
        self.animal.legs = 4
 
    def create_tail(self):
        self.animal.tail = True
 
    def create_roar(self):
        self.animal.roar = 'meowww!'
 
class Dog(Mutator):
    """
    Dog
    """
    def create_legs(self):
        self.animal.legs = 4
 
    def create_tail(self):
        self.animal.tail = True
 
    def create_roar(self):
        self.animal.roar = 'woffff!'
 
class AnimalOwner:
    """
    Farm owner
    """
    __mutator = ''
    def set_animal(self, mutator):
        self.__mutator = mutator
 
    def create_an_animal_for_me(self):
        self.__mutator.mutate()
        self.__mutator.create_legs()
        self.__mutator.create_tail()
        self.__mutator.create_roar()
 
    def get_animal(self):
        return self.__mutator.animal
 
c = Cat()
d = Dog()
ao = AnimalOwner()
ao.set_animal(c)
ao.create_an_animal_for_me()
animal = ao.get_animal()
print animal.roar #meow!

Print version

Various examples of the Chain of responsibility pattern.

Implementation in Java

The following Java code illustrates the pattern with the example of a logging class. Each logging handler decides if any action is to be taken at this log level and then passes the message on to the next logging handler. Note that this example should not be seen as a recommendation on how to write logging classes.

Also, note that in a 'pure' implementation of the chain of responsibility pattern, a logger would not pass responsibility further down the chain after handling a message. In this example, a message will be passed down the chain whether it is handled or not.

abstract class Logger {
 
    public static int ERR = 3;
    public static int NOTICE = 5;
    public static int DEBUG = 7;
    protected int mask;
 
    // The next element in the chain of responsibility
    protected Logger next;
 
    public Logger setNext(Logger l) {
        next = l;
        return l;
    }
 
    public void message(String msg, int priority) {
        if (priority <= mask) {
            writeMessage(msg);
        }
        if (next != null) {
            next.message(msg, priority);
        }
    }
 
    abstract protected void writeMessage(String msg);
 
}
class StdoutLogger extends Logger {
 
    public StdoutLogger(int mask) {
        this.mask = mask;
    }
 
    protected void writeMessage(String msg) {
        System.out.println("Writing to stdout: " + msg);
    }
 
}
class EmailLogger extends Logger {
 
    public EmailLogger(int mask) {
        this.mask = mask;
    }
 
    protected void writeMessage(String msg) {
        System.out.println("Sending via email: " + msg);
    }
 
}
class StderrLogger extends Logger {
 
    public StderrLogger(int mask) {
        this.mask = mask;
    }
 
    protected void writeMessage(String msg) {
        System.err.println("Sending to stderr: " + msg);
    }
 
}
public class ChainOfResponsibilityExample {
 
    public static void main(String[] args) {
        // Build the chain of responsibility
        Logger l,l1;
        l1 = l = new StdoutLogger(Logger.DEBUG);
        l1 = l1.setNext(new EmailLogger(Logger.NOTICE));
        l1 = l1.setNext(new StderrLogger(Logger.ERR));
 
        // Handled by StdoutLogger
        l.message("Entering function y.", Logger.DEBUG);
 
        // Handled by StdoutLogger and EmailLogger
        l.message("Step1 completed.", Logger.NOTICE);
 
        // Handled by all three loggers
        l.message("An error has occurred.", Logger.ERR);
    }
 
}

The output is:

  Writing to stdout:   Entering function y.
  Writing to stdout:   Step1 completed.
  Sending via e-mail:  Step1 completed.
  Writing to stdout:   An error has occurred.
  Sending via e-mail:  An error has occurred.
  Writing to stderr:   An error has occurred.

Below is another example of this pattern in Java. In this example we have different roles, each having a fix purchase power limit and a successor. Every time a user in a role receives a purchase request, when it's over his limit, he just passes that request to his successor.

The PurchasePower abstract class with the abstract method processRequest.

abstract class PurchasePower {
 
    protected final double base = 500;
    protected PurchasePower successor;
 
    public void setSuccessor(PurchasePower successor) {
        this.successor = successor;
    }
 
    abstract public void processRequest(PurchaseRequest request);
 
}

Four implementations of the abstract class above: Manager, Director, Vice President, President

class ManagerPPower extends PurchasePower {
 
    private final double ALLOWABLE = 10 * base;
 
    public void processRequest(PurchaseRequest request) {
        if(request.getAmount() < ALLOWABLE) {
            System.out.println("Manager will approve $"+ request.getAmount());
        }
        else if(successor != null) {
            successor.processRequest(request);
        }
    }
 
}
class DirectorPPower extends PurchasePower {
 
    private final double ALLOWABLE = 20 * base;
 
    public void processRequest(PurchaseRequest request) {
        if(request.getAmount() < ALLOWABLE) {
            System.out.println("Director will approve $"+ request.getAmount());
        }
        else if(successor != null) {
            successor.processRequest(request);
        }
    }
 
}
class VicePresidentPPower extends PurchasePower {
 
    private final double ALLOWABLE = 40 * base;
 
    public void processRequest(PurchaseRequest request) {
        if(request.getAmount() < ALLOWABLE) {
            System.out.println("Vice President will approve $" + request.getAmount());
        }
        else if(successor != null) {
            successor.processRequest(request);
        }
    }
}
class PresidentPPower extends PurchasePower {
 
    private final double ALLOWABLE = 60 * base;
 
    public void processRequest(PurchaseRequest request) {
        if(request.getAmount() < ALLOWABLE) {
            System.out.println("President will approve $" + request.getAmount());
        }
        else {
            System.out.println( "Your request for $" + request.getAmount() + " needs a board meeting!");
        }
    }
 
}

The PurchaseRequest class with its Getter methods which keeps the request data in this example.

class PurchaseRequest {
 
    private int number;
    private double amount;
    private String purpose;
 
    public PurchaseRequest(int number, double amount, String purpose) {
        this.number = number;
        this.amount = amount;
        this.purpose = purpose;
    }
 
    public double getAmount() {
        return amount;
    }
    public void setAmount(double amt)  {
        amount = amt;
    }
 
    public String getPurpose() {
        return purpose;
    }
    public void setPurpose(String reason) {
        purpose = reason;
    }
 
    public int getNumber(){
        return number;
    }
    public void setNumber(int num) {
        number = num;
    }  
 
}

And here a usage example, the successors are set like this: Manager -> Director -> Vice President -> President

class CheckAuthority {
 
    public static void main(String[] args) {
        ManagerPPower manager = new ManagerPPower();
        DirectorPPower director = new DirectorPPower();
        VicePresidentPPower vp = new VicePresidentPPower();
        PresidentPPower president = new PresidentPPower();
        manager.setSuccessor(director);
        director.setSuccessor(vp);
        vp.setSuccessor(president);
 
        //enter ctrl+c to kill.
        try{
            while (true) {
                System.out.println("Enter the amount to check who should approve your expenditure.");
                System.out.print(">");
                double d = Double.parseDouble(new BufferedReader(new InputStreamReader(System.in)).readLine());
                manager.processRequest(new PurchaseRequest(0, d, "General"));
           }
        }
        catch(Exception e){
            System.exit(1);
        }  
    }
 
}
Implementation in Python
class Car:
    def __init__(self):
        self.name = None
        self.km = 11100
        self.fuel = 5
        self.oil = 5
 
def handle_fuel(car):
    if car.fuel < 10:
        print "added fuel"
        car.fuel = 100
 
def handle_km(car):
    if car.km > 10000:
        print "made a car test."
        car.km = 0
 
def handle_oil(car):
    if car.oil < 10:
        print "Added oil"
        car.oil = 100
 
class Garage:
    def __init__(self):
        self.handlers = []
 
    def add_handler(self, handler):
        self.handlers.append(handler)
 
    def handle_car(self, car):
        for handler in self.handlers:
            handler(car)
 
if __name__ == '__main__':
    handlers = [handle_fuel, handle_km, handle_oil]
    garage = Garage()
 
    for handle in handlers:
        garage.add_handler(handle)
    garage.handle_car(Car())
Implementation in Racket
#lang racket
 
; Define an automobile structure
(struct auto (fuel km oil) #:mutable #:transparent)
 
; Create an instance of an automobile
(define the-auto (auto 5 1500 7))
 
; Define handlers
 
(define (handle-fuel auto)
  (when (< (auto-fuel auto) 10)
    (begin (set-auto-fuel! auto 10)
           (printf "set fuel\n"))))
 
(define (handle-km auto)
  (when (> (auto-km auto) 10000)
    (begin (set-auto-km! auto 0)
           (printf "made a car test\n"))))
 
(define (handle-oil auto)
  (when (< (auto-oil auto) 10)
    (begin (set-auto-oil! auto (+ (auto-oil auto) 5))
           (printf "added oil\n"))))
 
; Apply each handler to the auto
(define (handle-auto handlers auto)
  (unless (null? handlers)
    (begin ((car handlers) auto)
           (handle-auto (cdr handlers) auto))))
 
(display the-auto)
(newline)
 
; Handle the auto
(handle-auto (list handle-fuel handle-km handle-oil) the-auto)
 
(display the-auto)
(newline)
Implementation in Perl

Java example about purchase power of various roles, using perl 5.10 and Moose.

use feature ':5.10';
 
package PurchasePower;
use Moose;
 
has successor => ( is => "rw", isa => "PurchasePower" );
 
sub processRequest {
    my ( $self, $req ) = @_;
    if ( $req->amount < $self->allowable ) {
        printf "%s will approve \$%.2f\n", $self->title, $req->amount;
    }
    elsif ( $self->successor ) {
        $self->successor->processRequest($req);
    }
    else {
        $self->no_match( $req );
    }
}
 
package ManagerPPower;
use Moose; extends "PurchasePower";
sub allowable {5000}
sub title     {"manager"}
 
package DirectorPPower;
use Moose; extends "PurchasePower";
sub allowable {10000}
sub title     {"director"}
 
package VicePresidentPPower;
use Moose; extends "PurchasePower";
sub allowable {20000}
sub title     {"vice-president"}
 
package PresidentPPower;
use Moose; extends "PurchasePower";
sub allowable {30000}
sub title     {"president"}
 
sub no_match {
    my ( $self, $req ) = @_;
    printf "your request for \$%.2f will need a board meeting\n", $req->amount;
}
 
package PurchaseRequest;
use Moose;
 
has number  => ( is => "rw", isa => "Int" );
has amount  => ( is => "rw", isa => "Num" );
has purpose => ( is => "rw", isa => "Str" );
 
package main;
 
my $manager   = new ManagerPPower;
my $director  = new DirectorPPower;
my $vp        = new VicePresidentPPower;
my $president = new PresidentPPower;
 
$manager->successor($director);
$director->successor($vp);
$vp->successor($president);
 
print "Enter the amount to check who should approve your expenditure.\n>";
my $amount = readline;
$manager->processRequest(
    PurchaseRequest->new(
        number  => 0,
        amount  => $amount,
        purpose => "General"
    )
);

Perl example using perl 5.10 and Moose.

use feature ':5.10';
 
package Car;
use Moose;
 
has name => ( is => "rw", default => undef );
has km   => ( is => "rw", default => 11100 );
has fuel => ( is => "rw", default => 5 );
has oil  => ( is => "rw", default => 5 );
 
sub handle_fuel {
    my $self = shift;
    if ( $self->fuel < 10 ) {
        say "added fuel";
        $self->fuel(100);
    }
}
 
sub handle_km {
    my $self = shift;
    if ( $self->km > 10000 ) {
        say "made a car test";
        $self->km(0);
    }
}
 
sub handle_oil {
    my $self = shift;
    if ( $self->oil < 10 ) {
        say "added oil";
        $self->oil(100);
    }
}
 
package Garage;
use Moose;
 
has handler => (
    is      => "ro",
    isa     => "ArrayRef[Str]",
    traits  => ['Array'],
    handles => { add_handler => "push", handlers => "elements" },
    default => sub { [] }
);
 
sub handle_car {
    my ($self, $car) = @_;
    $car->$_ for $self->handlers
}
 
package main;
 
my @handlers = qw( handle_fuel handle_km handle_oil );
my $garage   = Garage->new;
$garage->add_handler($_) for @handlers;
$garage->handle_car( Car->new );
Implementation in C#
using System;
 
  class MainApp
  {
    static void Main()
    {
      // Setup Chain of Responsibility
      Handler h1 = new ConcreteHandler1();
      Handler h2 = new ConcreteHandler2();
      Handler h3 = new ConcreteHandler3();
      h1.SetSuccessor(h2);
      h2.SetSuccessor(h3);
 
      // Generate and process request
      int[] requests = {2, 5, 14, 22, 18, 3, 27, 20};
 
      foreach (int request in requests)
      {
        h1.HandleRequest(request);
      }
 
      // Wait for user
      Console.Read();
    }
  }
 
  // "Handler"
  abstract class Handler
  {
    protected Handler successor;
 
    public void SetSuccessor(Handler successor)
    {
      this.successor = successor;
    }
 
    public abstract void HandleRequest(int request);
  }
 
  // "ConcreteHandler1"
  class ConcreteHandler1 : Handler
  {
    public override void HandleRequest(int request)
    {
      if (request >= 0 && request < 10)
      {
        Console.WriteLine("{0} handled request {1}",
          this.GetType().Name, request);
      }
      else if (successor != null)
      {
        successor.HandleRequest(request);
      }
    }
  }
 
  // "ConcreteHandler2"
  class ConcreteHandler2 : Handler
  {
    public override void HandleRequest(int request)
    {
      if (request >= 10 && request < 20)
      {
        Console.WriteLine("{0} handled request {1}",
          this.GetType().Name, request);
      }
      else if (successor != null)
      {
        successor.HandleRequest(request);
      }
    }
  }
 
  // "ConcreteHandler3"
  class ConcreteHandler3 : Handler
  {
    public override void HandleRequest(int request)
    {
      if (request >= 20 && request < 30)
      {
        Console.WriteLine("{0} handled request {1}",
          this.GetType().Name, request);
      }
      else if (successor != null)
      {
        successor.HandleRequest(request);
      }
    }
  }

Command

The command pattern is an Object behavioural pattern that decouples sender and receiver. It can also be thought as an object oriented equivalent of call back method. Call Back: It is a function that is registered to be called at later point of time based on user actions.

Scope

Object

Purpose

Behavioral

Intent

Encapsulate the request for a service as an object.

Applicability

  • to parameterize objects with an action to perform
  • to specify, queue, and execute requests at different times
  • for a history of requests
  • for multilevel undo/redo

Structure

Command.jpg

Consequences

  • + abstracts executor of a service
  • + supports arbitrary-level undo-redo
  • + composition yields macro-commands
  • - might result in lots of trivial command subclasses

Examples

Libreoffice writer nice pic of ui.png

The best example for this pattern is the graphical user interface. On most multidata interfaces, you have both a "New" menu and a "New" button with an icon like in Libre Office Writer. The both controls are connected to a command object, so the action is performed by the same code.

Cost

This pattern is dealing with the whole architecture of your program. It can have important impacts on your project.

Creation

If the code already exists, this pattern is very expensive.

Maintenance

This pattern is very expensive to maintain.

Removal

This pattern is very expensive to remove too.

Advises

  • Use the Command term to indicate the use of the pattern to the other developers.

Implementations

Consider a "simple" switch. In this example we configure the Switch with two commands: to turn the light on and to turn the light off. A benefit of this particular implementation of the command pattern is that the switch can be used with any device, not just a light — the Switch in the following example turns a light on and off, but the Switch's constructor is able to accept any subclasses of Command for its two parameters. For example, you could configure the Switch to start an engine.

Implementation in Java
/* The Command interface */
public interface Command {
   void execute();
}
import java.util.List;
import java.util.ArrayList;
/* The Invoker class */
public class Switch {
   private List<Command> history = new ArrayList<Command>();
   public Switch() {
   }
   public void storeAndExecute(Command cmd) {
      this.history.add(cmd); // optional
      cmd.execute();        
   }
}
/* The Receiver class */
public class Light {
   public Light() {
   }
   public void turnOn() {
      System.out.println("The light is on");
   }
   public void turnOff() {
      System.out.println("The light is off");
   }
}
/* The Command for turning on the light - ConcreteCommand #1 */
public class FlipUpCommand implements Command {
   private Light theLight;
   public FlipUpCommand(Light light) {
      this.theLight = light;
   }
   public void execute(){
      theLight.turnOn();
   }
}
/* The Command for turning off the light - ConcreteCommand #2 */
public class FlipDownCommand implements Command {
   private Light theLight;
   public FlipDownCommand(Light light) {
      this.theLight = light;
   }
   public void execute() {
      theLight.turnOff();
   }
}
/* The test class or client */
public class PressSwitch {
   public static void main(String[] args){
      Light lamp = new Light();
      Command switchUp = new FlipUpCommand(lamp);
      Command switchDown = new FlipDownCommand(lamp);
      Switch s = new Switch();
      try {
         if (args[0].equalsIgnoreCase("ON")) {
            s.storeAndExecute(switchUp);
         }
         else if (args[0].equalsIgnoreCase("OFF")) {
            s.storeAndExecute(switchDown);
         }
         else {
            System.out.println("Argument \"ON\" or \"OFF\" is required.");
         }
      } catch (Exception e) {
         System.out.println("Arguments required.");
      }
   }
}

Operations

The implementations to do are:

  • copying a command before putting it on a history list
  • handling hysteresis
  • supporting transactions
  1.  public interface Command {
    
  2.    public int execute(int a, int b);
    
  3.  }
    
  1.  public class AddCommand implements Command {
    
  2.    public int execute(int a, int b) {
    
  3.       return a + b;
    
  4.    }
    
  5.  }
    
  1.  public class MultCommand implements Command {
    
  2.    public int execute(int a, int b) {
    
  3.       return a * b;
    
  4.    }
    
  5.  }
    
  1.  public class TestCommand {
    
  2.    public static void main(String a[]) {
    
  3.      Command add = new AddCommand();
    
  4.      add.execute(1, 2); // returns 3
    
  5.      Command multiply = new MultCommand();
    
  6.      multiply.execute(2, 3); // returns 6
    
  7.    }
    
  8.  }
    

In the above example, it can be noted that the command pattern decouples the object that invokes the operation from the ones having the knowledge to perform it.

Command in Java

Menus and buttons provide a classic example of the need for the Command pattern. When you add a menu to an application, you have to configure the menu with words that describe actions that the user can choose, such as Save and Open. Similarly for a button. You also have to configure the menu or button so that it can take action, calling a method in response to a user's click. However, JMenuItem or JButton class has no way of knowing what action to perform when an item or button is selected. In the following example we use the same Action for both a menu item and a button saving us from having to write the same code twice.

  1.  Action actionOpen = new AbstractAction("Open...", iconOpen) {
    
  2.       public void actionPerformed(ActionEvent e) {
    
  3.        ...    // open a file
    
  4.       }
    
  5.  }
    
  6.  
    
  7.  JMenu mFile = new JMenu("File");
    
  8.  JMenuItem item =  mFile.add(actionOpen);   // use the same action for both a menu item ...
    
  9.  
    
  10.  JToolBar m_toolBar = new JToolBar();
    
  11.  JButton bOpen = new JButton(actionOpen);   // ... and a button
    
  12.  m_toolBar.add(bOpen);
    

Java Swing applications commonly apply the Mediator pattern, registering a single object to receive all GUI events. This object mediates the interaction of the components and translates user input into commands for business domain objects.

Undo in Java

Swing provides for Undo/Redo functionality.

  1.  import javax.swing.undo.*;
    
  2.  
    
  3.  UndoManager undoManager = new UndoManager();
    
  4.  Action undoAction, redoAction;
    
  5.  undoAction = new AbstractAction("Undo",    
    
  6.      new ImageIcon("edit_undo.gif")) {
    
  7.       public void actionPerformed(ActionEvent e) {
    
  8.         try {
    
  9.           undoManager.undo(); // undoManager.redo();
    
  10.         }
    
  11.         catch (CannotUndoException ex) {
    
  12.           System.err.println("Unable to undo: " + ex);
    
  13.         }
    
  14.         updateUndo();
    
  15.       }
    
  16.     };
    
  17.  // same for Redo
    
  18.  
    
  19.  protected void updateUndo() {
    
  20.     if(undo.canUndo()) {
    
  21.       undoAction.setEnabled(true);
    
  22.       undoAction.putValue(Action.NAME, undo.getUndoPresentationName());
    
  23.     } else {
    
  24.       undoAction.setEnabled(false);
    
  25.       undoAction.putValue(Action.NAME, "Undo");
    
  26.     }
    
  27.     if(undo.canRedo()) {
    
  28.       redoAction.setEnabled(true);
    
  29.       redoAction.putValue(Action.NAME, undo.getRedoPresentationName());
    
  30.     } else {
    
  31.       redoAction.setEnabled(false);
    
  32.       redoAction.putValue(Action.NAME, "Redo");
    
  33.     }
    
  34.   }
    
Implementation in C#

The following code is an implementation of Command pattern in C#.

using System;
using System.Collections.Generic;
namespace CommandPattern
{
    public interface ICommand
    {
        void Execute();
    }
    /* The Invoker class */
    public class Switch
    {
        private List<ICommand> _commands = new List<ICommand>();
        public void StoreAndExecute(ICommand command)
        {
            _commands.Add(command);
            command.Execute();
        }
    }
    /* The Receiver class */
    public class Light
    {
        public void TurnOn()
        {
            Console.WriteLine("The light is on");
        }
        public void TurnOff()
        {
            Console.WriteLine("The light is off");
        }
    }
    /* The Command for turning on the light - ConcreteCommand #1 */
    public class FlipUpCommand : ICommand
    {
        private Light _light;
        public FlipUpCommand(Light light)
        {
            _light = light;
        }
        public void Execute()
        {
            _light.TurnOn();
        }
    }
    /* The Command for turning off the light - ConcreteCommand #2 */
    public class FlipDownCommand : ICommand
    {
        private Light _light;
        public FlipDownCommand(Light light)
        {
            _light = light;
        }
        public void Execute()
        {
            _light.TurnOff();
        }
    }
    /* The test class or client */
    internal class Program
    {
        public static void Main(string[] args)
        {
            Light lamp = new Light();
            ICommand switchUp = new FlipUpCommand(lamp);
            ICommand switchDown = new FlipDownCommand(lamp);
            Switch s = new Switch();
            string arg = args.Length > 0 ? args[0].ToUpper() : null;
            if (arg == "ON")
            {
                s.StoreAndExecute(switchUp);
            }
            else if (arg == "OFF")
            {
                s.StoreAndExecute(switchDown);
            }
            else
            {
                Console.WriteLine("Argument \"ON\" or \"OFF\" is required.");
            }
        }
    }
}
Implementation in Python

The following code is an implementation of Command pattern in Python.

class Switch(object):
    """The INVOKER class"""
    def __init__(self, flip_up_cmd, flip_down_cmd):
        self.flip_up = flip_up_cmd
        self.flip_down = flip_down_cmd
class Light(object):
    """The RECEIVER class"""
    def turn_on(self):
        print "The light is on"
    def turn_off(self):
        print "The light is off"
class LightSwitch(object):
    """The CLIENT class"""
    def __init__(self):
        lamp = Light()
        self._switch = Switch(lamp.turn_on, lamp.turn_off)
    def switch(self, cmd):
        cmd = cmd.strip().upper()
        if cmd == "ON":
            self._switch.flip_up()
        elif cmd == "OFF":
            self._switch.flip_down()
        else:
            print 'Argument "ON" or "OFF" is required.'
# Execute if this file is run as a script and not imported as a module
if __name__ == "__main__":
    light_switch = LightSwitch()
    print "Switch ON test."
    light_switch.switch("ON")
    print "Switch OFF test."
    light_switch.switch("OFF")
    print "Invalid Command test."
    light_switch.switch("****")
Implementation in Scala
/* The Command interface */
trait Command {
   def execute()
}
 
/* The Invoker class */
class Switch {
   private var history: List[Command] = Nil
 
   def storeAndExecute(cmd: Command) {
      cmd.execute()
      this.history :+= cmd
   }
}
 
/* The Receiver class */
class Light {
   def turnOn() = println("The light is on")
   def turnOff() = println("The light is off")
}
 
/* The Command for turning on the light - ConcreteCommand #1 */
class FlipUpCommand(theLight: Light) extends Command {
   def execute() = theLight.turnOn()
}
 
/* The Command for turning off the light - ConcreteCommand #2 */
class FlipDownCommand(theLight: Light) extends Command {
   def execute() = theLight.turnOff()
}
 
/* The test class or client */
object PressSwitch {
   def main(args: Array[String]) {
      val lamp = new Light()
      val switchUp = new FlipUpCommand(lamp)
      val switchDown = new FlipDownCommand(lamp)
 
      val s = new Switch()
 
      try {
         args(0).toUpperCase match {
            case "ON" => s.storeAndExecute(switchUp)
            case "OFF" => s.storeAndExecute(switchDown)
            case _ => println("Argument \"ON\" or \"OFF\" is required.")
         }
      } catch {
         case e: Exception => println("Arguments required.")
      }
   }
}
Implementation in JavaScript

The following code is an implementation of Command pattern in Javascript.

/* The Invoker function */
var Switch = function(){
    this.storeAndExecute = function(command){
        command.execute();
    }
}
/* The Receiver function */
var Light = function(){
    this.turnOn = function(){ console.log ('turn on')};
    this.turnOff = function(){ console.log ('turn off') };
}
/* The Command for turning on the light - ConcreteCommand #1 */
var FlipUpCommand = function(light){
    this.execute = light.turnOn;
}
/* The Command for turning off the light - ConcreteCommand #2 */
var FlipDownCommand = function(light){
    this.execute = light.turnOff;
}
var light = new Light();
var switchUp = new FlipUpCommand(light);
var switchDown = new FlipDownCommand(light);
var s = new Switch();
s.storeAndExecute(switchUp);
s.storeAndExecute(switchDown);
Implementation in Smalltalk
Object subclass: #Switch
  instanceVariableNames:
    ' flipUpCommand flipDownCommand '
  classVariableNames: ''
  poolDictionaries: ''
 
Object subclass: #Light
  instanceVariableNames: ''
  classVariableNames: ''
  poolDictionaries: ''
 
Object subclass: #PressSwitch
  instanceVariableNames: ''
  classVariableNames: ''
  poolDictionaries: ''
 
!Switch class methods !
upMessage: flipUpMessage downMessage: flipDownMessage
^self new upMessage: flipUpMessage downMessage: flipDownMessage; yourself.! !
!Switch methods !
upMessage: flipUpMessage downMessage: flipDownMessage
flipUpCommand := flipUpMessage.
flipDownCommand := flipDownMessage.!
flipDown
flipDownCommand perform.!
 
flipUp
flipUpCommand perform.! !
 
!Light methods !
turnOff
Transcript show: 'The light is off'; cr.!
 
turnOn
Transcript show: 'The light is on'; cr.! !
!PressSwitch class methods !
switch: state
" This is the test method "
| lamp switchUp switchDown switch |
lamp := Light new.
switchUp := Message receiver: lamp selector: #turnOn.
switchDown := Message receiver: lamp selector: #turnOff.
switch := Switch upMessage: switchUp downMessage: switchDown.
state = #on ifTrue: [ ^switch flipUp ].
state = #off ifTrue: [ ^switch flipDown ].
Transcript show: 'Argument #on or #off is required.'.
! !

Composite

The composite design pattern reduces the cost of an implementation that handles data represented as a tree. When an application does a process on a tree, usually the process has to handle the iteration on the components, the move on the tree and has to process the nodes and the leafs separately. All of this creates a big amount of code. Suppose that you have to handle a file system repertory. Each folders can contain files or folders. To handle this, you have an array of items that can be file or folder. The files have a name and the folders are arrays. Now you have to implement a file search operation on the whole folder tree. The pseudo-code should look like this:

method searchFilesInFolders(rootFolder, searchedFileName) is
    input: a list of the content of the rootFolder.
    input: the searchedFileName that should be found in the folders.
    output: the list of encountered files.

  Empty the foundFiles list
  Empty the parentFolders list
  Empty the parentIndices list
  currentFolder := rootFolder
  currentIndex := 0
  Add rootFolder to parentFolders

  while parentFolders is not empty do
    if currentIndex is out of currentFolder then
        currentFolder := last item of parentFolders
        Remove the last item of parentFolders
        currentIndex := last item of parentIndices + 1
        Remove the last item of parentIndices

    else if the item at the currentIndex of the currentFolder is a folder then
      currentFolder := the folder
      Add currentFolder to parentFolders
      Add currentIndex to parentIndices
      currentIndex := 0

    otherwise
      if the name of the file is equal to the searchedFileName then
        Add the file to foundFiles
      Increment currentIndex

  Return the foundFiles

In the previous code, each iteration of the same while loop is a process of one node or leaf. At the end of the process, the code move to the position of the next node or leaf to process. There are three branches in the loop. The first branch is true when we have processed all the children of the node and it moves to the parent, the second goes into a child node and the last process a leaf (i.e. a file). The memory of the location should be stored to go back in the tree. The problem in this implementation is that it is hardly readable and the process of the folders and the files is completely separate. This code is heavy to maintain and you have to think to the whole tree each moment. The folders and the files should be called the same way so they should be objects that implements the same interface.

Composite pattern in UML.
Component
  • is the abstraction for all components, including composite ones.
  • declares the interface for objects in the composition.
  • (optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate.
Leaf
  • represents leaf objects in the composition.
  • implements all Component methods.
Composite
  • represents a composite Component (component having children).
  • implements methods to manipulate children.
  • implements all Component methods, generally by delegating them to its children.

So now the implementation is rather like this:

interface FileSystemComponent is
  method searchFilesInFolders(searchedFileName) is
      input: the searchedFileName that should be found in the folders.
      output: the list of encountered files.
class File implementing FileSystemComponent is
  method searchFilesInFolders(searchedFileName) is
      input: the searchedFileName that should be found in the folders.
      output: the list of encountered files.

    if the name of the file is equal to the searchedFileName then
      Empty the foundFiles list
      Add the file to foundFiles
      Return the foundFiles

    otherwise
      Return an empty list
class Folder implementing FileSystemComponent is
  field children is
    The list of the direct children.

  method searchFilesInFolders(searchedFileName) is
      input: the searchedFileName that should be found in the folders.
      output: the list of encountered files.

    Empty the foundFiles list

    for each child in children
      Call searchFilesInFolders(searchedFileName) on the child
      Add the result to foundFiles

    Return the foundFiles

As you can see, a component can be an individual object and also can be a collection of objects. A Composite pattern can represent both the conditions. In this pattern, one can develop tree structures for representing part-whole hierarchies.

Examples

The best example of use of this pattern is the Graphical User Interface. The widgets of the interface are organized in a tree and the operations (resizing, repainting...) on all the widgets are processed using the composite design pattern.

Cost

This pattern is one of the least expensive patterns. You can implement it each time you have to handle a tree of data without worrying. There is no bad usage of this pattern. The cost of the pattern is only to handle the children of a composite but this cost would be required and more expensive without the design pattern.

Creation

You have to create an almost empty interface and implement the management of the composite children. This cost is very low.

Maintenance

You can't get caught in the system. The only relatively expensive situation occurs when you have to often change the operations applied to the whole data tree.

Removal

You should remove the pattern when you remove the data tree. So you just remove all in once. This cost is very low.

Advices

  • Put the composite and component terms in the name of the classes to indicate the use of the pattern to the other developers.

Implementations

Various examples of the composite pattern.

Implementation in C#
using System;
using System.Collections.Generic;
namespace Composite
{
    class Program
    {
        interface IGraphic
        {
            void Print();
        }
        class CompositeGraphic : IGraphic
        {
            private List<IGraphic> child = new List<IGraphic>();
            public CompositeGraphic(IEnumerable<IGraphic> collection)
            {
                child.AddRange(collection);
            }
            public void Print()
            {
                foreach(IGraphic g in child)
                {
                    g.Print();
                }
            }
        }
        class Ellipse : IGraphic
        {
            public void Print()
            {
                Console.WriteLine("Ellipse");
            }
        }
        static void Main(string[] args)
        {
            new CompositeGraphic(new IGraphic[]
                {
                    new CompositeGraphic(new IGraphic[]
                        {
                            new Ellipse(),
                            new Ellipse(),
                            new Ellipse()
                        }),
                    new CompositeGraphic(new IGraphic[]
                        {
                            new Ellipse()
                        })
                }).Print();
        }
    }
}
Implementation in Common Lisp

The following example, written in Common Lisp, and translated directly from the Java example below it, implements a method named print-graphic, which can be used on either an ellipse, or a list whose elements are either lists or ellipses.

(defstruct ellipse) ;; An empty struct.
;; For the method definitions, "object" is the variable,
;; and the following word is the type.
(defmethod print-graphic ((object null))
  NIL)
(defmethod print-graphic ((object cons))
  (print-graphic (first object))
  (print-graphic (rest object)))
(defmethod print-graphic ((object ellipse))
  (print 'ELLIPSE))
(let* ((ellipse-1 (make-ellipse))
       (ellipse-2 (make-ellipse))
       (ellipse-3 (make-ellipse))
       (ellipse-4 (make-ellipse)))
  (print-graphic (cons (list ellipse-1 (list ellipse-2 ellipse-3)) ellipse-4)))
Implementation in Java

The following example, written in Java, implements a graphic class, which can be either an ellipse or a composition of several graphics. Every graphic can be printed. In algebraic form,

       Graphic = ellipse | GraphicList
       GraphicList = empty | Graphic GraphicList

It could be extended to implement several other shapes (rectangle, etc.) and methods (translate, etc.).

  1. /** "Component" */
    
  2. interface Graphic {
    
  3.     // Prints the graphic.
    
  4.     public void print();
    
  5. }
    
  1. /** "Composite" */
    
  2. import java.util.List;
    
  3. import java.util.ArrayList;
    
  4. class CompositeGraphic implements Graphic {
    
  5.     // Collection of child graphics.
    
  6.     private List<Graphic> mChildGraphics = new ArrayList<Graphic>();
    
  7.     // Prints the graphic.
    
  8.     public void print() {
    
  9.         for (Graphic graphic : mChildGraphics) {
    
  10.             graphic.print();
    
  11.         }
    
  12.     }
    
  13.     // Adds the graphic to the composition.
    
  14.     public void add(Graphic graphic) {
    
  15.         mChildGraphics.add(graphic);
    
  16.     }
    
  17.     // Removes the graphic from the composition.
    
  18.     public void remove(Graphic graphic) {
    
  19.         mChildGraphics.remove(graphic);
    
  20.     }
    
  21. }
    
  1. /** "Leaf" */
    
  2. class Ellipse implements Graphic {
    
  3.     // Prints the graphic.
    
  4.     public void print() {
    
  5.         System.out.println("Ellipse");
    
  6.     }
    
  7. }
    
  1. /** Client */
    
  2. public class Program {
    
  3.     public static void main(String[] args) {
    
  4.         // Initialize four ellipses
    
  5.         Ellipse ellipse1 = new Ellipse();
    
  6.         Ellipse ellipse2 = new Ellipse();
    
  7.         Ellipse ellipse3 = new Ellipse();
    
  8.         Ellipse ellipse4 = new Ellipse();
    
  9.         // Initialize three composite graphics
    
  10.         CompositeGraphic graphic = new CompositeGraphic();
    
  11.         CompositeGraphic graphic1 = new CompositeGraphic();
    
  12.         CompositeGraphic graphic2 = new CompositeGraphic();
    
  13.         // Composes the graphics
    
  14.         graphic1.add(ellipse1);
    
  15.         graphic1.add(ellipse2);
    
  16.         graphic1.add(ellipse3);
    
  17.         graphic2.add(ellipse4);
    
  18.         graphic.add(graphic1);
    
  19.         graphic.add(graphic2);
    
  20.         // Prints the complete graphic (four times the string "Ellipse").
    
  21.         graphic.print();
    
  22.     }
    
  23. }
    
Implementation in PHP
<?php
/** "Component" */
interface Graphic
{
    /**
     * Prints the graphic
     *
     * @return void
     */
    public function printOut();
}
 
/**
 * "Composite" - Collection of graphical components
 */
class CompositeGraphic implements Graphic
{
    /**
     * Collection of child graphics
     *
     * @var array
     */
    private $childGraphics = array();
 
    /**
     * Prints the graphic
     *
     * @return void
     */
    public function printOut()
    {
        foreach ($this->childGraphics as $graphic) {
            $graphic->printOut();
        }
    }
 
    /**
     * Adds the graphic to the composition
     *
     * @param Graphic $graphic Graphical element
     *
     * @return void  
     */
    public function add(Graphic $graphic)
    {
        $this->childGraphics[] = $graphic;
    }
 
    /**
     * Removes the graphic from the composition
     *
     * @param Graphic $graphic Graphical element
     *
     * @return void
     */
    public function remove(Graphic $graphic)
    {
        if (in_array($graphic, $this->childGraphics)) {
            unset($this->childGraphics[array_search($graphic, $this->childGraphics)]);
        }
    }
}
 
/** "Leaf" */
class Ellipse implements Graphic
{
    /**
     * Prints the graphic
     *
     * @return void
     */
    public function printOut()
    {
        echo "Ellipse";
    }
}
 
/** Client */
 
//Initialize four ellipses
$ellipse1 = new Ellipse();
$ellipse2 = new Ellipse();
$ellipse3 = new Ellipse();
$ellipse4 = new Ellipse();
 
//Initialize three composite graphics
$graphic = new CompositeGraphic();
$graphic1 = new CompositeGraphic();
$graphic2 = new CompositeGraphic();
 
//Composes the graphics
$graphic1->add($ellipse1);
$graphic1->add($ellipse2);
$graphic1->add($ellipse3);
 
$graphic2->add($ellipse4);
 
$graphic->add($graphic1);
$graphic->add($graphic2);
 
//Prints the complete graphic (four times the string "Ellipse").
$graphic->printOut();
Implementation in Python
class Component(object):
    def __init__(self, *args, **kw):
        pass
    def component_function(self): pass
class Leaf(Component):
    def __init__(self, *args, **kw):
        Component.__init__(self, *args, **kw)
    def component_function(self):
        print "some function"
class Composite(Component):
    def __init__(self, *args, **kw):
        Component.__init__(self, *args, **kw)
        self.children = []
 
    def append_child(self, child):
        self.children.append(child)
 
    def remove_child(self, child):
        self.children.remove(child)
    def component_function(self):
        map(lambda x: x.component_function(), self.children)
 
c = Composite()
l = Leaf()
l_two = Leaf()
c.append_child(l)
c.append_child(l_two)
c.component_function()
Implementation in Ruby
module Component
  def do_something
    raise NotImplementedError
  end
end
class Leaf
  include Component
  def do_something
    puts "Hello"
  end
end
class Composite
  include Component
  attr_accessor :children
  def initialize
    self.children = []
  end
  def do_something
    children.each {|c| c.do_something}
  end
  def append_child(child)
    children << child
  end
  def remove_child(child)
    children.delete child
  end
end
composite = Composite.new
leaf_one = Leaf.new
leaf_two = Leaf.new
composite.append_child(leaf_one)
composite.append_child(leaf_two)
composite.do_something

Decorator

The decorator pattern helps to add behavior or responsibilities to an object. This is also called “Wrapper”.

Examples

Cost

This pattern can be very expensive. You should only use it when it is really necessary. You should have lots of different behaviors and responsibilities for the same class.

Creation

This pattern is expensive to create.

Maintenance

This pattern can be expensive to maintain. If the representation of a class often changes, you will have lots of refactoring.

Removal

This pattern is hard to remove too.

Advises

  • Put the decorator term in the name of the decorator classes to indicate the use of the pattern to the other developers.

Implementations

Implementation in C#

This example illustrates a simple extension method for a bool type.

using System;
static class BooleanExtensionMethodSample
{
    public static void Main()
    {
        bool yes = true;
        bool no = false;
        // Toggle the booleans! yes should return false and no should return true.
        Console.WriteLine(yes.Toggle());
        Console.WriteLine(no.Toggle());
    }
    // The extension method that adds Toggle to bool.
    public static bool Toggle(this bool target)
    {
        // Evaluate the input and then return the opposite value.
        if (target)
            return false;
        else
            return true; // Satisfy the compiler in case of a null value.
    }
}
Implementation in C++

Coffee making scenario

# include <iostream>
# include <string>
// The abstract coffee class
class Coffee
{
public:
    virtual double getCost() = 0;
    virtual std::string getIngredient() = 0;
};
// Plain coffee without ingredient
class SimpleCoffee:public Coffee
{
private:
    double cost;
    std::string ingredient;
public:
    SimpleCoffee()
    {
        cost = 1;
        ingredient = std::string("Coffee");
    }
    double getCost()
    {
        return cost;
    }
    std::string getIngredient()
    {
        return ingredient;
    }
};
// Abstract decorator class
class CoffeeDecorator:public Coffee
{
protected:
    Coffee & decoratedCoffee;
public:
    CoffeeDecorator(Coffee & decoratedCoffee):decoratedCoffee(decoratedCoffee){}
};
// Milk Decorator
class Milk:public CoffeeDecorator
{
private:
    double cost;
public:
    Milk(Coffee & decoratedCoffee):CoffeeDecorator(decoratedCoffee)
    {
        cost = 0.5;
    }
    double getCost()
    {
        return cost + decoratedCoffee.getCost();
    }
    std::string getIngredient()
    {
        return "Milk "+decoratedCoffee.getIngredient();
    }
};
// Whip decorator
class Whip:public CoffeeDecorator
{
private:
    double cost;
public:
    Whip(Coffee & decoratedCoffee):CoffeeDecorator(decoratedCoffee)
    {
        cost = 0.7;
    }
    double getCost()
    {
        return cost + decoratedCoffee.getCost();
    }
    std::string getIngredient()
    {
        return "Whip "+decoratedCoffee.getIngredient();
    }
};
// Sprinkles decorator
class Sprinkles:public CoffeeDecorator
{
private:
    double cost;
public:
    Sprinkles(Coffee & decoratedCoffee):CoffeeDecorator(decoratedCoffee)
    {
        cost = 0.6;
    }
    double getCost()
    {
        return cost + decoratedCoffee.getCost();
    }
    std::string getIngredient()
    {
        return "Sprinkles "+decoratedCoffee.getIngredient();
    }
};
// Here's a test
int main()
{
    Coffee* sample;
    sample = new SimpleCoffee();
    sample = new Milk(*sample);
    sample = new Whip(*sample);
    std::cout << sample->getIngredient() << std::endl;
    std::cout << "Cost: " << sample->getCost() << std::endl;
}

The output of this program is given below:

Whip Milk Coffee
Cost: 2.2

Implementation in JavaScript
// Class to be decorated
function Coffee() {
    this.cost = function() {
return 1;
    };
}
// Decorator A
function Milk(coffee) {
    this.cost = function() {
return coffee.cost() + 0.5;
    };
}
// Decorator B
function Whip(coffee) {
    this.cost = function() {
return coffee.cost() + 0.7;
    };
}
// Decorator C
function Sprinkles(coffee) {
    this.cost = function() {
return coffee.cost() + 0.2;
    };
}
// Here's one way of using it
var coffee = new Milk(new Whip(new Sprinkles(new Coffee())));
alert( coffee.cost() );
// Here's another
var coffee = new Coffee();
coffee = new Sprinkles(coffee);
coffee = new Whip(coffee);
coffee = new Milk(coffee);
alert(coffee.cost());
Implementation in Java

First Example (window/scrolling scenario)

The following Java example illustrates the use of decorators using the window/scrolling scenario.

// the Window abstract class
public abstract class Window {
    public abstract void draw(); // draws the Window
    public abstract String getDescription(); // returns a description of the Window
}
// extension of a simple Window without any scrollbars
class SimpleWindow extends Window {
    public void draw() {
        // draw window
    }
    public String getDescription() {
        return "simple window";
    }
}

The following classes contain the decorators for all Window classes, including the decorator classes themselves.

// abstract decorator class - note that it extends Window
abstract class WindowDecorator extends Window {
    protected Window decoratedWindow; // the Window being decorated
    public WindowDecorator (Window decoratedWindow) {
        this.decoratedWindow = decoratedWindow;
    }
    public void draw() {
        decoratedWindow.draw(); //delegation
    }
    public String getDescription() {
        return decoratedWindow.getDescription(); //delegation
    }
}
// the first concrete decorator which adds vertical scrollbar functionality
class VerticalScrollBarDecorator extends WindowDecorator {
    public VerticalScrollBarDecorator (Window decoratedWindow) {
        super(decoratedWindow);
    }
    @Override
    public void draw() {
        super.draw();
        drawVerticalScrollBar();
    }
    private void drawVerticalScrollBar() {
        // draw the vertical scrollbar
    }
    @Override
    public String getDescription() {
        return super.getDescription() + ", including vertical scrollbars";
    }
}
// the second concrete decorator which adds horizontal scrollbar functionality
class HorizontalScrollBarDecorator extends WindowDecorator {
    public HorizontalScrollBarDecorator (Window decoratedWindow) {
        super(decoratedWindow);
    }
    @Override
    public void draw() {
        super.draw();
        drawHorizontalScrollBar();
    }
    private void drawHorizontalScrollBar() {
        // draw the horizontal scrollbar
    }
    @Override
    public String getDescription() {
        return super.getDescription() + ", including horizontal scrollbars";
    }
}

Here's a test program that creates a Window instance which is fully decorated (i.e., with vertical and horizontal scrollbars), and prints its description:

public class DecoratedWindowTest {
    public static void main(String[] args) {
        // create a decorated Window with horizontal and vertical scrollbars
        Window decoratedWindow = new HorizontalScrollBarDecorator (
                new VerticalScrollBarDecorator(new SimpleWindow()));
        // print the Window's description
        System.out.println(decoratedWindow.getDescription());
    }
}

The output of this program is "simple window, including vertical scrollbars, including horizontal scrollbars". Notice how the getDescription method of the two decorators first retrieve the decorated Window's description and decorates it with a suffix.

Second Example (coffee making scenario)

The next Java example illustrates the use of decorators using coffee making scenario. In this example, the scenario only includes cost and ingredients.

// The abstract Coffee class defines the functionality of Coffee implemented by decorator
public abstract class Coffee {
    public abstract double getCost(); // returns the cost of the coffee
    public abstract String getIngredients(); // returns the ingredients of the coffee
}
// extension of a simple coffee without any extra ingredients
public class SimpleCoffee extends Coffee {
    public double getCost() {
        return 1;
    }
    public String getIngredients() {
        return "Coffee";
    }
}

The following classes contain the decorators for all Coffee classes, including the decorator classes themselves..

// abstract decorator class - note that it extends Coffee abstract class
public abstract class CoffeeDecorator extends Coffee {
    protected final Coffee decoratedCoffee;
    protected String ingredientSeparator = ", ";
    public CoffeeDecorator(Coffee decoratedCoffee) {
        this.decoratedCoffee = decoratedCoffee;
    }
    public double getCost() { // implementing methods of the abstract class
        return decoratedCoffee.getCost();
    }
    public String getIngredients() {
        return decoratedCoffee.getIngredients();
    }
}
// Decorator Milk that mixes milk with coffee
// note it extends CoffeeDecorator
class Milk extends CoffeeDecorator {
    public Milk(Coffee decoratedCoffee) {
        super(decoratedCoffee);
    }
    public double getCost() { // overriding methods defined in the abstract superclass
        return super.getCost() + 0.5;
    }
    public String getIngredients() {
        return super.getIngredients() + ingredientSeparator + "Milk";
    }
}
// Decorator Whip that mixes whip with coffee
// note it extends CoffeeDecorator
class Whip extends CoffeeDecorator {
    public Whip(Coffee decoratedCoffee) {
        super(decoratedCoffee);
    }
    public double getCost() {
        return super.getCost() + 0.7;
    }
    public String getIngredients() {
        return super.getIngredients() + ingredientSeparator + "Whip";
    }
}
// Decorator Sprinkles that mixes sprinkles with coffee
// note it extends CoffeeDecorator
class Sprinkles extends CoffeeDecorator {
    public Sprinkles(Coffee decoratedCoffee) {
        super(decoratedCoffee);
    }
    public double getCost() {
        return super.getCost() + 0.2;
    }
    public String getIngredients() {
        return super.getIngredients() + ingredientSeparator + "Sprinkles";
    }
}

Here's a test program that creates a Coffee instance which is fully decorated (i.e., with milk, whip, sprinkles), and calculate cost of coffee and prints its ingredients:

public class Main {
 
    public static final void main(String[] args) {
Coffee c = new SimpleCoffee();
System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());
c = new Milk(c);
System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());
c = new Sprinkles(c);
System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());
c = new Whip(c);
System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());
// Note that you can also stack more than one decorator of the same type
c = new Sprinkles(c);
System.out.println("Cost: " + c.getCost() + "; Ingredients: " + c.getIngredients());
    }
 
}

The output of this program is given below:

Cost: 1.0 Ingredient: Coffee
Cost: 1.5 Ingredient: Coffee, Milk
Cost: 1.7 Ingredient: Coffee, Milk, Sprinkles
Cost: 2.4 Ingredient: Coffee, Milk, Sprinkles, Whip

Implementation in Python

Window System

# the Window base class
class Window(object):
    def draw(self, device):
        device.append('flat window')
    def info(self):
        pass
# The decorator pattern approch
class WindowDecorator:
    def __init__(self, w):
        self.window = w
    def draw(self, device):
        self.window.draw(device)
    def info(self):
        self.window.info()
class BorderDecorator(WindowDecorator):
    def draw(self, device):
        self.window.draw(device)
        device.append('borders')
class ScrollDecorator(WindowDecorator):
    def draw(self, device):
        self.window.draw(device)
        device.append('scroll bars')
def test_deco():
    # The way of using the decorator classes
    w = ScrollDecorator(BorderDecorator(Window()))
    dev = []
    w.draw(dev)
    print dev
test_deco()

Difference between subclass method and decorator pattern

# The subclass approch
class BorderedWindow(Window):
    def draw(self, device):
        super(BorderedWindow, self).draw(device)
        device.append('borders')
class ScrolledWindow(Window):
    def draw(self, device):
        super(ScrolledWindow, self).draw(device)
        device.append('scroll bars')
# combine the functionalities using multiple inheritance.
class MyWindow(ScrolledWindow, BorderedWindow, Window):
    pass
def test_muli():
    w = MyWindow()
    dev = []
    w.draw(dev)
    print dev
def test_muli2():
    # note that python can create a class on the fly.
    MyWindow = type('MyWindow', (ScrolledWindow, BorderedWindow, Window), {})
    w = MyWindow()
    dev = []
    w.draw(dev)
    print dev
test_muli()
test_muli2()

Dynamic languages

The decorator pattern can also be implemented in dynamic languages either with interfaces or with traditional OOP inheritance.

External links

Facade

A Facade pattern hides the complexities of the system and provides an interface to the client from where the client can access the system. Dividing a system into subsystems helps reduce complexity. We need to minimize the communication and dependencies between subsystems. For this, we introduce a facade object that provides a single, simplified interface to the more general facilities of a subsystem.

Examples

The SCP command is a shortcut for SSH commands. A remote file copy could be done writing several commands with an SSH connection but it can be done in one command with SCP. So the SCP command is a facade for the SSH commands. Although it may not be coded in the object programming paradigm, it is a good illustration of the design pattern.

Cost

This pattern is very easy and has not additional cost.

Creation

This pattern is very easy to create.

Maintenance

This pattern is very easy to maintain.

Removal

This pattern is very easy to remove too.

Advises

  • Do not use this pattern to mask only three or four method calls.

Implementations

Implementation in Java

This is an abstract example of how a client ("you") interacts with a facade (the "computer") to a complex system (internal computer parts, like CPU and HardDrive).

/* Complex parts */
 
class CPU {
    public void freeze() { ... }
    public void jump(long position) { ... }
    public void execute() { ... }
}
class Memory {
    public void load(long position, byte[] data) { ... }
}
class HardDrive {
    public byte[] read(long lba, int size) { ... }
}
/* Facade */
 
class Computer {
    private CPU processor;
    private Memory ram;
    private HardDrive hd;
 
    public Computer() {
        this.processor = new CPU();
        this.ram = new Memory();
        this.hd = new HardDrive();
    }
 
    public void start() {
        processor.freeze();
        ram.load(BOOT_ADDRESS, hd.read(BOOT_SECTOR, SECTOR_SIZE));
        processor.jump(BOOT_ADDRESS);
        processor.execute();
    }
}
/* Client */
 
class You {
    public static void main(String[] args) {
        Computer facade = new Computer();
        facade.start();
    }
}
Implementation in C#
using System;
 
namespace Facade
{
	public class CPU
        {
		public void Freeze() { }
		public void Jump(long addr) { }
		public void Execute() { }
	}
 
	public class Memory
	{
		public void Load(long position, byte[] data) { }
        }
 
	public class HardDrive
	{
		public byte[] Read(long lba, int size) { return null; }
	}
 
	public class Computer
	{
		var cpu = new CPU();
		var memory = new Memory();
		var hardDrive = new HardDrive();
 
		public void StartComputer()
		{
			cpu.Freeze();
			memory.Load(0x22, hardDrive.Read(0x66, 0x99));
			cpu.Jump(0x44);
			cpu.Execute();
		}
	}
 
	public class SomeClass
	{
            public static void Main(string[] args)
            {
                var facade = new Computer();
                facade.StartComputer();
            }
        }
}
Implementation in Ruby
# Complex parts
class CPU
  def freeze; puts 'CPU: freeze'; end
  def jump(position); puts "CPU: jump to #{position}"; end
  def execute; puts 'CPU: execute'; end
end
 
class Memory
  def load(position, data)
    puts "Memory: load #{data} at #{position}"
  end
end
 
class HardDrive
  def read(lba, size)
    puts "HardDrive: read sector #{lba} (#{size} bytes)"
    return 'hdd data'
  end
end
 
# Facade
class Computer
  BOOT_ADDRESS = 0
  BOOT_SECTOR = 0
  SECTOR_SIZE = 512
 
  def initialize
    @cpu = CPU.new
    @memory = Memory.new
    @hard_drive = HardDrive.new
  end
 
  def start_computer
    @cpu.freeze
    @memory.load(BOOT_ADDRESS, @hard_drive.read(BOOT_SECTOR, SECTOR_SIZE))
    @cpu.jump(BOOT_ADDRESS)
    @cpu.execute
  end
end
 
# Client
facade = Computer.new
facade.start_computer
Implementation in Python
# Complex parts
class CPU:
    def freeze(self): pass
    def jump(self, position): pass
    def execute(self): pass
 
class Memory:
    def load(self, position, data): pass
 
class HardDrive:
    def read(self, lba, size): pass
 
# Facade
class Computer:
    def __init__(self):
        self.cpu = CPU()
        self.memory = Memory()
        self.hard_drive = HardDrive()
 
    def start_computer(self):
        self.cpu.freeze()
        self.memory.load(0, self.hard_drive.read(0, 1024))
        self.cpu.jump(10)
        self.cpu.execute()
 
# Client
if __name__ == '__main__':
    facade = Computer()
    facade.start_computer()
Implementation in PHP
/* Complex parts */
class CPU
{
    public function freeze() { /* ... */ }
    public function jump( $position ) { /* ... */ }
    public function execute() { /* ... */ }
 
}
 
class Memory
{
    public function load( $position, $data ) { /* ... */ }
}
 
class HardDrive
{
    public function read( $lba, $size ) { /* ... */ }
}
 
/* Facade */
class Computer
{
    protected $cpu = null;
    protected $memory = null;
    protected $hardDrive = null;
 
    public function __construct()
    {
        $this->cpu = new CPU();
        $this->memory = new Memory();
        $this->hardDrive = new HardDrive();
    }
 
    public function startComputer()
    {
        $this->cpu->freeze();
        $this->memory->load( BOOT_ADDRESS, $this->hardDrive->read( BOOT_SECTOR, SECTOR_SIZE ) );
        $this->cpu->jump( BOOT_ADDRESS );
        $this->cpu->execute();
    }
}
 
/* Client */
$facade = new Computer();
$facade->startComputer();
Implementation in JavaScript
/* Complex parts */
var CPU = function () {};
CPU.prototype = {
  freeze: function () {
    console.log('CPU: freeze');
  },
  jump: function (position) {
    console.log('CPU: jump to ' + position);
  },
  execute: function () {
    console.log('CPU: execute');
  }
};
 
var Memory = function () {};
Memory.prototype = {
  load: function (position, data) {
    console.log('Memory: load "' + data + '" at ' + position);
  }
};
 
var HardDrive = function () {};
HardDrive.prototype = {
  read: function (lba, size) {
    console.log('HardDrive: read sector ' + lba + '(' + size + ' bytes)');
    return 'hdd data';
  }
};
 
 
/* Facade */
var Computer = function () {
  var cpu, memory, hardDrive;
 
  cpu = new CPU();
  memory = new Memory();
  hardDrive = new HardDrive();
 
  var constant = function (name) {
    var constants = {
      BOOT_ADDRESS: 0,
      BOOT_SECTOR: 0,
      SECTOR_SIZE: 512
    };
 
    return constants[name];
  };
 
  this.startComputer = function () {
    cpu.freeze();
    memory.load(constant('BOOT_ADDRESS'), hardDrive.read(constant('BOOT_SECTOR'), constant('SECTOR_SIZE')));
    cpu.jump(constant('BOOT_ADDRESS'));
    cpu.execute();
  }
 
};
 
/* Client */
var facade = new Computer();
facade.startComputer();
Implementation in ActionScript 3.0
/* Complex Parts */
 
/* CPU.as */
package
{
    public class CPU
    {
        public function freeze():void
        {
            trace("CPU::freeze");
        }
 
        public function jump(addr:Number):void
        {
            trace("CPU::jump to", String(addr));
        }
 
        public function execute():void
        {
            trace("CPU::execute");
        }
    }
}
 
/* Memory.as */
package
{
    import flash.utils.ByteArray;
 
    public class Memory
    {
        public function load(position:Number, data:ByteArray):void
        {
            trace("Memory::load position:", position, "data:", data);
        }
    }
}
 
/* HardDrive.as */
package
{
    import flash.utils.ByteArray;
 
    public class HardDrive
    {
        public function read(lba:Number, size:int):ByteArray
        {
            trace("HardDrive::read returning null");
            return null;
        }
    }
}
 
/* The Facade */
/* Computer.as */
package
{
    public class Computer
    {
        public static const BOOT_ADDRESS:Number = 0x22;
        public static const BOOT_SECTOR:Number = 0x66;
        public static const SECTOR_SIZE:int = 0x200;
 
        private var _cpu:CPU;
        private var _memory:Memory;
        private var _hardDrive:HardDrive;
 
        public function Computer()
        {
            _cpu = new CPU();
            _memory = new Memory();
            _hardDrive = new HardDrive();
        }
 
        public function startComputer():void
        {
            _cpu.freeze();
            _memory.load(BOOT_ADDRESS, _hardDrive.read(BOOT_SECTOR, SECTOR_SIZE));
            _cpu.jump(BOOT_ADDRESS);
            _cpu.execute();
        }
    }
}
 
/* Client.as : This is the application's Document class */
package
{
    import flash.display.MovieClip;
 
    public class Client extends MovieClip
    {       
        private var _computer:Computer;
 
        public function Client()
        {
            _computer = new Computer();
            _computer.startComputer();
        }
    }
}
Implementation in Scala
/* Complex parts */
 
package intel {
  class CPU {
    def freeze() = ???
    def jump(position: Long) = ???
    def execute() = ???
  }
}
 
package ram.plain {
  class Memory {
    def load(position: Long, data: Array[Byte]) = ???
  }
}
 
package hdd {
  class HardDrive {
    def read(lba: Long, size: Int): Array[Byte]  = ???
  }
}
/* Facade */
//imports for the facade
import common.patterns.intel.CPU
import common.patterns.ram.plain.Memory
import common.patterns.hdd.HardDrive
 
package pk {
  class ComputerFacade(conf: String) {
    val processor: CPU = new CPU
    val ram: Memory = new Memory
    val hd: HardDrive = new HardDrive
 
    val BOOT_ADDRESS: Long = ???
    val BOOT_SECTOR: Long = ???
    val SECTOR_SIZE: Int = ???
 
    def start() = {
      processor.freeze()
      ram.load(BOOT_ADDRESS, hd.read(BOOT_SECTOR, SECTOR_SIZE))
      processor.jump(BOOT_ADDRESS)
      processor.execute()
    }
  }
}
//imports for your package
import common.patterns.pk.ComputerFacade
 
/* Client */
 
object You {
  def main(args: Array[String]) {
    new ComputerFacade("conf").start()
  }
}

Factory method

The factory pattern is a design pattern used to promote encapsulation of data representation.

Problem 
We want to decide at run time what object is to be created based on some configuration or application parameter. When we write the code we do not know what class should be instantiated.
Solution 
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. It's primary purpose is to provide a way for users to retrieve an instance with a known compile-time type, but whose runtime type may actually be different. In other words, a factory method that is supposed to return an instance of the class Foo may return an instance of the class Foo, or it may return an instance of the class Bar, so long as Bar inherits from Foo. The reason for this is that it strengthens the boundary between implementor and client, hiding the true representation of the data (see Abstraction Barrier) from the user, thereby allowing the implementor to change this representation at anytime without affecting the client, as long as the client facing interface doesn't change.

Basic Implementation of the Factory Pattern

The general template for implementing the factory pattern is to provide a primary user facing class with static methods which the user can use to get instances with that type. Constructors are then made private/protected from the user, forcing them to use the static factory methods to get objects. The following Java code shows a very simple implementation of the factory pattern for type Foo.

public class Foo {
  // Static factory method
  public static Foo getInstance() {
    // Inside this class, we have access to private methods
    return new Foo();
  }
  // Guarded constructor, only accessible from within this class, since it's marked private
  private Foo() {
    // Typical initialization code goes here
  }
} // End class Foo

With this code, it would be impossible for a client of the code to use the new operator to get an instance of the class, as is traditionally done:

// Client code
Foo f = new Foo(); // Won't Work!

because the constructor is marked private. Instead, the client would have to use the factory method:

// Client code
Foo f = Foo.getInstance(); // Works!

It should be noted that even within a programming language community, there is no general consensus as to the naming convention of a factory method. Some suggest naming the method with the name of the class, similar to a normal constructor, but starting with a lowercase. Others say that this is confusing, and suggest using an accessor type syntax, like the getInstance style used above, though others complain that this may incorrectly imply a singleton implementation. Likewise, some offer newInstance, but this is criticized as being misleading in certain situations where a strictly new instance may not actually be returned (once again, refer to the singleton pattern). As such, we will not attempt to follow any particularly rigid standard here, we will simply try to use a name that makes the most sense for our current purposes.

Factory Pattern Implementation of the Alphabet

That's great, you know how to implement a real simple factory pattern, but what good did it do you? Users are asking for something that fits into type Foo, and they're getting an instance of the class Foo, how is that different from just calling the constructor? Well it's not, except that you're putting another function call on the stack (which is a bad thing). But that's only for the above case. We'll now discuss a more useful use of the factory pattern. Consider a simple type called Letter, representing a letter in the alphabet, which has the following client facing interface (i.e., public instance methods):

char toCharacter();
boolean isVowel();
boolean isConsonant();

We could implement this easily enough without using the factory method, which might start out something like this:

public class Letter {
  private char fTheLetter;
  public Letter(char aTheLetter) {
    fTheLetter = aTheLetter;
  }
  public char toCharacter() {
    return fTheLetter;
  }
  public boolean isVowel() {
    //TODO: we haven't implemented this yet
    return true;
  }
  public boolean isConsonant() {
    // TODO: we haven't implemented this yet
    return false;
  }
} // End class Letter

Fairly simple, but notice we haven't implemented the last two methods yet. We can still do it pretty easily. The first might look like this:

  public boolean isVowel() {
    return
      fTheLetter == 'a' ||
      fTheLetter == 'e' ||
      fTheLetter == 'i' ||
      fTheLetter == 'o' ||
      fTheLetter == 'u' ||
      fTheLetter == 'A' ||
      fTheLetter == 'E' ||
      fTheLetter == 'I' ||
      fTheLetter == 'O' ||
      fTheLetter == 'U';
  }

Now that's not too bad, but we still need to do isConsonant. Fortunately, we at least know in this case that if it's a vowel, it's not a consonant, and vice versa, so our last method could simply be:

  public boolean isConsonant() {
    return !this.isVowel();
  }

So what's the problem here? Basically, every time you call either of these methods, your program has to do all that checking. Granted, this isn't a real heavy burden for the Java Runtime Environment, but you can imagine a much more complex, much more time consuming operation. Wouldn't it be great if we could avoid doing this every time we call the method? Let's say, for instance, we could do it once when we create the object, and then not have to do it again. Well sure, we can do that. Here's an implementation that'll do that for us, and we still don't have to use the factory method:

public class Letter {
  private char fTheLetter;
  private boolean fIsVowel;
  public Letter(char aTheLetter) {
    fTheLetter = aTheLetter;
    fIsVowel = fTheLetter == 'a' ||
      fTheLetter == 'e' ||
      fTheLetter == 'i' ||
      fTheLetter == 'o' ||
      fTheLetter == 'u' ||
      fTheLetter == 'A' ||
      fTheLetter == 'E' ||
      fTheLetter == 'I' ||
      fTheLetter == 'O' ||
      fTheLetter == 'U';
  }
  public char toCharacter() {
    return fTheLetter;
  }
  public boolean isVowel() {
    return fIsVowel;
  }
  public boolean isConsonant() {
    return !fIsVowel;
  }
} // End class Letter

Notice how we moved the lengthy operation into the constructor, and stored the result. OK, so now we're all fine and dandy, no? Sure, but let's say you came up with a new idea, a different implementation: you want to split this type into two classes, one class to handle the vowels, and one to handle the consonants. Great, they can both be subclasses of the Letter class, and the user will never know the difference, right? Wrong. How is the client supposed to get at these new classes? They've got code that works perfectly well for them by calling new Letter('a') and new Letter('Z'). Now you're going to make them go through all their code and change these to new Vowel('a') and new Consonant('Z')? They probably won't be too happy with that. If only you could get new instances of both classes from one method! Well you can, just use a static method in the Letter class, it'll do the same one-time checking as the constructor did, and will return an appropriate instance of the right class. And what do you know, it's a factory method! But that still doesn't do your client much good, they still need to go through and change all the new Letter()s into Letter.getLetter(). Well, sad to say, it's too late to help them at all, unless you just give up your new implementation. But that illustrates the reason for using the factory method right off the bat. One of the key components of good object oriented programming is that you never know exactly where your code will go in the future. By making good use of the abstraction barrier and using encapsulation-friendly programming patterns, such as the factory pattern, you can better prepare yourself—and your client—for future changes to the specific implementation. In particular, it allows you to use a "big hammer" kind of approach to get something done in a perhaps-less-than-ideal but rapid manner in order to meet deadlines or move ahead with testing,. You can then go back later and refine the implementation—the data representation and algorithms—to be faster, smaller, or what-have-you, and as long as you maintained the abstraction barrier between implementor and client and properly encapsulated your implementation, then you can change it without requiring the client to change any of their code. Well now that I'm sure you're a raving advocate for the factory method, let's take a look at how we would implement it for our Letter type:

public abstract class Letter {
 
// Factory Method
 public static Letter getLetter(char aTheLetter) {
 // Like before, we do a one time check to see what kind of
 // letter we are dealing with. Only this time, instead of setting
 // a property to track it, we actually have a different class for each
 // of the two letter types.
 if (
 aTheLetter == 'a' ||
 aTheLetter == 'e' ||
 aTheLetter == 'i' ||
 aTheLetter == 'o' ||
 aTheLetter == 'u' ||
 aTheLetter == 'A' ||
 aTheLetter == 'E' ||
 aTheLetter == 'I' ||
 aTheLetter == 'O' ||
 aTheLetter == 'U'
 ) {
   return new Vowel(aTheLetter);
 } else {
   return new Consonant(aTheLetter);
 }
 }
 
 
// User facing interface
// We make these methods abstract, thereby requiring all subclasses
// (actually, just all concrete subclasses, that is, non-abstract)
// to implement the methods.
 public abstract boolean isVowel();
 
 public abstract boolean isConsonant();
 
 public abstract char getChar();
 
 
 // Now we define the two concrete classes for this type,
 // the ones that actually implement the type.
 private static class Vowel extends Letter {
 private char iTheLetter;
 
 // Constructor
 Vowel(char aTheLetter) {
 this.iTheLetter = aTheLetter;
 }
 
 // Nice easy implementation of this method!
 public boolean isVowel() {
 return true;
 }
 
 // This one, too!
 public boolean isConsonant() {
 return false;
 }
 public char getLetter(){
   return iTheLetter;
 }
 } // End local class Vowel
 private static class Consonant extends Letter {
 private char iTheLetter;
 
 // Constructor
 Consonant(char aTheLetter) {
   this.iTheLetter = aTheLetter;
 }
 
 public boolean isVowel() {
   return false;
 }
 
 public boolean isConsonant(){
   return true;
 }
 public char getLetter(){
   return iTheLetter;
 }
 } // End local class Consonant
 
} // End toplevel class Letter

Several things to note here.

  • First, you'll notice the top level class Letter is abstract. This is fine because you'll notice that it doesn't actually do anything except define the interface and provide a top level container for the two other classes. However, it's also important (not just OK) to make this abstract because we don't want people trying to instantiate the Letter class directly. Of course we could solve this problem by making a private constructor, but making the class abstract instead is cleaner, and makes it more obvious that the Letter class is not meant to be instantiated. It also, as mentioned, allows us to define the user facing interface that the work horse classes need to implement.
  • The two nested classes we created are called local classes, which is basically the same as an inner class except that local classes are static, and inner classes are not. They have to be static so that our static factory method can create them. If they were non static (i.e., dynamic) then they could only be accessed through an instance of the Letter class, which we can never have because Letter is abstract. Also note that (in Java, anyway) the fields for inner and local classes typically use the "i" (for inner) prefix, as opposed to the "f" (for field) prefix used by top level classes. This is simply a naming convention used by many Java programmers and doesn't actually effect the program.
  • The two nested classes that implement the Letter data type do not actually have to be local/inner. They could just have easily been top level classes that extend the abstract Letter class. However, this is contrary to the point of the factory pattern, which is encapsulation. Top level classes can't be private in Java, because that doesn't make any sense (what are they private to?) and the whole point is that no client has to (or should, really) know how the type is implemented. Making these classes top level allows clients to potentially stumble across them, and worse yet, instantiate them, by-passing the factory pattern all together.
  • Lastly, this is not very good code. There's a lot of ways we can make it better to really illustrate the power of the factory pattern. I'll discuss these refactorings briefly, and then show another, more polished, version of the above code which includes a lot of them.

Refactoring the Factory Pattern

Notice that both of the local classes do the same thing in a few places. This is redundant code which is not only more work to write, but it's also highly discouraged in object oriented programming (partially because it takes more work to write, but mostly because it's harder to maintain and prone to errors, e.g., you find a bug in the code and change it in one spot, but forget to in another.) Below is a list of redundancies in the above code:

  • The field iTheLetter
  • The method getLetter()
  • The constructor of each inner class does the same thing.

In addition, as we discovered above, the isVowel() and isConsonant() just happen to always return the opposite of each other for a given instance. However, since this is something of a peculiarity for this particular example, we won't worry about it. The lesson you would learn from us doing that will already be covered in the refactoring of the getLetter() method. OK, so we have redundant code in two classes. If you're familiar with abstracting processes, then this is probably a familiar scenario to you. Often, having redundant code in two different classes makes them prime candidates for abstraction, meaning that a new abstract class is created to implement the redundant code, and the two classes simply extend this new abstract class instead of implementing the redundant code. Well what do you know? We already have an abstract super class that our redundant classes have in common. All we have to do is make the super class implement the redundant code, and the other classes will automatically inherit this implementation, as long as we don't override it. So that works fine for the getLetter() method, we can move both the method and the iTheLetter field up to the abstract parent class. But what about the constructors? Well our constructor takes an argument, so we won't automatically inherit it, that's just the way java works. But we can use the super keyword to automatically delegate to the super classes constructor. In other words, we'll implement the constructor in the super class, since that's where the field is anyway, and the other two classes will delegate to this method in their own constructors. For our example, this doesn't save much work, we're replacing a one line assignment with a one line call to super(), but in theory, there could be hundred of lines of code in the constructors, and moving it up could be a great help. At this point, you might be a little worried about putting a constructor in the Letter class. Didn't I already say not to do that? I thought we didn't want people trying to instantiate Letter directly? Don't worry, the class is still abstract. Even if there's a concrete constructor, Java won't let you instantiate an abstract class, because it's abstract, it could have method that are accessible but undefined, and it wouldn't know what to do if such a method was invoked. So putting the constructor in is fine. After making the above refactorings, our code now looks like this:

public abstract class Letter {
 
// Factory Method
 public static Letter getLetter(char aTheLetter){
 if (
 aTheLetter == 'a' ||
 aTheLetter == 'e' ||
 aTheLetter == 'i' ||
 aTheLetter == 'o' ||
 aTheLetter == 'u' ||
 aTheLetter == 'A' ||
 aTheLetter == 'E' ||
 aTheLetter == 'I' ||
 aTheLetter == 'O' ||
 aTheLetter == 'U'
 ) {
   return new Vowel(aTheLetter);
 } else {
   return new Consonant(aTheLetter);
 }
 }
 
 
 // Our new abstracted field. We'll make it protected so that subclasses can see it,
 // and we rename it from "i" to "f", following our naming convention.
 protected char fTheLetter;
 
 
 // Our new constructor. It can't actually be used to instantiate an instance
 // of Letter, but our sub classes can invoke it with super
 protected Letter(char aTheLetter) {
 this.fTheLetter = aTheLetter;
 }
 
 // The new method we're abstracting up to remove redundant code in the sub classes
 public char getChar() {
 return this.fTheLetter;
 }
 // Same old abstract methods that define part of our client facing interface
 public abstract boolean isVowel();
 
 public abstract boolean isConsonant();
 
 
 
 // The local subclasses with the redundant code moved up.
 
 private static class Vowel extends Letter {
 
 // Constructor delegates to the super constructor
 Vowel(char aTheLetter) {
   super(aTheLetter);
 }
 
 // Still need to implement the abstract methods
 public boolean isVowel()  {
   return true;
 }
 
 public boolean isConsonant(){
   return false;
 }
 } // End local class Vowel
 
 
 private static class Consonant extends Letter {
 
 Consonant(char aTheLetter){
   super(aTheLetter);
 }
 
 public boolean isVowel(){
   return false;
 }
 
 public boolean isConsonant(){
   return true;
 }
 } // End local class Consonant
 
} // End toplevel class Letter

Note that we made our abstracted field protected. This isn't strictly necessary in this case, we could have left it private, because the subclasses don't actually need to access it at all. In general, I prefer to make things protected instead of private, since, as I mentioned, you can never really be sure where a project will go in the future, and you may not want to restrict future implementors (including yourself) unnecessarily. However, many people prefer to default to private and only use protected when they know it's necessary. A major reason for this is the rather peculiar and somewhat unexpected meaning of protected in Java, which allows not only subclasses, but anything in the same package to access it. This is a bit of a digression, but I think it's a fairly important debate that a good Java programmer should be aware of.

The Factory Pattern and Parametric Polymorphism

The version of the Java Virtual Machine 5.0 has introduced something called Parametric Polymorphism, which goes by many other names in other languages, including "generic typing" in C++. In order to really understand the rest of this section, you should read that section first. But basically, this means that you can introduce additional parameters into a class--parameters which are set at instantiation--that define the types of certain elements in the class, for instance fields or method return values. This is a very powerful tool which allows programmers to avoid a lot of those nasty instanceofs and narrowing castes. However, the implementation of this device in the JVM does not promote the use of the Factory pattern, and in the two do not play well together. This is because Java does not allow methods to be parameterized the way types are, so you cannot dynamically parameterize an instance through a method, only through use of the new operator. As an example, imagine a type Foo which is parameterized with a single type which we'll call T. In java we would write this class like this:

class Foo<T> {
} // End class Foo

Now we can have instances of Foo parameterized by all sorts of types, for instance:

Foo<String> fooOverString = new Foo<String>();
Foo<Integer> fooOverInteger = new Foo<Integer>();

But let's say we want to use the factory pattern for Foo. How do we do that? You could create a different factory method for each type you want to parameterize over, for instance:

class Foo<T> {
  static Foo<String> getFooOverString() {
    return new Foo<String>();
  }
  static Foo<Integer> getFooOverInteger() {
    return new Foo<Integer>();
  }
} // End class Foo

But what about something like the ArrayList class (in the java.util package)? In the java standard libraries released with 5.0, ArrayList is parameterized to define the type of the object stored in it. We certainly don't want to restrict what kinds of types it can be parameterized with by having to write a factory method for each type. This is often the case with parameterized types: you don't know what types users will want to parameterize with, and you don't want to restrict them, so the factory pattern won't work for that. You are allowed to instantiate a parameterized type in a generic form, meaning you don't specify the parameter at all, you just instantiate it the way you would have before 5.0. But that forces you to give up the parameterization. This is how you do it with generics:

class Foo<T> {
  public static <E> Foo<E> getFoo() {
    return new Foo<E>();
  }
} // End class Foo

Examples

In Java, a class that implements java.sql.Connection is a factory of statements. By calling the createStatement() method, you create a statement for which you only know the interface. The factory chooses the right instance class for you.

Cost

This pattern is not so expensive when it is implemented at the right time. It can be more expensive if you have to refactor an existing code.

Creation

Its implementation is easy and there is no additional cost (it is not more expensive than an implementation without this pattern).

Maintenance

There is no additional cost nor additional constraint.

Removal

This pattern can be easily removed as automatic refactoring operations can easily remove its existence.

Advises

  • Put the factory term in the name of the factory class to indicate the use of the pattern to the other developers.

Implementation

Implementation in ABAP
REPORT zz_pizza_factory_test NO STANDARD PAGE HEADING .
TYPES ty_pizza_type TYPE i .
*----------------------------------------------------------------------*
*       CLASS lcl_pizza DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_pizza DEFINITION ABSTRACT .
  PUBLIC SECTION .
    DATA p_pizza_name TYPE string .
    METHODS get_price ABSTRACT
                      RETURNING value(y_price) TYPE i .
ENDCLASS .                    "lcl_pizza DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_ham_and_mushroom_pizza DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_ham_and_mushroom_pizza DEFINITION INHERITING FROM lcl_pizza .
  PUBLIC SECTION .
    METHODS constructor .
    METHODS get_price REDEFINITION .
ENDCLASS .                    "lcl_ham_and_mushroom_pizza DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_deluxe_pizza DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_deluxe_pizza DEFINITION INHERITING FROM lcl_pizza .
  PUBLIC SECTION .
    METHODS constructor .
    METHODS get_price REDEFINITION .
ENDCLASS .                    "lcl_ham_and_mushroom_pizza DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_hawaiian_pizza DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_hawaiian_pizza DEFINITION INHERITING FROM lcl_pizza .
  PUBLIC SECTION .
    METHODS constructor .
    METHODS get_price REDEFINITION .
ENDCLASS .                    "lcl_ham_and_mushroom_pizza DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_pizza_factory DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_pizza_factory DEFINITION .
  PUBLIC SECTION .
    CONSTANTS: BEGIN OF co_pizza_type ,
                 ham_mushroom  TYPE ty_pizza_type VALUE 1 ,
                 deluxe        TYPE ty_pizza_type VALUE 2 ,
                 hawaiian      TYPE ty_pizza_type VALUE 3 ,
               END OF co_pizza_type .
    CLASS-METHODS create_pizza IMPORTING  x_pizza_type TYPE ty_pizza_type
                               RETURNING value(yo_pizza) TYPE REF TO lcl_pizza
                               EXCEPTIONS ex_invalid_pizza_type .
ENDCLASS .                    "lcl_pizza_factory DEFINITION
*----------------------------------------------------------------------*
*       CLASS lcl_ham_and_mushroom_pizza
*----------------------------------------------------------------------*
CLASS lcl_ham_and_mushroom_pizza IMPLEMENTATION .
  METHOD constructor .
    super->constructor( ) .
    p_pizza_name = 'Ham & Mushroom Pizza'(001) .
  ENDMETHOD .                    "constructor
  METHOD get_price .
    y_price = 850 .
  ENDMETHOD .                    "get_price
ENDCLASS .                    "lcl_ham_and_mushroom_pizza IMPLEMENTATION
*----------------------------------------------------------------------*
*       CLASS lcl_deluxe_pizza IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_deluxe_pizza IMPLEMENTATION .
  METHOD constructor .
    super->constructor( ) .
    p_pizza_name = 'Deluxe Pizza'(002) .
  ENDMETHOD .                    "constructor
  METHOD get_price .
    y_price = 1050 .
  ENDMETHOD .                    "get_price
ENDCLASS .                    "lcl_deluxe_pizza IMPLEMENTATION
*----------------------------------------------------------------------*
*       CLASS lcl_hawaiian_pizza IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_hawaiian_pizza IMPLEMENTATION .
  METHOD constructor .
    super->constructor( ) .
    p_pizza_name = 'Hawaiian Pizza'(003) .
  ENDMETHOD .                    "constructor
  METHOD get_price .
    y_price = 1150 .
  ENDMETHOD .                    "get_price
ENDCLASS .                    "lcl_hawaiian_pizza IMPLEMENTATION
*----------------------------------------------------------------------*
*       CLASS lcl_pizza_factory IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_pizza_factory IMPLEMENTATION .
  METHOD create_pizza .
    CASE x_pizza_type .
      WHEN co_pizza_type-ham_mushroom .
        CREATE OBJECT yo_pizza TYPE lcl_ham_and_mushroom_pizza .
      WHEN co_pizza_type-deluxe .
        CREATE OBJECT yo_pizza TYPE lcl_deluxe_pizza .
      WHEN co_pizza_type-hawaiian .
        CREATE OBJECT yo_pizza TYPE lcl_hawaiian_pizza .
    ENDCASE .
  ENDMETHOD .                    "create_pizza
ENDCLASS .                    "lcl_pizza_factory IMPLEMENTATION
START-OF-SELECTION .
  DATA go_pizza TYPE REF TO lcl_pizza .
  DATA lv_price TYPE i .
  DO 3 TIMES .
    go_pizza = lcl_pizza_factory=>create_pizza( sy-index ) .
    lv_price = go_pizza->get_price( ) .
    WRITE:/ 'Price of', go_pizza->p_pizza_name, 'is £', lv_price LEFT-JUSTIFIED .
  ENDDO .
*Output:
*Price of Ham & Mushroom Pizza is £ 850
*Price of Deluxe Pizza is £ 1.050      
*Price of Hawaiian Pizza is £ 1.150
Implementation in ActionScript 3.0
public class Pizza
{
protected var _price:Number;
public function get price():Number
        {
return _price;
}
}
public class HamAndMushroomPizza extends Pizza
{
public function HamAndMushroomPizza()
        {
_price = 8.5;
}
}
public class DeluxePizza extends Pizza
{
public function DeluxePizza()
        {
_price = 10.5;
}
}
public class HawaiianPizza extends Pizza
{
public function HawaiianPizza()
        {
_price = 11.5;
}
}
public class PizzaFactory
{
static public function createPizza(type:String):Pizza
        {
switch (type)
                {
case "HamAndMushroomPizza":
return new HamAndMushroomPizza();
break;
case "DeluxePizza":
return new DeluxePizza();
break;
case "HawaiianPizza":
return new HawaiianPizza();
break;
default:
throw new ArgumentError("The pizza type " + type + " is not recognized.");
}
}
}
public class Main extends Sprite
{
public function Main()
        {
for each (var pizza:String in ["HamAndMushroomPizza", "DeluxePizza", "HawaiianPizza"])
                {
trace("Price of " + pizza + " is " + PizzaFactory.createPizza(pizza).price);
}
}
}
Output:
Price of HamAndMushroomPizza is 8.5
Price of DeluxePizza is 10.5
Price of HawaiianPizza is 11.5
Implementation in Common Lisp

In Common Lisp, factory methods are not really needed, because classes and class names are first class values.

(defclass pizza ()
  ((price :accessor price)))
(defclass ham-and-mushroom-pizza (pizza)
  ((price :initform 850)))
(defclass deluxe-pizza (pizza)
  ((price :initform 1050)))
(defclass hawaiian-pizza (pizza)
  ((price :initform 1150)))
(defparameter *pizza-types*
  (list 'ham-and-mushroom-pizza
        'deluxe-pizza
        'hawaiian-pizza))
(loop for pizza-type in *pizza-types*
      do (format t "~%Price of ~a is ~a"
                 pizza-type
                 (price (make-instance pizza-type))))
Output:
Price of HAM-AND-MUSHROOM-PIZZA is 850
Price of DELUXE-PIZZA is 1050
Price of HAWAIIAN-PIZZA is 1150
Implementation in Delphi
program FactoryMethod;
{$APPTYPE CONSOLE}
uses
  SysUtils;
type
// Product
TProduct = class(TObject)
public
  function GetName(): string; virtual; abstract;
end;
// ConcreteProductA
TConcreteProductA = class(TProduct)
public
  function GetName(): string; override;
end;
// ConcreteProductB
TConcreteProductB = class(TProduct)
public
  function GetName(): string; override;
end;
// Creator
TCreator = class(TObject)
public
  function FactoryMethod(): TProduct; virtual; abstract;
end;
// ConcreteCreatorA
TConcreteCreatorA = class(TCreator)
public
  function FactoryMethod(): TProduct; override;
end;
// ConcreteCreatorB
TConcreteCreatorB = class(TCreator)
public
  function FactoryMethod(): TProduct; override;
end;
{ ConcreteProductA }
function TConcreteProductA.GetName(): string;
begin
  Result := 'ConcreteProductA';
end;
{ ConcreteProductB }
function TConcreteProductB.GetName(): string;
begin
  Result := 'ConcreteProductB';
end;
{ ConcreteCreatorA }
function TConcreteCreatorA.FactoryMethod(): TProduct;
begin
  Result := TConcreteProductA.Create();
end;
{ ConcreteCreatorB }
function TConcreteCreatorB.FactoryMethod(): TProduct;
begin
  Result := TConcreteProductB.Create();
end;
const
  Count = 2;
var
  Creators: array[1..Count] of TCreator;
  Product: TProduct;
  I: Integer;
begin
  // An array of creators
  Creators[1] := TConcreteCreatorA.Create();
  Creators[2] := TConcreteCreatorB.Create();
  // Iterate over creators and create products
  for I := 1 to Count do
  begin
    Product := Creators[I].FactoryMethod();
    WriteLn(Product.GetName());
    Product.Free();
  end;
  for I := 1 to Count do
    Creators[I].Free();
  ReadLn;
end.
Implementation in Java

Example with pizza

abstract class Pizza {
    public abstract int getPrice(); // Count the cents
}
class HamAndMushroomPizza extends Pizza {
    public int getPrice() {
        return 850;
    }
}
class DeluxePizza extends Pizza {
    public int getPrice() {
        return 1050;
    }
}
class HawaiianPizza extends Pizza {
    public int getPrice() {
        return 1150;
    }
}
class PizzaFactory {
    public enum PizzaType {
        HamMushroom,
        Deluxe,
        Hawaiian
    }
    public static Pizza createPizza(PizzaType pizzaType) {
        switch (pizzaType) {
            case HamMushroom:
                return new HamAndMushroomPizza();
            case Deluxe:
                return new DeluxePizza();
            case Hawaiian:
                return new HawaiianPizza();
        }
        throw new IllegalArgumentException("The pizza type " + pizzaType + " is not recognized.");
    }
}
class PizzaLover {
    /**
     * Create all available pizzas and print their prices
     */
    public static void main (String args[]) {
        for (PizzaFactory.PizzaType pizzaType : PizzaFactory.PizzaType.values()) {
            System.out.println("Price of " + pizzaType + " is " + PizzaFactory.createPizza(pizzaType).getPrice());
        }
    }
}

Output:

Price of HamMushroom is 850
Price of Deluxe is 1050
Price of Hawaiian is 1150

Another example with image

Abstract creator
Interface to create the Product.
  1. package mypkg;
    
  2. import java.awt.image.BufferedImage;
    
  3. import java.io.IOException;
    
  4. /**
    
  5.  *
    
  6.  * @author xxx
    
  7.  */
    
  8. public interface PhotoReader {
    
  9.     public BufferedImage getImage() throws IOException;
    
  10. }
    
Concrete creator 
a class to create specific Product.
  1. package mypkg;
    
  2. import java.awt.image.BufferedImage;
    
  3. import java.io.File;
    
  4. import java.io.IOException;
    
  5. import java.util.Iterator;
    
  6. import javax.imageio.ImageIO;
    
  7. import javax.imageio.ImageReader;
    
  8. import javax.imageio.stream.ImageInputStream;
    
  9. /**
    
  10.  *
    
  11.  * @author xxx
    
  12.  */
    
  13. public class JPEGReader implements PhotoReader {
    
  14.     ImageReader reader;
    
  15.     File jpegFile;
    
  16.     ImageInputStream iis;
    
  17.     public JPEGReader(String filePath) throws IOException {
    
  18.         jpegFile = new File(filePath);
    
  19.         iis = ImageIO.createImageInputStream(jpegFile);
    
  20.         Iterator readers = ImageIO.getImageReadersByFormatName("jpg");
    
  21.         reader = (ImageReader)readers.next();
    
  22.         this.reader.setInput(iis, true);
    
  23.     }
    
  24.     public BufferedImage getImage() throws IOException {
    
  25.         return reader.read(0);
    
  26.     }
    
Factory class 
a class to return a specific concrete creator at runtime to create the product.
  1. package mypkg;
    
  2. import java.io.IOException;
    
  3. /**
    
  4.  *
    
  5.  * @author xxx
    
  6.  */
    
  7. public class PhotoReaderFactory {
    
  8.     enum Mimi {
    
  9.         jpg, JPG, gif, GIF, bmp, BMP, png, PNG
    
  10.     };
    
  11.     public static PhotoReader getPhotoReader(String filePath) {
    
  12.         String suffix = getFileSuffix(filePath);
    
  13.         PhotoReader reader = null;
    
  14.        try {
    
  15.         switch (Mimi.valueOf(suffix)) {
    
  16.             case jpg :
    
  17.             case JPG : reader = new JPEGReader(filePath); break;
    
  18.             case gif :
    
  19.             case GIF : reader = new GIFReader(filePath); break;
    
  20.             case bmp :
    
  21.             case BMP : reader = new BMPReader(filePath); break;
    
  22.             case png :
    
  23.             case PNG : reader = new PNGReader(filePath); break;
    
  24.             default : break;
    
  25.         }
    
  26.         } catch(IOException io) {
    
  27.             io.printStackTrace();
    
  28.         }        
    
  29.         return reader;
    
  30.     }
    
  31.     private static String getFileSuffix(String filePath) {
    
  32.         String[] stringArray = filePath.split("\\.");        
    
  33.         return stringArray[stringArray.length - 1];
    
  34.     }
    
  35. }
    
Implementation in Javascript

This example in JavaScript uses Firebug console to output information.

/**
 * Extends parent class with child. In Javascript, the keyword "extends" is not
 * currently implemented, so it must be emulated.
 * Also it is not recommended to use keywords for future use, so we name this
 * function "extends" with capital E. Javascript is case-sensitive.
 *
 * @param  function  parent constructor function
 * @param  function  (optional) used to override default child constructor function
 */
function Extends(parent, childConstructor) {
  var F = function () {};
  F.prototype = parent.prototype;
  var Child = childConstructor || function () {};
  Child.prototype = new F();
  Child.prototype.constructor = Child;
  Child.parent = parent.prototype;
  // return instance of new object
  return Child;
}
/**
 * Abstract Pizza object constructor
 */
function Pizza() {
  throw new Error('Cannot instatiate abstract object!');
}
Pizza.prototype.price = 0;
Pizza.prototype.getPrice = function () {
  return this.price;
}
var HamAndMushroomPizza = Extends(Pizza);
HamAndMushroomPizza.prototype.price = 8.5;
var DeluxePizza = Extends(Pizza);
DeluxePizza.prototype.price = 10.5;
var HawaiianPizza = Extends(Pizza);
HawaiianPizza.prototype.price = 11.5;
var PizzaFactory = {
  createPizza: function (type) {
    var baseObject = 'Pizza';
    var targetObject = type.charAt(0).toUpperCase() + type.substr(1);
    if (typeof window[targetObject + baseObject] === 'function') {
      return new window[targetObject + baseObject];
    }
    else {
      throw new Error('The pizza type ' + type + ' is not recognized.');
    }
  }
};
//var price = PizzaFactory.createPizza('deluxe').getPrice();
var pizzas = ['HamAndMushroom', 'Deluxe', 'Hawaiian'];
for (var i in pizzas) {
  console.log('Price of ' + pizzas[i] + ' is ' + PizzaFactory.createPizza(pizzas[i]).getPrice());
}

Output

Price of HamAndMushroom is 8.50
Price of Deluxe is 10.50
Price of Hawaiian is 11.50
Implementation in Haskell
class Pizza a where
  price :: a -> Float
data HamMushroom = HamMushroom
data Deluxe      = Deluxe    
data Hawaiian    = Hawaiian  
instance Pizza HamMushroom where
  price _ = 8.50
instance Pizza Deluxe where
  price _ = 10.50
instance Pizza Hawaiian where
  price _ = 11.50

Usage example:

main = print (price Hawaiian)
Implementation in Perl
package Pizza;
use Moose;
has price => (is => "rw", isa => "Num", builder => "_build_price" );
package HamAndMushroomPizza;
use Moose; extends "Pizza";
sub _build_price { 8.5 }
package DeluxePizza;
use Moose; extends "Pizza";
sub _build_price { 10.5 }
package HawaiianPizza;
use Moose; extends "Pizza";
sub _build_price { 11.5 }
package PizzaFactory;
sub create {
    my ( $self, $type ) = @_;
    return ($type . "Pizza")->new;
}
package main;
for my $type ( qw( HamAndMushroom Deluxe Hawaiian ) ) {
    printf "Price of %s is %.2f\n", $type, PizzaFactory->create( $type )->price;
}

Factories are not really needed for this example in Perl, and this may be written more concisely:

package Pizza;
use Moose;
has price => (is => "rw", isa => "Num", builder => "_build_price" );
package HamAndMushroomPizza;
use Moose; extends "Pizza";
sub _build_price { 8.5 }
package DeluxePizza;
use Moose; extends "Pizza";
sub _build_price { 10.5 }
package HawaiianPizza;
use Moose; extends "Pizza";
sub _build_price { 11.5 }
package main;
for my $type ( qw( HamAndMushroom Deluxe Hawaiian ) ) {
    printf "Price of %s is %.2f\n", $type, ($type . "Pizza")->new->price;
}

Output

Price of HamAndMushroom is 8.50
Price of Deluxe is 10.50
Price of Hawaiian is 11.50
Implementation in PHP
<?php
abstract class Pizza
{
    protected $_price;
    public function getPrice()
    {
        return $this->_price;
    }
}
 
class HamAndMushroomPizza extends Pizza
{
    protected $_price = 8.5;
}
 
class DeluxePizza extends Pizza
{
    protected $_price = 10.5;
}
 
class HawaiianPizza extends Pizza
{
    protected $_price = 11.5;
}
 
class PizzaFactory
{
    public static function createPizza($type)
    {
        $baseClass = 'Pizza';
        $targetClass = ucfirst($type).$baseClass;
 
        if (class_exists($targetClass) && is_subclass_of($targetClass, $baseClass))
            return new $targetClass;
        else
            throw new Exception("The pizza type '$type' is not recognized.");
    }
}
$pizzas = array('HamAndMushroom','Deluxe','Hawaiian');
foreach($pizzas as $p) {
    printf(
        "Price of %s is %01.2f".PHP_EOL ,
        $p ,
        PizzaFactory::createPizza($p)->getPrice()
    );
}
// Output:
// Price of HamAndMushroom is 8.50
// Price of Deluxe is 10.50
// Price of Hawaiian is 11.50
?>
Implementation in Python
#
# Pizza
#
class Pizza(object):
    def __init__(self):
        self._price = None
    def get_price(self):
        return self._price
class HamAndMushroomPizza(Pizza):
    def __init__(self):
        self._price = 8.5
class DeluxePizza(Pizza):
    def __init__(self):
        self._price = 10.5
class HawaiianPizza(Pizza):
    def __init__(self):
        self._price = 11.5
#
# PizzaFactory
#
class PizzaFactory(object):
    @staticmethod
    def create_pizza(pizza_type):
        if pizza_type == 'HamMushroom':
            return HamAndMushroomPizza()
        elif pizza_type == 'Deluxe':
            return DeluxePizza()
        elif pizza_type == 'Hawaiian':
            return HawaiianPizza()
if __name__ == '__main__':
    for pizza_type in ('HamMushroom', 'Deluxe', 'Hawaiian'):
        print 'Price of {0} is {1}'.format(pizza_type, PizzaFactory.create_pizza(pizza_type).get_price())

As in Perl, Common Lisp and other dynamic languages, factories of the above sort aren't really necessary, since classes are first-class objects and can be passed around directly, leading to this more natural version:

#
# Pizza
#
class Pizza(object):
    def __init__(self):
        self._price = None
    def get_price(self):
        return self._price
class HamAndMushroomPizza(Pizza):
    def __init__(self):
        self._price = 8.5
class DeluxePizza(Pizza):
    def __init__(self):
        self._price = 10.5
class HawaiianPizza(Pizza):
    def __init__(self):
        self._price = 11.5
if __name__ == '__main__':
    for pizza_class in (HamAndMushroomPizza, DeluxePizza, HawaiianPizza):
        print 'Price of {0} is {1}'.format(pizza_class.__name__, pizza_class().get_price())

Note in the above that the classes themselves are simply used as values, which pizza_class iterates over. The class gets created simply by treating it as a function. In this case, if pizza_class holds a class, then pizza_class() creates a new object of that class. Another way of writing the final clause, which sticks more closely to the original example and uses strings instead of class objects, is as follows:

if __name__ == '__main__':
    for pizza_type in ('HamAndMushroom', 'Deluxe', 'Hawaiian'):
        print 'Price of {0} is {1}'.format(pizza_type, eval(pizza_type + 'Pizza')().get_price())

In this case, the correct class name is constructed as a string by adding 'Pizza', and eval is called to turn it into a class object.

Implementation in VB.NET
Imports System
Namespace FactoryMethodPattern
  Public Class Program
    Shared Sub Main()
      OutputPizzaFactory(New LousPizzaStore())
      OutputPizzaFactory(New TonysPizzaStore())
      Console.ReadKey()
    End Sub
    Private Shared Sub OutputPizzaFactory(ByVal factory As IPizzaFactory)
      Console.WriteLine("Welcome to {0}", factory.Name)
      For Each p As Pizza In factory.CreatePizzas
        Console.WriteLine("  {0} - ${1} - {2}", p.GetType().Name, p.Price, p.Toppings)
      Next
    End Sub
  End Class
  Public MustInherit Class Pizza
    Protected _toppings As String
    Protected _price As Decimal
    Public ReadOnly Property Toppings() As String
      Get
        Return _toppings
      End Get
    End Property
    Public ReadOnly Property Price() As Decimal
      Get
        Return _price
      End Get
    End Property
    Public Sub New(ByVal __price As Decimal)
      _price = __price
    End Sub
  End Class
  Public Interface IPizzaFactory
    ReadOnly Property Name() As String
    Function CreatePizzas() As Pizza()
  End Interface
  Public Class Pepperoni
    Inherits Pizza
    Public Sub New(ByVal price As Decimal)
      MyBase.New(price)
      _toppings = "Cheese, Pepperoni"
    End Sub
  End Class
  Public Class Cheese
    Inherits Pizza
    Public Sub New(ByVal price As Decimal)
      MyBase.New(price)
      _toppings = "Cheese"
    End Sub
  End Class
  Public Class LousSpecial
    Inherits Pizza
    Public Sub New(ByVal price As Decimal)
      MyBase.New(price)
      _toppings = "Cheese, Pepperoni, Ham, Lou's Special Sauce"
    End Sub
  End Class
  Public Class TonysSpecial
    Inherits Pizza
    Public Sub New(ByVal price As Decimal)
      MyBase.New(price)
      _toppings = "Cheese, Bacon, Tomatoes, Tony's Special Sauce"
    End Sub
  End Class
  Public Class LousPizzaStore
    Implements IPizzaFactory
    Public Function CreatePizzas() As Pizza() Implements IPizzaFactory.CreatePizzas
      Return New Pizza() {New Pepperoni(6.99D), New Cheese(5.99D), New LousSpecial(7.99D)}
    End Function
    Public ReadOnly Property Name() As String Implements IPizzaFactory.Name
      Get
        Return "Lou's Pizza Store"
      End Get
    End Property
  End Class
  Public Class TonysPizzaStore
    Implements IPizzaFactory
    Public Function CreatePizzas() As Pizza() Implements IPizzaFactory.CreatePizzas
      Return New Pizza() {New Pepperoni(6.5D), New Cheese(5.5D), New TonysSpecial(7.5D)}
    End Function
    Public ReadOnly Property Name() As String Implements IPizzaFactory.Name
      Get
        Return "Tony's Pizza Store"
      End Get
    End Property
  End Class
End Namespace
Output:
Welcome to Lou's Pizza Store
  Pepperoni - $6.99 - Cheese, Pepperoni
  Cheese - $5.99 - Cheese
  LousSpecial - $7.99 - Cheese, Pepperoni, Ham, Lou's Special Sauce
Welcome to Tony's Pizza Store
  Pepperoni - $6.5 - Cheese, Pepperoni
  Cheese - $5.5 - Cheese
  TonysSpecial - $7.5 - Cheese, Bacon, Tomatoes, Tony's Special Sauce

Flyweight

It is a mechanism by which you can avoid creating a large number of object instances to represent the entire system. To decide if some part of a program is a candidate for using Flyweights, consider whether it is possible to remove some data from the class and make it extrinsic.

Examples

Libreoffice writer nice pic of ui.png

A classic example usage of the flyweight pattern is the data structures for graphical representation of characters in a word processor. It might be desirable to have, for each character in a document, a glyph object containing its font outline, font metrics, and other formatting data, but this would amount to hundreds or thousands of bytes for each character. Instead, for every character there might be a reference to a flyweight glyph object shared by every instance of the same character in the document; only the position of each character (in the document and/or the page) would need to be stored internally. Another example is string interning.

Game Cloud Screenshot.png

In video games, it is usual that you have to display the same sprite (i.e. an image of an item of the game) several times. It would highly use the CPU and the memory if each sprite was a different object. So the sprite is created once and then is rendered at different locations in the screen. This problem can be solved using the flyweight pattern. The object that renders the sprite is a flyweight.

Cost

There are several implementations for this pattern. So it's up to you to find a cheap implementation. Only implement this pattern if you have or will have CPU or memory issues.

Creation

This pattern is quite easy to create.

Maintenance

This pattern is quite easy to maintain.

Removal

This pattern is quite easy to remove too.

Advises

  • Use pre-existing tools from the language like the sets in Java.

Implementations

Implementation in Java

The following programs illustrate the document example given above: the flyweights are called FontData in the Java example. The examples illustrate the flyweight pattern used to reduce memory by loading only the data necessary to perform some immediate task from a large Font object into a much smaller FontData (flyweight) object.

import java.lang.ref.WeakReference;
import java.util.WeakHashMap;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;
import java.awt.Color;
public final class FontData {
    enum FontEffect {
        BOLD, ITALIC, SUPERSCRIPT, SUBSCRIPT, STRIKETHROUGH
    }
    /**
     * A weak hash map will drop unused references to FontData.
     * Values have to be wrapped in WeakReferences, 
     * because value objects in weak hash map are held by strong references.
     */
    private static final WeakHashMap<FontData, WeakReference<FontData>> FLY_WEIGHT_DATA =
        new WeakHashMap<FontData, WeakReference<FontData>>();
    private final int pointSize;
    private final String fontFace;
    private final Color color;
    private final Set<FontEffect> effects;
    private FontData(int pointSize, String fontFace, Color color, EnumSet<FontEffect> effects) {
        this.pointSize = pointSize;
        this.fontFace = fontFace;
        this.color = color;
        this.effects = Collections.unmodifiableSet(effects);
    }
    public static FontData create(int pointSize, String fontFace, Color color,
        FontEffect... effects) {
        EnumSet<FontEffect> effectsSet = EnumSet.noneOf(FontEffect.class);
        for (FontEffect fontEffect : effects) {
            effectsSet.add(fontEffect);
        }
        // We are unconcerned with object creation cost, we are reducing overall memory consumption
        FontData data = new FontData(pointSize, fontFace, color, effectsSet);
        FontData result = null;
        // Retrieve previously created instance with the given values if it (still) exists
        WeakReference<FontData> ref = FLY_WEIGHT_DATA.get(data);
        if (ref != null) {
            result = ref.get();
        }
        // Store new font data instance if no matching instance exists
        if(result == null){      	
            FLY_WEIGHT_DATA.put(data, new WeakReference<FontData> (data));
            result = data;
        }
        // return the single immutable copy with the given values
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof FontData) {
            if (obj == this) {
                return true;
            }
            FontData other = (FontData) obj;
            return other.pointSize == pointSize && other.fontFace.equals(fontFace)
                && other.color.equals(color) && other.effects.equals(effects);
        }
        return false;
    }
    @Override
    public int hashCode() {
        return (pointSize * 37 + effects.hashCode() * 13) * fontFace.hashCode();
    }
    // Getters for the font data, but no setters. FontData is immutable.
}

Interpreter

Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

Examples

The following Reverse Polish notation example illustrates the interpreter pattern. The grammar: expression ::= plus | minus | variable | number
plus ::= expression expression '+'
minus ::= expression expression '-'
variable  ::= 'a' | 'b' | 'c' | ... | 'z'
digit ::= '0' | '1' | ... '9'
number ::= digit | digit number

defines a language which contains reverse Polish expressions like:

a b +
a b c + -
a b + c a - -

Following the interpreter pattern there is a class for each grammar rule.

Cost

This pattern is not expensive. It dramatically reduces the business code so there is only few code to handle.

Creation

If the code already exists, this pattern is a little expensive.

Maintenance

This pattern is very easy to maintain. There is no additional cost due to the pattern.

Removal

This pattern is easy to remove with refactoring operations from your IDE.

Advises

  • Use the Interpreter term to indicate the use of the pattern to the other developers.

Implementations

Implementation in C#
using System;
using System.Collections.Generic;
 
namespace Interpreter
{
    class Program
    {
        interface IExpression
        {
            int Interpret(Dictionary<string, int> variables);
        }
 
        class Number : IExpression
        {
            public int number;
            public Number(int number) { this.number = number; }
            public int Interpret(Dictionary<string, int> variables) { return number; }
        }
 
        abstract class BasicOperation : IExpression
        {
            IExpression leftOperator, rightOperator;
 
            public BasicOperation(IExpression left, IExpression right)
            {
                leftOperator = left;
                rightOperator = right;
            }
 
            public int Interpret(Dictionary<string, int> variables)
            {
                return Execute(leftOperator.Interpret(variables), rightOperator.Interpret(variables));
            }
 
            abstract protected int Execute(int left, int right);
        }
 
        class Plus : BasicOperation
        {
            public Plus(IExpression left, IExpression right) : base(left, right) { }
 
            protected override int Execute(int left, int right)
            {
                return left + right;
            }
        }
 
        class Minus : BasicOperation
        {
            public Minus(IExpression left, IExpression right) : base(left, right) { }
 
            protected override int Execute(int left, int right)
            {
                return left - right;
            }
        }
 
        class Variable : IExpression
        {
            private string name;
 
            public Variable(string name) { this.name = name; }
 
            public int Interpret(Dictionary<string, int> variables)
            {
                return variables[name];
            }
        }
 
        class Evaluator
        {
            private IExpression syntaxTree;
 
            public Evaluator(string expression)
            {
                Stack<IExpression> stack = new Stack<IExpression>();
                foreach (string token in expression.Split(' '))
                {
                    if (token.Equals("+"))
                        stack.Push(new Plus(stack.Pop(), stack.Pop()));
                    else if (token.Equals("-")){
                        IExpression right = stack.Pop();
                        IExpression left = stack.Pop();
                        stack.Push(new Minus(left, right));
                    }else
                        stack.Push(new Variable(token));
                }
                syntaxTree = stack.Pop();
            }
 
            public int Evaluate(Dictionary<string, int> context)
            {
                return syntaxTree.Interpret(context);
            }
        }
 
        static void Main(string[] args)
        {
            Evaluator evaluator = new Evaluator("w x z - +");
            Dictionary<string, int> values = new Dictionary<string,int>();
            values.Add("w", 5);
            values.Add("x", 10);
            values.Add("z", 42);
            Console.WriteLine(evaluator.Evaluate(values));
        }
    }
}
Implementation in Java
import java.util.Map;
interface Expression {
    public int interpret(Map<String, Expression> variables);
}
import java.util.Map;
class Number implements Expression {
    private int number;
    public Number(int number) {
        this.number = number;
    }
    public int interpret(Map<String, Expression> variables) {
        return number;
    }
}
import java.util.Map;
class Plus implements Expression {
    Expression leftOperand;
    Expression rightOperand;
    public Plus(Expression left, Expression right) {
        leftOperand = left;
        rightOperand = right;
    }
    public int interpret(Map<String, Expression> variables) {
        return leftOperand.interpret(variables) + rightOperand.interpret(variables);
    }
}
import java.util.Map;
class Minus implements Expression {
    Expression leftOperand;
    Expression rightOperand;
    public Minus(Expression left, Expression right) {
        leftOperand = left;
        rightOperand = right;
    }
    public int interpret(Map<String, Expression> variables)  {
        return leftOperand.interpret(variables) - rightOperand.interpret(variables);
    }
}
import java.util.Map;
class Variable implements Expression {
    private String name;
    public Variable(String name) {
        this.name = name;
    }
    public int interpret(Map<String, Expression> variables) {
        if (variables.get(name) == null) {
            // Either return new Number(0).
            return 0;
        } else {
            return variables.get(name).interpret(variables);
        }
    }
}

While the interpreter pattern does not address parsing a parser is provided for completeness.

import java.util.Map;
import java.util.Stack;
class Evaluator implements Expression {
    private Expression syntaxTree;
 
    public Evaluator(String expression) {
        Stack<Expression> expressionStack = new Stack<Expression>();
        for (String token : expression.split(" ")) {
            if  (token.equals("+")) {
                Expression subExpression = new Plus(expressionStack.pop(), expressionStack.pop());
                expressionStack.push( subExpression );
            }
            else if (token.equals("-")) {
                // it's necessary remove first the right operand from the stack
                Expression right = expressionStack.pop();
                // ..and after the left one
                Expression left = expressionStack.pop();
                Expression subExpression = new Minus(left, right);
                expressionStack.push( subExpression );
            }
            else                        
                expressionStack.push( new Variable(token) );
        }
        syntaxTree = expressionStack.pop();
    }
 
    public int interpret(Map<String,Expression> context) {
        return syntaxTree.interpret(context);
    }
}

Finally evaluating the expression "w x z - +" with w = 5, x = 10, and z = 42.

import java.util.Map;
import java.util.HashMap;
public class InterpreterExample {
    public static void main(String[] args) {
        String expression = "w x z - +";
        Evaluator sentence = new Evaluator(expression);
        Map<String,Expression> variables = new HashMap<String,Expression>();
        variables.put("w", new Number(5));
        variables.put("x", new Number(10));
        variables.put("z", new Number(42));
        int result = sentence.interpret(variables);
        System.out.println(result);
    }
}
Implementation in PHP

In a file we have the classes and the interface, defining the logic of the program (and applying the Interpreter pattern). Now the expr.php file:

<?php
    interface expression{
        public function interpret (array $variables);
        public function __toString();
    }
    class number implements expression{
        private $number;
        public function __construct($number){
            $this->number = intval($number);
        }
        public function interpret(array $variables){
            return $this->number;
        }
        public function __toString(){
            return (string) $this->number;
        }
    }
    class plus implements expression{
        private $left_op;
        private $right_op;
        public function __construct(expression $left, expression $right){
            $this->left_op = $left;
            $this->right_op = $right;
        }
        public function interpret(array $variables){
            return ($this->left_op->interpret($variables) + $this->right_op->interpret($variables));
        }
        public function __toString(){
            return (string) ("Left op: {$this->left_op} + Right op: {$this->right_op}\n");
        }
    }
    class minus implements expression{
        private $left_op;
        private $right_op;
        public function __construct(expression $left, expression $right){
            $this->left_op = $left;
            $this->right_op = $right;
        }
        public function interpret(array $variables){
            return ($this->left_op->interpret($variables) - $this->right_op->interpret($variables));
        }
        public function __toString(){
            return (string) ("Left op: {$this->left_op} - Right op: {$this->right_op}\n");
        }
    }
    class variable implements expression{
        private $name;
        public function __construct($name){
            $this->name = $name;
        }
        public function interpret(array $variables){
            if(!isset($variables[$this->name]))
                return 0;
            return $variables[$this->name]->interpret($variables);
        }
        public function __toString(){
            return (string) $this->name;
        }
    }
?>

And the evaluate.php:

<?php
    require_once('expr.php');
    class evaluator implements expression{
        private $syntaxTree;
        public function __construct($expression){
            $stack = array();
            $tokens = explode(" ", $expression);
            foreach($tokens as $token){
                if($token == "+"){
                    $right = array_pop($stack);
                    $left = array_pop($stack);
                    array_push($stack, new plus($left, $right));
                }
                else if($token == "-"){
                    $right = array_pop($stack);
                    $left = array_pop($stack);
                    array_push($stack, new minus($left, $right));
                }else if(is_numeric($token)){
                    array_push($stack, new number($token));
                }else{
                    array_push($stack, new variable($token));
                }
            }
            $this->syntaxTree = array_pop($stack);
        }
        public function interpret(array $context){
            return $this->syntaxTree->interpret($context);
        }
        public function __toString(){
            return "";
        }
    }
    // main code
    // works for it:
    $expression = "5 10 42 - +";
    // or for it:
    //$expression = "w x z - +";
    $variables = array();
    $variables['w'] = new number("5");
    $variables['x'] = new number("10");
    $variables['z'] = new number("42");
    print ("Evaluating expression {$expression}\n");
    $sentence = new evaluator($expression);
    $result = $sentence->interpret($variables);
    print $result . "\n";
?>

And you can run on a terminal by typing php5 -f evaluator.php (or putting it on a web application).

Iterator

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Examples

In Java, the interface java.util.Iterator<E> is an implementation of the iterator pattern. That way, all the objects that implement the java.lang.Iterable<T> interface don't need a handy implementation of this pattern.

Cost

This pattern has a cost. Only implement this pattern for an important amount of code. IDE refactoring can't help you much.

Creation

This pattern has a cost to create.

Maintenance

This pattern is easy to maintain.

Removal

This pattern has a cost to remove too.

Advises

  • Put the iterator term in the name of the iterator class to indicate the use of the pattern to the other developers.

Implementations

Implementation in Java

Here is an example in Java:

import java.util.BitSet;
import java.util.Iterator;
public class BitSetIterator implements Iterator<Boolean> {
    private final BitSet bitset;
    private int index = 0;
    public BitSetIterator(BitSet bitset) {
        this.bitset = bitset;
    }
    public boolean hasNext() {  
        return index < bitset.length();
    }
    public Boolean next() {
        if (index >= bitset.length()) {
            throw new NoSuchElementException();
        }
        return bitset.get(index++);
    }
    public void remove() {
        throw new UnsupportedOperationException();
    }
}

Two different usage examples:

import java.util.BitSet;
public class TestClientBitSet {
    public static void main(String[] args) {
        // create BitSet and set some bits
        BitSet bitset = new BitSet();
        bitset.set(1);
        bitset.set(3400);
        bitset.set(20);
        bitset.set(47);
        for (BitSetIterator iter = new BitSetIterator(bitset); iter.hasNext(); ) {
            Boolean b = iter.next();              
            String tf = (b.booleanValue() ? "T" : "F");
            System.out.print(tf);
        }
        System.out.println();
    }
}
import java.util.ArrayList;
import java.util.Collection;
public class TestClientIterator  {
    public static void main(String[] args) {
        Collection<Object> al = new ArrayList<Object>();
        al.add(new Integer(42));
        al.add("test");
        al.add(new Double("-12.34"));
        for (Iterator<Object> iter = al.iterator(); iter.hasNext(); ) {
            System.out.println(iter.next());
        }
        for (Object o : al) {
            System.out.println(o);
        }
    }
}
Implementation in C#

Here is an example in C#:

using System;
using System.Collections;
  class MainApp
  {
    static void Main()
    {
      ConcreteAggregate a = new ConcreteAggregate();
      a[0] = "Item A";
      a[1] = "Item B";
      a[2] = "Item C";
      a[3] = "Item D";
      // Create Iterator and provide aggregate
      ConcreteIterator i = new ConcreteIterator(a);
      Console.WriteLine("Iterating over collection:");
 
      object item = i.First();
      while (item != null)
      {
        Console.WriteLine(item);
        item = i.Next();
      }
      // Wait for user
      Console.Read();
    }
  }
  // "Aggregate"
  abstract class Aggregate
  {
    public abstract Iterator CreateIterator();
  }
  // "ConcreteAggregate"
  class ConcreteAggregate : Aggregate
  {
    private ArrayList items = new ArrayList();
    public override Iterator CreateIterator()
    {
      return new ConcreteIterator(this);
    }
    // Property
    public int Count
    {
      get{ return items.Count; }
    }
    // Indexer
    public object this[int index]
    {
      get{ return items[index]; }
      set{ items.Insert(index, value); }
    }
  }
  // "Iterator"
  abstract class Iterator
  {
    public abstract object First();
    public abstract object Next();
    public abstract bool IsDone();
    public abstract object CurrentItem();
  }
  // "ConcreteIterator"
  class ConcreteIterator : Iterator
  {
    private ConcreteAggregate aggregate;
    private int current = 0;
    // Constructor
    public ConcreteIterator(ConcreteAggregate aggregate)
    {
      this.aggregate = aggregate;
    }
    public override object First()
    {
      return aggregate[0];
    }
    public override object Next()
    {
      object ret = null;
      if (current < aggregate.Count - 1)
      {
        ret = aggregate[++current];
      }
 
      return ret;
    }
    public override object CurrentItem()
    {
      return aggregate[current];
    }
    public override bool IsDone()
    {
      return current >= aggregate.Count ? true : false ;
    }
  }
Implementation in PHP 5

As a default behavior in PHP 5, using an object in a foreach structure will traverse all public values. Multiple Iterator classes are available with PHP to allow you to iterate through common lists, such as directories, XML structures and recursive arrays. It's possible to define your own Iterator classes by implementing the Iterator interface, which will override the default behavior. The Iterator interface definition:

interface Iterator
{
    // Returns the current value
    function current();
 
    // Returns the current key
    function key();
    // Moves the internal pointer to the next element
    function next();
 
    // Moves the internal pointer to the first element
    function rewind();
 
    // If the current element is valid (boolean)
    function valid();
}

These methods are all being used in a complete foreach( $object as $key=>$value ) sequence. The methods are executed in the following order:

rewind() 
while valid() {
    current() in $value 
    key() in $key 
    next()
} 
End of Loop

According to Zend, the current() method is called before and after the valid() method.

Implementation in Perl

In Perl, objects providing an iterator interface either overload the <> (iterator operator)[1], or provide a hash or tied hash interface that can be iterated over with each[2]. Both <> and each return undef when iteration is complete. Overloaded <> operator:

# fibonacci sequence
package FibIter;
use overload
    '<>' => 'next_fib';
sub new {
    my $class = shift;
    bless { index => 0, values => [0, 1] }, $class
}
sub next_fib {
    my $self = shift;
    my $i = $self->{index}++;
    $self->{values}[$i] ||=
        $i > 1 ? $self->{values}[-2]+$self->{values}[-1]
               : ($self->{values}[$i]);
}
# reset iterator index
sub reset {
    my $self = shift;
    $self->{index} = 0
}
package main;
my $iter = FibIter->new;
while (my $fib = <$iter>) {
    print "$fib","\n";
}

Iterating over a hash (or tied hash):

# read from a file like a hash
package HashIter;
use base 'Tie::Hash';
sub new {
    my ($class, $fname) = @_;
    my $obj = bless {}, $class;
    tie %$obj, $class, $fname;
    bless $obj, $class;
}
 
sub TIEHASH {
    # tie hash to a file
    my $class = shift;
    my $fname = shift or die 'Need filename';
    die "File $fname must exist"
         unless [-f $fname];
    open my $fh, '<', $fname or die "open $!";
    bless { fname => $fname, fh => $fh }, $class;
}
sub FIRSTKEY {
    # (re)start iterator 
    my $self = shift;
    my $fh = $self->{fh};
    if (not fileno $self->{fh}) {
        open $fh, '<', $self->{fname} or die "open $!";
    }
    # reset file pointer
    seek( $fh, 0, 0 );
    chomp(my $line = <$fh>);
    $line
}
sub NEXTKEY {
    # next item from iterator
    my $self = shift;
    my $fh = $self->{fh};
    return if eof($fh);
    chomp(my $line = <$fh>);
    $line
}
sub FETCH {
    # get value for key, in this case we don't
    # care about the values, just return 
    my ($self, $key) = @_;  
    return
}
sub main;
# iterator over a word file
my $word_iter = HashIter->new('/usr/share/dict/words');
# iterate until we get to abacus
while (my $word = each( %$word_iter )) {
    print "$word\n";
    last if $word eq 'abacus'
}
# call keys %tiedhash in void context to reset iterator
keys %$word_iter;
Implementation in Python

In Python, iterators are objects that adhere to the iterator protocol. You can get an iterator from any sequence (i.e. collection: lists, tuples, dictionaries, sets, etc.) with the iter() method. Another way to get an iterator is to create a generator, which is a kind of iterator. To get the next element from an iterator, you use the next() method (Python 2) / next() function (Python 3). When there are no more elements, it raises the StopIteration exception. To implement your own iterator, you just need an object that implements the next() method (Python 2) / __next__() method (Python 3). Here are two use cases:

# from a sequence
x = [42, "test", -12.34]
it = iter(x)
try:
  while True:
    x = next(it) # in Python 2, you would use it.next()
    print x
except StopIteration:
  pass
# a generator
def foo(n):
  for i in range(n):
    yield i
it = foo(5)
try:
  while True:
    x = next(it) # in Python 2, you would use it.next()
    print x
except StopIteration:
  pass
Implementation in MATLAB

MATLAB supports both external and internal implicit iteration using either "native" arrays or cell arrays. In the case of external iteration where the onus is on the user to advance the traversal and request next elements, one can define a set of elements within an array storage structure and traverse the elements using the for-loop construct. For example,

% Define an array of integers
myArray = [1,3,5,7,11,13];
for n = myArray
   % ... do something with n
   disp(n)  % Echo integer to Command Window
end

traverses an array of integers using the for keyword. In the case of internal iteration where the user can supply an operation to the iterator to perform over every element of a collection, many built-in operators and MATLAB functions are overloaded to execute over every element of an array and return a corresponding output array implicitly. Furthermore, the arrayfun and cellfun functions can be leveraged for performing custom or user defined operations over "native" arrays and cell arrays respectively. For example,

function simpleFun
% Define an array of integers
myArray = [1,3,5,7,11,13];
% Perform a custom operation over each element
myNewArray = arrayfun(@(a)myCustomFun(a),myArray);
 
% Echo resulting array to Command Window          
myNewArray
 
 
function outScalar = myCustomFun(inScalar)
% Simply multiply by 2
outScalar = 2*inScalar;

defines a primary function simpleFun which implicitly applies custom subfunction myCustomFun to each element of an array using built-in function arrayfun.

Alternatively, it may be desirable to abstract the mechanisms of the array storage container from the user by defining a custom object-oriented MATLAB implementation of the Iterator Pattern. Such an implementation supporting external iteration is demonstrated in MATLAB Central File Exchange item Design Pattern: Iterator (Behavioural). This is written in the new class-definition syntax introduced with MATLAB software version 7.6 (R2008a) [3] and features a one-dimensional cell array realisation of the List Abstract Data Type (ADT) as the mechanism for storing a heterogeneous (in data type) set of elements. It provides the functionality for explicit forward List traversal with the hasNext(), next() and reset() methods for use in a while-loop.

References

  1. File handle objects implement this to provide line by line reading of their contents
  2. In Perl 5.12, arrays and tied arrays can be iterated over like hashes, with each
  3. "New Class-Definition Syntax Introduced with MATLAB Software Version 7.6". The MathWorks, Inc. March 2009. http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brqzfth-1.html#brqzfth-3. Retrieved September 22, 2009. 

Mediator

This pattern defines an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. This pattern helps to model a class whose object at run-time is responsible for controlling and coordinating the interactions of a group of other objects.

Participants

Mediator — defines the interface for communication between Colleague objects. ConcreteMediator — implements the Mediator interface and coordinates communication between Colleague objects. It is aware of all the Colleagues and their purpose with regards to inter communication. ConcreteColleague — communicates with other Colleagues through its Mediator.

Mediator design pattern.png

Examples

Cost

This pattern is not expensive because there is not much constraint on it.

Creation

This pattern is easy to create.

Maintenance

This pattern is easy to maintain.

Removal

Each object can easily be transformed to another structure using refactoring.

Advises

  • Put the mediator term in the name of the iterator class to indicate the use of the pattern to the other developers.

Implementations

Implementation in Java
// Colleague interface
interface Command {
    void execute();
}
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
// Concrete mediator
class Mediator {
    BtnView btnView;
    BtnSearch btnSearch;
    BtnBook btnBook;
    LblDisplay show;
    //....
    void registerView(BtnView v) {
        btnView = v;
    }
    void registerSearch(BtnSearch s) {
        btnSearch = s;
    }
    void registerBook(BtnBook b) {
        btnBook = b;
    }
    void registerDisplay(LblDisplay d) {
        show = d;
    }
    void book() {
        btnBook.setEnabled(false);
        btnView.setEnabled(true);
        btnSearch.setEnabled(true);
        show.setText("booking...");
    }
    void view() {
        btnView.setEnabled(false);
        btnSearch.setEnabled(true);
        btnBook.setEnabled(true);
        show.setText("viewing...");
    }
    void search() {
        btnSearch.setEnabled(false);
        btnView.setEnabled(true);
        btnBook.setEnabled(true);
        show.setText("searching...");
    }
 
}
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
// A concrete colleague
class BtnView extends JButton implements Command {
    Mediator med;
    BtnView(ActionListener al, Mediator m) {
        super("View");
        addActionListener(al);
        med = m;
        med.registerView(this);
    }
    public void execute() {
        med.view();
    }
 
}
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
// A concrete colleague
class BtnSearch extends JButton implements Command {
    Mediator med;
    BtnSearch(ActionListener al, Mediator m) {
        super("Search");
        addActionListener(al);
        med = m;
        med.registerSearch(this);
    }
    public void execute() {
        med.search();
    }
 
}
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
// A concrete colleague
class BtnBook extends JButton implements Command {
    Mediator med;
    BtnBook(ActionListener al, Mediator m) {
        super("Book");
        addActionListener(al);
        med = m;
        med.registerBook(this);
    }
    public void execute() {
        med.book();
    }
}
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class LblDisplay extends JLabel {
    Mediator med;
    LblDisplay(Mediator m) {
        super("Just start...");
        med = m;
        med.registerDisplay(this);
        setFont(new Font("Arial", Font.BOLD, 24));
    }
}
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class MediatorDemo extends JFrame implements ActionListener {
    Mediator med = new Mediator();
    MediatorDemo() {
        JPanel p = new JPanel();
        p.add(new BtnView(this, med));
        p.add(new BtnBook(this, med));
        p.add(new BtnSearch(this, med));
        getContentPane().add(new LblDisplay(med), "North");
        getContentPane().add(p, "South");
        setSize(400, 200);
        setVisible(true);
    }
    public void actionPerformed(ActionEvent ae) {
        Command comd = (Command) ae.getSource();
        comd.execute();
    }
    public static void main(String[] args) {
        new MediatorDemo();
    }
}
Implementation in C#
using System;
using System.Collections;
  class MainApp
  {
    static void Main()
    {
      ConcreteMediator m = new ConcreteMediator();
      ConcreteColleague1 c1 = new ConcreteColleague1(m);
      ConcreteColleague2 c2 = new ConcreteColleague2(m);
      m.Colleague1 = c1;
      m.Colleague2 = c2;
      c1.Send("How are you?");
      c2.Send("Fine, thanks");
      // Wait for user
      Console.Read();
    }
  }
  // "Mediator"
  abstract class Mediator
  {
    public abstract void Send(string message,
      Colleague colleague);
  }
  // "ConcreteMediator"
  class ConcreteMediator : Mediator
  {
    private ConcreteColleague1 colleague1;
    private ConcreteColleague2 colleague2;
    public ConcreteColleague1 Colleague1
    {
      set{ colleague1 = value; }
    }
    public ConcreteColleague2 Colleague2
    {
      set{ colleague2 = value; }
    }
    public override void Send(string message,
      Colleague colleague)
    {
      if (colleague == colleague1)
      {
        colleague2.Notify(message);
      }
      else
      {
        colleague1.Notify(message);
      }
    }
  }
  // "Colleague"
  abstract class Colleague
  {
    protected Mediator mediator;
    // Constructor
    public Colleague(Mediator mediator)
    {
      this.mediator = mediator;
    }
  }
  // "ConcreteColleague1"
  class ConcreteColleague1 : Colleague
  {
    // Constructor
    public ConcreteColleague1(Mediator mediator)
      : base(mediator)
    {
    }
    public void Send(string message)
    {
      mediator.Send(message, this);
    }
    public void Notify(string message)
    {
      Console.WriteLine("Colleague1 gets message: "
        + message);
    }
  }
  // "ConcreteColleague2"
  class ConcreteColleague2 : Colleague
  {
    // Constructor
    public ConcreteColleague2(Mediator mediator)
      : base(mediator)
    {
    }
    public void Send(string message)
    {
      mediator.Send(message, this);
    }
    public void Notify(string message)
    {
      Console.WriteLine("Colleague2 gets message: "
        + message);
    }
  }

Memento

Briefly, the Originator (the object to be saved) creates a snap-shot of itself as a Memento object, and passes that reference to the Caretaker object. The Caretaker object keeps the Memento until such a time as the Originator may want to revert to a previous state as recorded in the Memento object.

Implementation in Java

The following Java program illustrates the "undo" usage of the Memento Pattern.

class Originator {
    private String state;
    // The class could also contain additional data that is not part of the
    // state saved in the memento.
 
    public void set(String state) {
        System.out.println("Originator: Setting state to " + state);
        this.state = state;
    }
 
    public Memento saveToMemento() {
        System.out.println("Originator: Saving to Memento.");
        return new Memento(state);
    }
 
    public void restoreFromMemento(Memento memento) {
        state = memento.getSavedState();
        System.out.println("Originator: State after restoring from Memento: " + state);
    }
 
    public static class Memento {
        private final String state;
 
        private Memento(String stateToSave) {
            state = stateToSave;
        }
 
        private String getSavedState() {
            return state;
        }
    }
}
import java.util.List;
import java.util.ArrayList;
 
class Caretaker {
    public static void main(String[] args) {
        List<Originator.Memento> savedStates = new ArrayList<Originator.Memento>();
 
        Originator originator = new Originator();
        originator.set("State1");
        originator.set("State2");
        savedStates.add(originator.saveToMemento());
        originator.set("State3");
        // We can request multiple mementos, and choose which one to roll back to.
        savedStates.add(originator.saveToMemento());
        originator.set("State4");
 
        originator.restoreFromMemento(savedStates.get(1));  
    }
}

The output is:

Originator: Setting state to State1
Originator: Setting state to State2
Originator: Saving to Memento.
Originator: Setting state to State3
Originator: Saving to Memento.
Originator: Setting state to State4
Originator: State after restoring from Memento: State3
Implementation in C#

C# Example

using System;
 
namespace DoFactory.GangOfFour.Memento.Structural
{
  // MainApp startup class for Structural
  // Memento Design Pattern.
  class MainApp
  {
     // Entry point into console application.
    static void Main()
    {
      Originator o = new Originator();
      o.State = "On";
 
      // Store internal state
      Caretaker c = new Caretaker();
      c.Memento = o.CreateMemento();
 
      // Continue changing originator
      o.State = "Off";
 
      // Restore saved state
      o.SetMemento(c.Memento);
 
      // Wait for user
      Console.ReadKey();
    }
  }
 
  // The 'Originator' class
  class Originator
  {
    private string _state;
 
    // Property
    public string State
    {
      get { return _state; }
      set
      {
        _state = value;
        Console.WriteLine("State = " + _state);
      }
    }
 
    // Creates memento
    public Memento CreateMemento()
    {
      return (new Memento(_state));
    }
 
    // Restores original state
    public void SetMemento(Memento memento)
    {
      Console.WriteLine("Restoring state...");
      State = memento.State;
    }
  }
 
  // The 'Memento' class
  class Memento
  {
    private readonly string _state;
 
    // Constructor
    public Memento(string state) {
      this._state = state;
    }
 
    // Gets or sets state
    public string State
    {
      get { return _state; }
    }
  }
 
  // The 'Caretaker' class
  class Caretaker
  {
    private Memento _memento;
 
    // Gets or sets memento
    public Memento Memento
    {
      set { _memento = value; }
      get { return _memento; }
    }
  }
}

Another way to implement memento in C#

public interface IOriginator
{
   IMemento GetState();
}
 
public interface IShape : IOriginator
{
   void Draw();
   void Scale(double scale);
   void Move(double dx, double dy);
}
 
public interface IMemento
{
   void RestoreState();
}
 
public class CircleOriginator : IShape
{
   private class CircleMemento : IMemento
   {
      private readonly double x;
      private readonly double y;
      private readonly double r;
      private readonly CircleOriginator originator;
 
      public CircleMemento(CircleOriginator originator)
      {
         this.originator = originator;
         x = originator.x;
         y = originator.y;
         r = originator.r;
      }
 
      public void RestoreState()
      {
         originator.x = x;
         originator.y = y;
         originator.r = r;
      }
   }
 
   double x;
   double y;
   double r;
 
   public CircleOriginator(double x, double y, double r)
   {
      this.x = x;
      this.y = y;
      this.r = r;
   }
 
   public void Draw()
   {
      Console.WriteLine("Circle with radius {0} at ({1}, {2})", r, x, y);
   }
 
   public void Scale(double scale)
   {
      r *= scale;
   }
 
   public void Move(double dx, double dy)
   {
      x += dx;
      y += dy;
   }
 
   public IMemento GetState()
   {
      return new CircleMemento(this);
   }
}
 
public class RectOriginator : IShape
{
   private class RectMemento : IMemento
   {
      private readonly double x;
      private readonly double y;
      private readonly double w;
      private readonly double h;
      private readonly RectOriginator originator;
 
      public RectMemento(RectOriginator originator)
      {
         this.originator = originator;
         x = originator.x;
         y = originator.y;
         w = originator.w;
         h = originator.h;
      }
 
      public void RestoreState()
      {
         originator.x = x;
         originator.y = y;
         originator.w = w;
         originator.h = h;
      }
   }
 
   double x;
   double y;
   double w;
   double h;
 
   public RectOriginator(double x, double y, double w, double h)
   {
      this.x = x;
      this.y = y;
      this.w = w;
      this.h = h;
   }
 
   public void Draw()
   {
      Console.WriteLine("Rectangle {0}x{1} at ({2}, {3})", w, h, x, y);
   }
 
   public void Scale(double scale)
   {
      w *= scale;
      h *= scale;
   }
 
   public void Move(double dx, double dy)
   {
      x += dx;
      y += dy;
   }
 
   public IMemento GetState()
   {
      return new RectMemento(this);
   }
}
 
public class Caretaker
{
   public void Draw(IEnumerable<IShape> shapes)
   {
      foreach(IShape shape in shapes)
      {
         shape.Draw();
      }
   }
 
   public void MoveAndScale(IEnumerable<IShape> shapes)
   {
      foreach(IShape shape in shapes)
      {
         shape.Scale(10);
         shape.Move(3, 2);
      }
   }
 
   public IEnumerable<IMemento> SaveStates(IEnumerable<IShape> shapes)
   {
      List<IMemento> states = new List<IMemento>();
      foreach(IShape shape in shapes)
      {
         states.Add(shape.GetState());
      }
      return states;
   }
 
   public void RestoreStates(IEnumerable<IMemento> states)
   {
      foreach(IMemento state in states)
      {
         state.RestoreState();
      }
   }
 
   public static void Main()
   {
      IShape[] shapes = { new RectOriginator(10, 20, 3, 5), new CircleOriginator(5, 2, 10) };
 
      //Outputs:
      // Rectangle 3x5 at (10, 20)
      // Circle with radius 10 at (5, 2)
      Draw(shapes);
 
      //Save states of figures
      IEnumerable<IMemento> states = SaveStates(shapes);
 
      //Change placement of figures
      MoveAndScale(shapes);
 
      //Outputs:
      // Rectangle 30x50 at (13, 22)
      // Circle with radius 100 at (8, 4)
      Draw(shapes);
 
      //Restore old placement of figures
      RestoreStates(states);
 
      //Outputs:
      // Rectangle 3x5 at (10, 20)
      // Circle with radius 10 at (5, 2)
      Draw(shapes);
   }
}
Implementation in Pascal
{$apptype console}
program Memento;
 
uses
  SysUtils, Classes;
 
type
  TMemento = class
  private
    _state: string;
    function GetSavedState: string;
  public
    constructor Create(stateToSave: string);
    property SavedState: string read GetSavedState;
  end;
 
  TOriginator = class
  private
    _state: string;
    // The class could also contain additional data that is not part of the
    // state saved in the memento.
    procedure SetState(const state: string);
  public
    function SaveToMemento: TMemento;
    procedure RestoreFromMemento(Memento: TMemento);
    property state: string read _state write SetState;
  end;
 
  TCaretaker = class
  private
    _memento: TMemento;
  public
    property Memento: TMemento read _memento write _memento;
  end;
 
  { TMemento }
 
constructor TMemento.Create(stateToSave: string);
begin
  _state := stateToSave;
end;
 
function TMemento.GetSavedState: string;
begin
  writeln('restoring state from Memento');
  result := _state;
end;
 
{ TOriginator }
 
procedure TOriginator.RestoreFromMemento(Memento: TMemento);
begin
  _state := Memento.SavedState;
  writeln('Originator: State after restoring from Memento: ' + state);
end;
 
function TOriginator.SaveToMemento: TMemento;
begin
  writeln('Originator: Saving to Memento.');
  result := TMemento.Create(state);
end;
 
procedure TOriginator.SetState(const state: string);
begin
  writeln('Originator: Setting state to ' + state);
  _state := state;
end;
 
var
  originator: TOriginator;
  c1,c2: TCaretaker;
begin
  originator := TOriginator.Create;
  originator.SetState('State1');
  originator.SetState('State2');
  // Store internal state
  c1 := TCaretaker.Create();
  c1.Memento := originator.SaveToMemento();
  originator.SetState('State3');
  // We can request multiple mementos, and choose which one to roll back to.
  c2 := TCaretaker.Create();
  c2.Memento := originator.SaveToMemento();
  originator.SetState('State4');
 
  // Restore saved state
  originator.RestoreFromMemento(c1.Memento);
  originator.RestoreFromMemento(c2.Memento);
  c1.Memento.Free;
  c1.Free;
  c2.Memento.Free;
  c2.Free;
  originator.Free;
end.
Implementation in Scala
import java.util.Date
import common.patterns.Originator.Memento
import scala.collection.immutable.TreeMap
 
object Originator {
  case class Memento(x: Int, y: Int, time: Date)
}
 
case class Originator(var x: Int, var y: Int) {
  def setState(x: Int, y: Int) {
    this.x = x
    this.y = y
  }
 
  def createMemento(): Memento = Memento(x, y, new Date())
 
  def restoreFromMemento(restore: Memento) {
    this.x = restore.x
    this.y = restore.y
  }
}
 
object Caretaker extends App {
  var states: TreeMap[Date, Memento] = new TreeMap
  val originator = new Originator(0, 0)
  var memento = originator.createMemento()
  states += (memento.time -> memento)
 
  Thread.sleep(100)
  originator.setState(5, 4)
  memento = originator.createMemento()
  states += (memento.time -> memento)
 
  Thread.sleep(100)
  originator.setState(10, 10)
  memento = originator.createMemento()
  states += (memento.time -> memento)
 
  originator.restoreFromMemento(states(states.drop(1).firstKey))  //restore second state
  println(originator)
}

Output:

   Originator(5,4)

Print version

The model-view-controller (MVC) pattern is an architectural pattern used primarily in creating Graphic User Interfaces (GUIs). The major premise of the pattern is based on modularity and it is to separate three different aspects of the GUI: the data (model), the visual representation of the data (view), and the interface between the view and the model (controller). The primary idea behind keeping these three components separate is so that each one is as independent of the others as possible, and changes made to one will not affect changes made to the others. In this way, for instance, the GUI can be updated with a new look or visual style without having to change the data model or the controller.

Newcomers will probably see this MVC pattern as wasteful, mainly because you are working with many extra objects at runtime, when it seems like one giant object will do. But the secret to the MVC pattern is not writing the code, but in maintaining it, and allowing people to modify the code without changing much else. Also, keep in mind, that different developers have different strengths and weaknesses, so team building around MVC is easier. Imagine a View Team that is responsible for great views, a Model Team that knows a lot about data, and a Controller Team that see the big picture of application flow, handing requests, working with the model, and selecting the most appropriate next view for that client.

One of the great advantages of the Model-View-Controller Pattern is the ability to reuse the application's logic (which is implemented in the model) when implementing a different view. A good example is found in web development, where a common task is to implement an external API inside of an existing piece of software. If the MVC pattern has cleanly been followed, this only requires modification to the controller, which can have the ability to render different types of views dependent on the content type requested by the user agent.

Model

Generally, the model is constructed first. The model has two jobs: it must both store a state and manage subscribers. The state does not need to be anything special; you simply need to define how you're going to store data, with setters and getters. However, anything which can change (any property) must have a list of listeners which it contacts whenever the value changes. The property listeners allow us to have multiple views on the same data, which are all live-updated when the state changes. The code for this can usually be defined in a superclass and inherited, so that you just write it in one place and then it applies consistently to every property. Nested states (for example, dynamically configured properties) may require some extra thinking about how you want to report the changes.

People sometimes use the word 'model' to refer to the 'data model' -- the architecture of the data structures in the application. A good MVC framework takes care of the model superclass and subscriptions for you, so that you can simply define your own data structure in that framework's model-language, and then immediately build the other components. In this end-user sense, defining the data model is equivalent to defining a model. However, if you are designing a framework, it is not MVC if it lacks property listeners.

View

Once you write a data model, the next easiest thing to write is usually a view. The view is the part of the application which subscribes to a model. Usually it presents it to a user alongside a user interface, or GUI. The GUI contains other components too, which are usually part of the controller and can be handled later.

You can also have view-controller components which have nothing to do with user interfaces. You can imagine, for example, an MVC circuit-board application where a model simply manages numbers (voltages and currents) throughout the circuit. A resistor, for example, then has a "view" of the voltages at each end, tightly coupled to a "controller" which updates the current going through that resistor. All of the components talking to each other through the model would then eventually perform the circuit's actions in real-time.

When you're writing a view, there are two things to think about: "what do I do when the state changes?" and "how do I display this to the user?" Your MVC framework usually provides editors for various properties in the model, like date selectors, text fields, sliding bars, and combo boxes. More complex properties often require more complex views and editors, like tree views. Your MVC framework will probably have already figured out how to connect these components to the model, and might even auto-generate appropriate components based on the type of a given property in the model.

Controller

The rest of the GUI -- the parts which do not update when the model changes -- are the responsibility of the controller. This includes navigating around the view, as well as what you do when someone tries to edit the data in the view. Strictly speaking, a view cannot be edited and is 'read-only' -- when you try to modify a field in the view, the controller needs to pick up the editing event, process it, and send it to the model; the model will then update the view if/when the value actually changes.

Different frameworks handle controllers in different ways. Some frameworks allow callback functions to be associated with editors, which get called when the value is changed. Other frameworks allow you to add listeners directly to the component, which can be notified when the value in the editor changes, when it is clicked on, or when it loses focus, etc. In many frameworks, the controller appears as a collection of methods and listeners built into both the data model and the view. For example, if you have a "password" property in the model which needs to be salted and hashed before being stored, this logic might appear in a setPassword function living in the data model. Similarly, when a framework generates a view, the widgets for the view are often not read-only but rather read-write, with listeners. The actual controllers provided by the MVC framework then "plug into" each of these interfaces, and pass data between them.

Validation

When possible, it is usually best to allow the model to do all the necessary validation of values, so that any changes to the allowed values, or changes simply to the validation process, only need to be made in one place. However, in some languages under certain circumstances, this may not be possible. For instance, if a numeric property is being edited with a text field, then the value of the property passed to the controller by the view will be text, not a number. In this case, the model could be made to have an additional method that takes text as the input value for this property, but more likely, the controller will do the initial parsing of the text to get the numeric value, and then pass it on to the model to do further validation (for instance, bounds checking). When either the controller or the model determines that a passed in value is invalid, the controller will need to tell the view that the value is invalid. In some cases, the view may then issue an error dialog or other notification, or it may simply revert the value in its editor to the older valid value.

Implementation in Java

In Java, we can implement a fat client GUI. In this case, we can use:

  • JavaBeans to implement the model and,
  • SWT or Java Swings to implement the view and the controller.

However, in Java EE, we can also implement a web application. In this case, we can use:

Observer

Scope

Object

Purpose

Behavioral

Intent

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Applicability

  • when an abstraction has two aspects, one dependent on the other
  • when a change to one object requires changing others, and you don't know how many objects need to be changed
  • when an object should notify other objects without making assumptions about who these objects are

Structure

Observer.svg

Consequences

  • + modularity: subject and observers may vary independently
  • + extensibility: can define and add any number of observers
  • + customizability: different observers provide different views of subject
  • - unexpected updates: observers don't know about each other
  • - update overhead: might need hints

Implementation

  • subject-observer mapping
  • dangling references
  • avoiding observer-specific update protocols: the push and pull models
  • registering modifications of interest explicitly

Related pattern

  • Singleton, is used to make observable object unique and accessible globally.
  • Mediator, is used to encapsulate updated objects

Description

Problem 
In one place or many places in the application we need to be aware about a system event or an application state change. We'd like to have a standard way of subscribing to listening for system events and a standard way of notifying the interested parties. The notification should be automated after an interested party subscribed to the system event or application state change. There should be a way to unsubscribe, too.
Forces 
Observers and observables probably should be represented by objects. The observer objects will be notified by the observable objects.
Solution 
After subscribing the listening objects will be notified by a way of method call.
Loose coupling
When two objects are loosely coupled, they can interact, but they have very little knowledge of each other. Strive for loosely coupled designs between objects that interact.
  • The only thing that Subject knows about an observer is that it implements a certain interface
  • We can add new observers at any time
  • We never need to modify the subject to add new types of observers
  • We can reuse subjects or observers independently of each other
  • Changes to either the subject or an observer will not affect the other

Examples

The Observer pattern is used extensively in Java. E.g. in the following piece of code

  • button is the Subject
  • MyListener is the Observer
  • actionPerformed() is the equivalent to update()
  1.  JButton button = new JButton("Click me!");
    
  2.  button.addActionListener(new MyListener());
    
  3.  
    
  4.  class MyListener implements ActionListener {
    
  5.     public void actionPerformed(ActionEvent event) {
    
  6.        ...
    
  7.     }  
    
  8.  }
    

Another example is the PropertyChangeSupport. The Component class uses a PropertyChangeSupport object to let interested observers register for notification of changes in the properties of labels, panels, and other GUI components.

Observer-PropertyChangeSupport.gif

Can you find the Subject, Observer and update() in the above class diagram?

  • Component is the Subject
  • PropertyChangeListener is the Observer
  • propertyChange() is the equivalent to update()

Cost

This pattern can be tricky if you do not beware. Beware the data representing the state changes of the subject can evolve without changing the interfaces. If you only transmit a string, the pattern could become very expensive if at least one new observer needs also a state code. You should use a mediator unless you know the implementation of the subject state will never change.

Creation

This pattern has a cost to create.

Maintenance

This pattern can be expensive to maintain.

Removal

This pattern has a cost to remove too.

Advises

  • Put the subject and observer term in the name of the subject and the observer classes to indicate the use of the pattern to the other developers.
  • To improve performances, you can only send to the observers the difference of states instead of the new state. The observers can only update following the changed part of the subject instead of all the state of the subject.

Implementations

Implementation in ActionScript 3
// Main Class
package {
    import flash.display.MovieClip;
 
    public class Main extends MovieClip {
        private var _cs:ConcreteSubject = new ConcreteSubject();
        private var _co1:ConcreteObserver1 = new ConcreteObserver1();
        private var _co2:ConcreteObserver2 = new ConcreteObserver2();
 
        public function Main() {
            _cs.registerObserver(_co1);
            _cs.registerObserver(_co2);
 
            _cs.changeState(10);
            _cs.changeState(99);
 
            _cs.unRegisterObserver(_co1);
 
            _cs.changeState(17);
 
            _co1 = null;
        }
    }
}
 
// Interface Subject
package {
    public interface ISubject {
        function registerObserver(o:IObserver):void;
 
        function unRegisterObserver(o:IObserver):void;
 
        function updateObservers():void;
 
        function changeState(newState:uint):void;
    }
}
 
// Interface Observer
package {
    public interface IObserver {
        function update(newState:uint):void;
    }
}
 
// Concrete Subject
package {
    public class ConcreteSubject implements ISubject {
        private var _observersList:Array = new Array();
        private var _currentState:uint;
 
        public function ConcreteSubject() {
        }
 
        public function registerObserver(o:IObserver):void {
            _observersList.push( o );
            _observersList[_observersList.length-1].update(_currentState); // update newly registered
        }
 
        public function unRegisterObserver(o:IObserver):void {
            _observersList.splice( _observersList.indexOf( o ), 1 );
        }
 
        public function updateObservers():void {
            for( var i:uint = 0; i<_observersList.length; i++) {
                _observersList[i].update(_currentState);
            }
        }
 
        public function changeState(newState:uint):void {
            _currentState = newState;
            updateObservers();
        }
    }
}
 
// Concrete Observer 1
package {
    public class ConcreteObserver1 implements IObserver {
        public function ConcreteObserver1() {
        }
 
        public function update(newState:uint):void {
            trace( "co1: "+newState );
        }
 
        // other Observer specific methods
    }
}
 
// Concrete Observer 2
package {
    public class ConcreteObserver2 implements IObserver {
        public function ConcreteObserver2() {
        }
 
        public function update(newState:uint):void {
            trace( "co2: "+newState );
        }
 
        // other Observer specific methods
    }
}
Implementation in C#

Traditional Method

C# and the other .NET Framework languages do not typically require a full implementation of the Observer pattern using interfaces and concrete objects. Here is an example of using them, however.

 
 
using System;
using System.Collections;
 
namespace Wikipedia.Patterns.Observer
{
  // IObserver --> interface for the observer
  public interface IObserver
  {
    // called by the subject to update the observer of any change
    // The method parameters can be modified to fit certain criteria
    void Update(string message);
  }
 
  public class Subject
  {
    // use array list implementation for collection of observers
    private ArrayList observers;
 
    // constructor
    public Subject()
    {
      observers = new ArrayList();
    }
 
    public void Register(IObserver observer)
    {
      // if list does not contain observer, add
      if (!observers.Contains(observer))
      {
        observers.Add(observer);
      }
    }
 
    public void Unregister(IObserver observer)
    {
      // if observer is in the list, remove
      observers.Remove(observer);
    }
 
    public void Notify(string message)
    {
      // call update method for every observer
      foreach (IObserver observer in observers)
      {
        observer.Update(message);
      }
    }
  }
 
  // Observer1 --> Implements the IObserver
  public class Observer1 : IObserver
  {
    public void Update(string message)
    {
      Console.WriteLine("Observer1:" + message);
    }
  }
 
  // Observer2 --> Implements the IObserver
  public class Observer2 : IObserver
  {
    public void Update(string message)
    {
      Console.WriteLine("Observer2:" + message);
    }
  }
 
  // Test class
  public class ObserverTester
  {
    [STAThread]
    public static void Main()
    {
      Subject mySubject = new Subject();
      IObserver myObserver1 = new Observer1();
      IObserver myObserver2 = new Observer2();
 
      // register observers
      mySubject.Register(myObserver1);
      mySubject.Register(myObserver2);
 
      mySubject.Notify("message 1");
      mySubject.Notify("message 2");
    }
  }
}

Using Events

The alternative to using concrete and abstract observers and publishers in C# and other .NET Framework languages, such as Visual Basic, is to use events. The event model is supported via delegates that define the method signature that should be used to capture events. Consequently, delegates provide the mediation otherwise provided by the abstract observer, the methods themselves provide the concrete observer, the concrete subject is the class defining the event, and the subject is the event system built into the base class library. It is the preferred method of accomplishing the Observer pattern in .NET applications.

using System;
 
// First, declare a delegate type that will be used to fire events.
//  This is the same delegate as System.EventHandler.
//  This delegate serves as the abstract observer.
//  It does not provide the implementation, but merely the contract.
public delegate void EventHandler(object sender, EventArgs e);
 
// Next, declare a published event.  This serves as the concrete subject.
//  Note that the abstract subject is handled implicitly by the runtime.
public class Button
{
    // The EventHandler contract is part of the event declaration.
    public event EventHandler Clicked;
 
    // By convention, .NET events are fired from descendant classes by a virtual method,
    //  allowing descendant classes to handle the event invocation without subscribing
    //  to the event itself.
    protected virtual void OnClicked(EventArgs e)
    {
        if (Clicked != null)
            Clicked(this, e); // implicitly calls all observers/subscribers
    }
}
 
// Then in an observing class, you are able to attach and detach from the events:
public class Window
{
    private Button okButton;
 
    public Window()
    {
        okButton = new Button();
        // This is an attach function.  Detaching is accomplished with -=.
        // Note that it is invalid to use the assignment operator - events are multicast
        //  and can have multiple observers.
        okButton.Clicked += new EventHandler(okButton_Clicked);
    }
 
    private void okButton_Clicked(object sender, EventArgs e)
    {
        // This method is called when Clicked(this, e) is called within the Button class
        //  unless it has been detached.
    }
}
Implementation in Java

Handy implementation

You can implement this pattern in Java like this:

  1.  // Observer pattern -- Structural example
    
  2.  // @since JDK 5.0
    
  3.  import java.util.ArrayList;
    
  4.  
    
  5.  // "Subject"
    
  6.  abstract class Subject {
    
  7.     // Fields
    
  8.     private ArrayList<Observer> observers = new ArrayList<Observer>();
    
  9.     // Methods
    
  10.     public void attach(Observer observer) {
    
  11.        observers.add(observer);
    
  12.     }
    
  13.     public void detach(Observer observer) {
    
  14.        observers.remove(observer);
    
  15.     }
    
  16.     public void notifyObservers() {
    
  17.        for (Observer o : observers)    
    
  18.           o.update();
    
  19.    }
    
  20.  }
    
  1.  // "ConcreteSubject"
    
  2.  class ConcreteSubject extends Subject {
    
  3.   // Fields
    
  4.   private String subjectState;
    
  5.   // Properties
    
  6.   public String getSubjectState() {
    
  7.     return subjectState;
    
  8.   }
    
  9.   public void setSubjectState(String value) {
    
  10.     subjectState = value;
    
  11.   }
    
  12.  }
    
  1.  // "Observer"
    
  2.  abstract class Observer {
    
  3.    // Methods
    
  4.    abstract public void update();
    
  5.  }
    
  1.  // "ConcreteObserver"
    
  2.  class ConcreteObserver extends Observer {
    
  3.   // Fields
    
  4.   private String name;
    
  5.   private String observerState;
    
  6.   private ConcreteSubject subject;
    
  7.  
    
  8.   // Constructors
    
  9.   public ConcreteObserver(ConcreteSubject subject, String name) {
    
  10.      this.subject = subject;
    
  11.      this.name = name;
    
  12.      //subject.attach(this);
    
  13.   }
    
  14.   // Methods
    
  15.   public void update() {
    
  16.      observerState = subject.getSubjectState();
    
  17.      System.out.printf("Observer %s's new state is %s\n", name, observerState);
    
  18.   }
    
  19.  }
    
  1.  // Client test
    
  2.  public class Client {
    
  3.    public static void main(String[] args) {
    
  4.       // Configure Observer structure
    
  5.       ConcreteSubject s = new ConcreteSubject();
    
  6.       s.attach(new ConcreteObserver(s, "A"));
    
  7.       s.attach(new ConcreteObserver(s, "B"));
    
  8.       s.attach(new ConcreteObserver(s, "C"));
    
  9.  
    
  10.       // Change subject and notify observers
    
  11.       s.setSubjectState("NEW");
    
  12.       s.notifyObservers();
    
  13.    }
    
  14.  }
    

Built-in support

The Java JDK has several implementations of this pattern: application in Graphical User Interfaces such as in the AWK toolkit, Swing etc. In Swing, whenever a user clicks a button or adjusts a slider, many objects in the application may need to react to the change. Swing refers to interested clients (observers) as "listeners" and lets you register as many listeners as you like to be notified of a component's events.

MVC is more M(VC) in Swing, i.e. View and Controller are tightly coupled; Swing does not divide Views from Controllers. MVC supports n-tier development, i.e. loosely coupled layers (see below) that can change independently and that may even execute on different machines.

There is also a built-in support for the Observer pattern. All one has to do is extend java.util.Observable (the Subject) and tell it when to notify the java.util.Observers. The API does the rest for you. You may use either push or pull style of updating your observers.

Observer-java.jpg

java.util.Observable is a class while java.util.Observer is an interface.

  1.  public void setValue(double value) {
    
  2.     this.value = value;
    
  3.     setChanged();
    
  4.     notifyObservers();
    
  5.  }
    

Note that you have to call setChanged() so that the Observable code will broadcast the change. The notifyObservers() method calls the update() method of each registered observer. The update() method is a requirement for implementers of the Observer Interface.

  1.  // Observer pattern -- Structural example
    
  2.  import java.util.Observable;
    
  3.  import java.util.Observer;
    
  4.  
    
  5.  // "Subject"
    
  6.  class ConcreteSubject extends Observable {
    
  7.   // Fields
    
  8.   private String subjectState;
    
  9.   // Methods
    
  10.   public void dataChanged() {
    
  11.      setChanged();
    
  12.      notifyObservers(); // use the pull method
    
  13.   }
    
  14.   // Properties
    
  15.   public String getSubjectState() {
    
  16.      return subjectState;
    
  17.   }
    
  18.   public void setSubjectState(String value) {
    
  19.       subjectState = value;
    
  20.       dataChanged();
    
  21.   }
    
  22.  }
    
  1.  // "ConcreteObserver"
    
  2.  import java.util.Observable;
    
  3.  import java.util.Observer;
    
  4.  
    
  5.  class ConcreteObserver implements Observer {
    
  6.    // Fields
    
  7.    private String name;
    
  8.    private String observerState;
    
  9.    private Observable subject;
    
  10.  
    
  11.    // Constructors
    
  12.    public ConcreteObserver(Observable subject, String name) {
    
  13.      this.subject = subject;
    
  14.      this.name = name;
    
  15.      subject.addObserver(this);
    
  16.   }
    
  17.  
    
  18.   // Methods
    
  19.   public void update(Observable subject, Object arg) {
    
  20.    if (subject instanceof ConcreteSubject) {
    
  21.      ConcreteSubject subj = (ConcreteSubject)subject;
    
  22.      observerState = subj.getSubjectState();
    
  23.      System.out.printf("Observer %s's new state is %s\n", name, observerState);
    
  24.    }
    
  25.   }
    
  26.  }
    
  1.  // Client test
    
  2.  public class Client {
    
  3.    public static void main(String[] args) {
    
  4.       // Configure Observer structure
    
  5.       ConcreteSubject s = new ConcreteSubject();
    
  6.       new ConcreteObserver(s, "A");
    
  7.       new ConcreteObserver(s, "B");
    
  8.       new ConcreteObserver(s, "C");
    
  9.       // Change subject and notify observers
    
  10.       s.setSubjectState("NEW");
    
  11.    }
    
  12.  }
    

Keyboard handling

Below is an example written in Java that takes keyboard input and treats each input line as an event. The example is built upon the library classes java.util.Observer and java.util.Observable. When a string is supplied from System.in, the method notifyObservers is then called, in order to notify all observers of the event's occurrence, in the form of an invocation of their 'update' methods - in our example, ResponseHandler.update(...).

The file MyApp.java contains a main() method that might be used in order to run the code.

  1. /* Filename : EventSource.java */
    
  2. package org.wikibooks.obs;
    
  3.  
    
  4. import java.util.Observable;          // Observable is here
    
  5. import java.io.BufferedReader;
    
  6. import java.io.IOException;
    
  7. import java.io.InputStreamReader;
    
  8.  
    
  9. public class EventSource extends Observable implements Runnable {
    
  10.     @Override
    
  11.     public void run() {
    
  12.         try {
    
  13.             final InputStreamReader isr = new InputStreamReader(System.in);
    
  14.             final BufferedReader br = new BufferedReader(isr);
    
  15.             while (true) {
    
  16.                 String response = br.readLine();
    
  17.                 setChanged();
    
  18.                 notifyObservers(response);
    
  19.             }
    
  20.         } catch (IOException e) {
    
  21.             e.printStackTrace();
    
  22.         }
    
  23.     }
    
  24. }
    
  1. /* Filename : ResponseHandler.java */
    
  2.  
    
  3. package org.wikibooks.obs;
    
  4.  
    
  5. import java.util.Observable;
    
  6. import java.util.Observer;  /* this is Event Handler */
    
  7.  
    
  8. public class ResponseHandler implements Observer {
    
  9.     private String resp;
    
  10.     public void update(Observable obj, Object arg) {
    
  11.         if (arg instanceof String) {
    
  12.             resp = (String) arg;
    
  13.             System.out.println("\nReceived Response: " + resp );
    
  14.         }
    
  15.     }
    
  16. }
    
  1. /* Filename : MyApp.java */
    
  2. /* This is the main program */
    
  3.  
    
  4. package org.wikibooks.obs;
    
  5.  
    
  6. public class MyApp {
    
  7.     public static void main(String[] args) {
    
  8.         System.out.println("Enter Text >");
    
  9.  
    
  10.         // create an event source - reads from stdin
    
  11.         final EventSource eventSource = new EventSource();
    
  12.  
    
  13.         // create an observer
    
  14.         final ResponseHandler responseHandler = new ResponseHandler();
    
  15.  
    
  16.         // subscribe the observer to the event source
    
  17.         eventSource.addObserver(responseHandler);
    
  18.  
    
  19.         // starts the event thread
    
  20.         Thread thread = new Thread(eventSource);
    
  21.         thread.start();
    
  22.     }
    
  23. }
    

The Java implementation of the Observer pattern has pros and cons:

Pros

  • It hides many of the details of the Observer pattern
  • It can be used both pull and push ways.

Cons

  • Because Observable is a class, you have to subclass it; you can’t add on the Observable behavior to an existing class that subclasses another superclass (fails the programming to interfaces principle). If you can’t subclass Observable, then use delegation, i.e. provide your class with an Observable object and have your class forward key method calls to it.
  • Because setChanged() is protected, you can’t favour composition over inheritance.
Implementation in PHP

class STUDENT

<?php
class Student implements SplObserver {
 
  protected $type = "Student";
  private   $name;
  private   $address;
  private   $telephone;
  private   $email;
  private   $_classes = array();
 
  public function __construct($name)
  {
    $this->name = $name;
  }
 
  public function GET_type()
  {
    return $this->type;
  }
 
  public function GET_name()
  {
    return $this->name;
  }
 
  public function GET_email()
  {
    return $this->email;
  }
 
  public function GET_telephone()
  {
    return $this->telephone;
  }
 
  public function update(SplSubject $object)
  {
    $object->SET_log("Comes from ".$this->name.": I'm a student of ".$object->GET_materia());
  }
 
}
 
?>

class TEACHER

<?php
class Teacher implements SplObserver {
 
  protected $type = "Teacher";
  private   $name;
  private   $address;
  private   $telephone;
  private   $email;
  private   $_classes = array();
 
  public function __construct($name)
  {
    $this->name = $name;
  }
 
  public function GET_type()
  {
    return $this->type;
  }
 
  public function GET_name()
  {
    return $this->name;
  }
 
  public function GET_email()
  {
    return $this->email;
  }
 
  public function GET_telephone()
  {
    return $this->name;
  }
 
  public function update(SplSubject $object)
  {
    $object->SET_log("Comes from ".$this->name.": I teach in ".$object->GET_materia());
  }
 
}
 
?>

Class SUBJECT

<?php
 
class Subject implements SplSubject {
 
  private $name_materia;
  private $_observers = array();
  private $_log = array();
 
  function __construct($name)
  {
    $this->name_materia = $name;
    $this->_log[]       = "Subject $name was included";
  }
 
  /* Add an observer */
  public function attach(SplObserver $classes) {
    $this->_classes[] = $classes;
    $this->_log[]     = " The ".$classes->GET_type()." ".$classes->GET_name()." was included";
  }
 
  /* Remove an observer */
  public function detach(SplObserver $classes) {
    foreach ($this->_classes as $key => $obj) {
      if ($obj == $classes) {
        unset($this->_classes[$key]);
        $this->_log[] = " The ".$classes->GET_type()." ".$classes->GET_name()." was removed";
                }
    }
  }
 
  /* Notificate an observer */
  public function notify(){
    foreach ($this->_classes as $classes){
      $classes->update($this);
    }
  }
 
  public function GET_materia()
  {
    return $this->name_materia;
  }
 
  function SET_log($valor)
  {
    $this->_log[] = $valor ;
  }
 
  function GET_log()
  {
    return $this->_log;
  }
 
 
}
?>

Application

<?php
require_once("teacher.class.php");
require_once("student.class.php");
require_once("subject.class.php");
 
$subject  = new Subject("Math");
$marcus   = new Teacher("Marcus Brasizza");
$rafael   = new Student("Rafael");
$vinicius = new Student("Vinicius");
 
// Include observers in the math Subject
$subject->attach($rafael);
$subject->attach($vinicius);
$subject->attach($marcus);
 
$subject2 = new Subject("English");
$renato   = new Teacher("Renato");
$fabio    = new Student("Fabio");
$tiago    = new Student("Tiago");
 
// Include observers in the english Subject
$subject2->attach($renato);
$subject2->attach($vinicius);
$subject2->attach($fabio);
$subject2->attach($tiago);
 
// Remove the instance "Rafael from subject"
$subject->detach($rafael);
 
// Notify both subjects
$subject->notify();
$subject2->notify();
 
echo "First Subject <br>";
echo "<pre>";
print_r($subject->GET_log());
echo "</pre>";
echo "<hr>";
echo "Second Subject <br>";
echo "<pre>";
print_r($subject2->GET_log());
echo "</pre>";
?>

OUTPUT

First Subject

Array
(
    [0] =>  Subject Math was included
    [1] =>  The Student Rafael was included
    [2] =>  The Student Vinicius was included
    [3] =>  The Teacher Marcus Brasizza was included
    [4] =>  The Student Rafael was removed
    [5] => Comes from Vinicius: I'm a student of Math
    [6] => Comes from Marcus Brasizza: I teach in Math
)

Second Subject

Array
(
    [0] =>  Subject English was included
    [1] =>  The Teacher Renato was included
    [2] =>  The Student Vinicius was included
    [3] =>  The Student Fabio was included
    [4] =>  The Student Tiago was included
    [5] => Comes from Renato: I teach in English
    [6] => Comes from Vinicius: I'm a student of English
    [7] => Comes from Fabio: I'm a student of English
    [8] => Comes from Tiago: I'm a student of English
)
Implementation in Python

The observer pattern in Python:

class AbstractSubject:
    def register(self, listener):
        raise NotImplementedError("Must subclass me")
 
    def unregister(self, listener):
        raise NotImplementedError("Must subclass me")
 
    def notify_listeners(self, event):
        raise NotImplementedError("Must subclass me")
 
class Listener:
    def __init__(self, name, subject):
        self.name = name
        subject.register(self)
 
    def notify(self, event):
        print self.name, "received event", event
 
class Subject(AbstractSubject):
    def __init__(self):
        self.listeners = []
        self.data = None
 
    def getUserAction(self):
        self.data = raw_input('Enter something to do:')
        return self.data
 
    # Implement abstract Class AbstractSubject
 
    def register(self, listener):
        self.listeners.append(listener)
 
    def unregister(self, listener):
        self.listeners.remove(listener)
 
    def notify_listeners(self, event):
        for listener in self.listeners:
            listener.notify(event)
 
 
if __name__=="__main__":
    # make a subject object to spy on
    subject = Subject()
 
    # register two listeners to monitor it.
    listenerA = Listener("<listener A>", subject)
    listenerB = Listener("<listener B>", subject)
 
    # simulated event
    subject.notify_listeners ("<event 1>")
    # outputs:
    #     <listener A> received event <event 1>
    #     <listener B> received event <event 1>
 
    action = subject.getUserAction()
    subject.notify_listeners(action)
    #Enter something to do:hello
    # outputs:
    #     <listener A> received event hello
    #     <listener B> received event hello

The observer pattern can be implemented more succinctly in Python using function decorators.

Implementation in Ruby

In Ruby, use the standard Observable mixin. For documentation and an example, see http://www.ruby-doc.org/stdlib/libdoc/observer/rdoc/index.html

Prototype

The prototype pattern is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. This pattern is used to:

  • avoid subclasses of an object creator in the client application, like the abstract factory pattern does.
  • avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) when it is prohibitively expensive for a given application.

To implement the pattern, declare an abstract base class that specifies a pure virtual clone() method. Any class that needs a "polymorphic constructor" capability derives itself from the abstract base class, and implements the clone() operation.

The client, instead of writing code that invokes the "new" operator on a hard-coded class name, calls the clone() method on the prototype, calls a factory method with a parameter designating the particular concrete derived class desired, or invokes the clone() method through some mechanism provided by another design pattern.

Structure

UML class diagram describing the Prototype design pattern

Rules of thumb

Sometimes creational patterns overlap — there are cases when either Prototype or Abstract Factory would be appropriate. At other times they complement each other: Abstract Factory might store a set of Prototypes from which to clone and return product objects (GoF, p126). Abstract Factory, Builder, and Prototype can use Singleton in their implementations. (GoF, p81, 134). Abstract Factory classes are often implemented with Factory Methods (creation through inheritance), but they can be implemented using Prototype (creation through delegation). (GoF, p95)

Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed. (GoF, p136)

Prototype doesn't require subclassing, but it does require an "initialize" operation. Factory Method requires subclassing, but doesn't require initialization. (GoF, p116)

Designs that make heavy use of the Composite and Decorator patterns often can benefit from Prototype as well. (GoF, p126)

The rule of thumb could be that you would need to clone() an Object when you want to create another Object at runtime that is a true copy of the Object you are cloning. True copy means all the attributes of the newly created Object should be the same as the Object you are cloning. If you could have instantiated the class by using new instead, you would get an Object with all attributes as their initial values. For example, if you are designing a system for performing bank account transactions, then you would want to make a copy of the Object that holds your account information, perform transactions on it, and then replace the original Object with the modified one. In such cases, you would want to use clone() instead of new.

Advices

  • Put the prototype term in the name of the prototype classes to indicate the use of the pattern to the other developers.
  • Only use an interface when it is necessary.

Implementations

It specifies the kind of objects to create using a prototypical instance. Prototypes of new products are often built prior to full production, but in this example, the prototype is passive and does not participate in copying itself. The mitotic division of a cell — resulting in two identical cells — is an example of a prototype that plays an active role in copying itself and thus, demonstrates the Prototype pattern. When a cell splits, two cells of identical genotype result. In other words, the cell clones itself.

Implementation in C#
//Note: In this example ICloneable interface (defined in .Net Framework) acts as Prototype
 
class ConcretePrototype : ICloneable
{
    public int X { get; set; }
 
    public ConcretePrototype(int x)
    {
        this.X = x;
    }
 
    public void PrintX()
    {
        Console.WriteLine("Value :" + X);
    }
 
    public object Clone()
    {
        return this.MemberwiseClone();
    }
}
 
/**
 * Client code
 */
public class PrototypeTest
{
    public static void Main()
    {
        var prototype = new ConcretePrototype(1000);
 
        for (int i = 1; i < 10; i++)
        {
            ConcretePrototype tempotype = prototype.Clone() as ConcretePrototype;
 
            // Usage of values in prototype to derive a new value.
            tempotype.X *= i;
            tempotype.PrintX();
        }
        Console.ReadKey();
    }
}
 
/*
 **Code output**
 
Value :1000
Value :2000
Value :3000
Value :4000
Value :5000
Value :6000
Value :7000
Value :8000
Value :9000
*/
Implementation in C++
#include <iostream>
 
using namespace std;
 
// Prototype
class Prototype
{
public:
    virtual ~Prototype() { }
 
    virtual Prototype* clone() const = 0;
 
    virtual void setX(int x) = 0;
 
    virtual int getX() const = 0;
 
    virtual void printX() const = 0;
};
 
// Concrete prototype
class ConcretePrototype : public Prototype
{
public:
    ConcretePrototype(int x) : x_(x) { }
 
    ConcretePrototype(const ConcretePrototype& p) : x_(p.x_) { }
 
    virtual ConcretePrototype* clone() const { return new ConcretePrototype(*this); }
 
    void setX(int x) { x_ = x; }
 
    int getX() const { return x_; }
 
    void printX() const { std::cout << "Value :" << x_ << std::endl; }
 
private:
    int x_;
};
 
// Client code
int main()
{
    Prototype* prototype = new ConcretePrototype(1000);
    for (int i = 1; i < 10; i++) {
        Prototype* temporaryPrototype = prototype->clone();
        temporaryPrototype->setX(temporaryPrototype->getX() * i);
        temporaryPrototype->printX();
        delete temporaryPrototype;
    }
    delete prototype;
    return 0;
}

Code output:

Value :1000
Value :2000
Value :3000
Value :4000
Value :5000
Value :6000
Value :7000
Value :8000
Value :9000
Implementation in Java
  1. /**
    
  2.  * Prototype class
    
  3.  */
    
  4. interface Prototype extends Cloneable {
    
  5.     void setX(int x);
    
  6.  
    
  7.     void printX();
    
  8.  
    
  9.     int getX();
    
  10. }
    
  1. /**
    
  2.  * Implementation of prototype class
    
  3.  */
    
  4. class PrototypeImpl implements Prototype {
    
  5.     private int x;
    
  6.  
    
  7.     /**
    
  8.      * Constructor
    
  9.      */
    
  10.     public PrototypeImpl(int x) {
    
  11.         setX(x);
    
  12.     }
    
  13.  
    
  14.     @Override
    
  15.     public void setX(int x) {
    
  16.         this.x = x;
    
  17.     }
    
  18.  
    
  19.     @Override
    
  20.     public void printX() {
    
  21.         System.out.println("Value: " + getX());
    
  22.     }
    
  23.  
    
  24.     @Override
    
  25.     public int getX() {
    
  26.         return x;
    
  27.     }
    
  28.  
    
  29.     @Override
    
  30.     public PrototypeImpl clone() throws CloneNotSupportedException {
    
  31.         return (PrototypeImpl) super.clone();
    
  32.     }
    
  33. }
    
  1. /**
    
  2.  * Client code
    
  3.  */
    
  4. public class PrototypeTest {
    
  5.     public static void main(String args[]) throws CloneNotSupportedException {
    
  6.         PrototypeImpl prototype = new PrototypeImpl(1000);
    
  7.  
    
  8.         for (int y = 1; y < 10; y++) {
    
  9.             // Create a defensive copy of the object to allow safe mutation
    
  10.             Prototype tempotype = prototype.clone();
    
  11.  
    
  12.             // Derive a new value from the prototype's "x" value
    
  13.             tempotype.setX(tempotype.getX() * y);
    
  14.             tempotype.printX();
    
  15.         }
    
  16.     }
    
  17. }
    

Code output:

Value: 1000
Value: 2000
Value: 3000
Value: 4000
Value: 5000
Value: 6000
Value: 7000
Value: 8000
Value: 9000
Implementation in PHP
<?php
class ConcretePrototype {
    protected $x;
 
    public function __construct($x) {
        $this->x = (int) $x;
    }
 
    public function printX() {
        echo sprintf('Value: %5d' . PHP_EOL, $this->x);
    }
 
    public function setX($x) {
        $this->x *= (int) $x;
    }
 
    public function __clone() {
        /*
         * This method is not required for cloning, although when implemented,
         * PHP will trigger it after the process in order to permit you some
         * change in the cloned object.
         *
         * Reference: http://php.net/manual/en/language.oop5.cloning.php
         */
        // $this->x = 1;
    }
}
 
/**
 * Client code
 */
$prototype = new ConcretePrototype(1000);
 
foreach (range(1, 10) as $i) {
    $tempotype = clone $prototype;
    $tempotype->setX($i);
    $tempotype->printX();
}
 
/*
 **Code output**
 
Value:  1000
Value:  2000
Value:  3000
Value:  4000
Value:  5000
Value:  6000
Value:  7000
Value:  8000
Value:  9000
Value: 10000
*/
Implementation in Python

This implementation uses the decorator pattern.

# Decorator class which allows an object to create an exact duplicate of itself
class Prototype:    
    def _clone_func(self):
        # This function will be assigned to the decorated object and can
        # be used to create an exact duplicate of that decorated object
        clone = self.cls()
        # Call _copy_func to ensure the attributes of the objects are identical
        self._copy_func(self.instance, clone)
        return clone
 
    def _copy_func(self, fromObj, toObj):
        # Dual purpose function which is assigned to the decorated object 
        # and used internally in the decorator to copy the original attributes 
        # to the clone to ensure it's identical to the object which made the clone        
        for attr in dir(fromObj):
            setattr(toObj, attr, getattr(fromObj, attr))
 
    def __init__(self, cls):
        # This is only called once per decorated class type so self.cls 
        # should remember the class that called it so it can generate
        # unique objects of that class later on (in __call__)
        self.cls = cls
 
    # NOTE: on a decorator "__call__" MUST be implemented
    # this function will automatically be called each time a decorated
    # object is cloned/created
    def __call__(self):
        # Create an instance of the class here so a unique object can be 
        # sent back to the constructor
        self.instance = self.cls()
        # Assign the private functions back to the unique class 
        # (which is the whole point of this decorator)
        self.instance.Clone = self._clone_func
        self.instance.Copy = self._copy_func
        # Finally, send the unique object back to the object's constructor
        return self.instance        
 
@Prototype
class Concrete:
    def __init__(self):
        # Test value to see if objects are independently generated as
        # well as if they properly copy from one another
        self.somevar = 0
 
@Prototype
class another:
    def __init__(self):
        self.somevar = 50
 
if __name__ == '__main__':
    print "Creating A"
    a = Concrete()
    print "Cloning A to B"
    b = a.Clone()
    print "A and B somevars"
    print a.somevar
    print b.somevar
    print "Changing A somevar to 10..."
    a.somevar = 10
    print "A and B somevars"
    print a.somevar
    print b.somevar
 
    print "Creating another kind of class as C"
    c = another()
    print "Cloning C to D"
    d = c.Clone()
    print "Changing C somevar to 100"
    c.somevar = 100
    print "C and D somevars"
    print c.somevar
    print d.somevar

Output:

Creating A
Cloning A to B
A and B somevars
0
0
Changing A somevar to 10...
A and B somevars
10
0
Creating another kind of class as C
Cloning C to D
Changing C somevar to 100
C and D somevars
100
50

Print version

Control the access to an object.

The example creates first an interface against which the pattern creates the classes. This interface contains only one method to display the image, called displayImage(), that has to be coded by all classes implementing it.

The proxy class ProxyImage is running on another system than the real image class itself and can represent the real image RealImage over there. The image information is accessed from the disk. Using the proxy pattern, the code of the ProxyImage avoids multiple loading of the image, accessing it from the other system in a memory-saving manner.

Implementation in C#
using System;
 
namespace Proxy
{
    class Program
    {
        interface IImage
        {
            void Display();
        }
 
        class RealImage : IImage
        {
            public RealImage(string fileName)
            {
                FileName = fileName;
                LoadFromFile();
            }
 
            private void LoadFromFile()
            {
                Console.WriteLine("Loading " + FileName);
            }
 
            public String FileName { get; private set; }
 
            public void Display()
            {
                Console.WriteLine("Displaying " + FileName);
            }
        }
 
        class ProxyImage : IImage
        {
            public ProxyImage(string fileName)
            {
                FileName = fileName;
            }
 
            public String FileName { get; private set; }
 
            private IImage image;
 
            public void Display()
            {
                if (image == null)
                    image = new RealImage(FileName);
                image.Display();
            }
        }
 
        static void Main(string[] args)
        {
            IImage image = new ProxyImage("HiRes_Image");
            for (int i = 0; i < 10; i++)
                image.Display();
        }
    }
}

The program's output is:

Loading HiRes_Image
Displaying HiRes_Image
Displaying HiRes_Image
Displaying HiRes_Image
Displaying HiRes_Image
Displaying HiRes_Image
Displaying HiRes_Image
Displaying HiRes_Image
Displaying HiRes_Image
Displaying HiRes_Image
Displaying HiRes_Image
Implementation in Java

The following Java example illustrates the "virtual proxy" pattern. The ProxyImage class is used to access a remote method.

interface Image {
    public void displayImage();
}
 
//on System A
class RealImage implements Image {
 
    private String filename = null;
    /**
     * Constructor
     * @param FILENAME
     */
    public RealImage(final String FILENAME) {
        filename = FILENAME;
        loadImageFromDisk();
    }
 
    /**
     * Loads the image from the disk
     */
    private void loadImageFromDisk() {
        System.out.println("Loading    " + filename);
    }
 
    /**
     * Displays the image
     */
    public void displayImage() {
        System.out.println("Displaying " + filename);
    }
 
}
 
//on System B
class ProxyImage implements Image {
 
    private RealImage image = null;
    private String filename = null;
    /**
     * Constructor
     * @param FILENAME
     */
    public ProxyImage(final String FILENAME) {
        filename = FILENAME;
    }
 
    /**
     * Displays the image
     */
    public void displayImage() {
        if (image == null) {
           image = new RealImage(filename);
        }
        image.displayImage();
    }
 
}
 
class ProxyExample {
 
   /**
    * Test method
    */
   public static void main(String[] args) {
        final Image IMAGE1 = new ProxyImage("HiRes_10MB_Photo1");
        final Image IMAGE2 = new ProxyImage("HiRes_10MB_Photo2");    
 
        IMAGE1.displayImage(); // loading necessary
        IMAGE1.displayImage(); // loading unnecessary
        IMAGE2.displayImage(); // loading necessary
        IMAGE2.displayImage(); // loading unnecessary
        IMAGE1.displayImage(); // loading unnecessary
    }
 
}

The program's output is:

Loading    HiRes_10MB_Photo1
Displaying HiRes_10MB_Photo1
Displaying HiRes_10MB_Photo1
Loading    HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo2
Displaying HiRes_10MB_Photo1

Singleton

The term Singleton refers to an object that can be instantiated only once. Use the Singleton Design pattern if:

  • You need global access to a resource, like logging ...
  • You need only one instance of a utility class, do not want to create lots of objects.

In some applications, it is appropriate to enforce a single instance of an object, for example: window managers, print spoolers, database access, and filesystems.

Refresh Java object creation

In programming languages like Java, you have to create an instance of an object type (commonly called a Class) to use it throughout the code. Let's take for example this code:

  1.  Animal dog;                  // Declaring the object type
    
  2.  dog = new Animal();          // Instantiating an object
    

This can also be written in a single line to save space.

  1.  Animal dog = new Animal();   // Both declaring and instantiating
    

At times, you need more than one instance of the same object type. You can create multiple instances of the same object type. Take for instance:

  1. Animal dog = new Animal();
    
  2. Animal cat = new Animal();
    

Now that I have two instances of the same object type, I can use both dog and cat instances separately in my program. Any changes to dog would not effect the cat instance because both of them have been created in separate memory spaces. To see whether these objects actually are different we do something like this:

  1.  System.out.println(dog.equals(cat));  // output: false
    

The code returns false because both the objects are different. Contrary to this behavior, the Singleton behaves differently. A Singleton object type can not be instantiated yet you can obtain an instance of the object type. Let us create a normal object using Java.

  1.  class NormalObject {
    
  2.      public NormalObject() {
    
  3.      }
    
  4.  }
    

What we have done here is that we have created a class (object type) to identify our object. Within the braces of the class is a single method with the same name as that of the class (methods can be identified by the usage of parentheses at the end of their names). Methods that have the same name as that of the class and that do not have a return type are called Constructors in OOP syntax. To create an instance of the class, the code can not be much simpler.

  1.  class TestHarness {
    
  2.      public static void main(String[] args) {
    
  3.           NormalObject object = new NormalObject();
    
  4.      }
    
  5.  }
    

Note that to encourage the instantiation of this object, the constructor is called. The constructor as in this case can be called outside the class paranthesis and into another class definition because it is declared a public accessor while creating the Constructor in the above example.

Creating singleton object 

Now we will create the Singleton object. You just have to change one thing to adhere to the Singleton design pattern: Make your Constructor's accessor private.

  1.  class SingletonObject {
    
  2.      private SingletonObject() {
    
  3.      }
    
  4.  }
    

Notice the constructor's accessor. This time around it has been declared private. Just by changing it to private, you have applied a great change to your object type. Now you can not instantiate the object type. Go ahead try out the following code.

  1.  class TestHarness {
    
  2.      public static void main(String[] args) {
    
  3.           SingletonObject object = new SingletonObject();
    
  4.      }
    
  5.  }
    

The code returns an error because private class members can not be accessed from outside the class itself and in another class. This way you have disabled the instantiation process for an object type. However, you have to find a way of obtaining an instance of the object type. Let's do some changes here.

  1.  class SingletonObject {
    
  2.      private static SingletonObject object;
    
  3.  
    
  4.      private SingletonObject() {
    
  5.         // Instantiate the object.
    
  6.      }
    
  7.  
    
  8.      public static SingletonObject getInstance() {
    
  9.          if (object == null) {
    
  10.              object = new SingletonObject(); // Create the object for the first and last time
    
  11.          }
    
  12.          return object;
    
  13.      }
    
  14.  }
    

The changes involve adding a static class field called object and a public static method that can be accessible outside the scope of the class by using the name of the Class. To see how we can obtain the instance, lets code:

  1.  class TestHarness {
    
  2.      public static void main(String[] args) {
    
  3.           SingletonObject object = SingletonObject.getInstance();
    
  4.      }
    
  5.  }
    

This way you control the creation of objects derived from your class. But we have still not unearthed the final and interesting part of the whole process. Try getting multiple objects and see what happens.

  1.  class TestHarness {
    
  2.      public static void main(String[] args) {
    
  3.           SingletonObject object1 = SingletonObject.getInstance();
    
  4.           SingletonObject object2 = SingletonObject.getInstance();
    
  5.      }
    
  6.  }
    

Unlike multiple instances of normal object types, multiple instances of a Singleton are all actually the same object instance. To validate the concept in Java, we try:

  1.  System.out.println(object1.equals(object2)); // output: true
    

The code returns true because both object declarations are actually referring to the same object. So, in summarizing the whole concept, a Singleton can be defined as an object that can not be instantiated more than once. Typically it is obtained using a static custom implementation.

Singleton & Multi Threading

Java uses multi threading concept, to run/execute any program. Consider the class SingletonObject discussed above. Call to the method getInstance() by more than one thread at any point of time might create two instances of SingletonObject thus defeating the whole purpose of creating the singleton pattern. To make singelton thread safe, we have three options: 1. Synchronize the method getInstance(), which would look like:

  1.    // Only one thread can enter and stay in this method at a time
    
  2.    public synchronized static Singleton getInstance() {
    
  3.         if (instance == null) {
    
  4.             instance = new Singleton();
    
  5.         }
    
  6.         return instance;
    
  7.     }
    

Synchronizing the method guarantees that the method will never be called twice at the same time. The problem with the above code is that 'synchronization' is expensive. So practically the above code should never be used. 2. Another approach would be to create a singleton instance as shown below:

  1.  class SingletonObject {
    
  2.      public final static SingletonObject object;
    
  3.      private static String lockObj = "Lock"; // Use for locking
    
  4.  
    
  5.      private SingletonObject() {
    
  6.         // Exists only to avoid instantiation.
    
  7.      }
    
  8.  
    
  9.      public static SingletonObject getInstance() {
    
  10.          if (object != null) {
    
  11.              return object;
    
  12.          } else {
    
  13.             // Start a synchronized block, only one thread can enter and stay in the block one at a time
    
  14.             synchronized(lockObj) {
    
  15.                 if (object == null) {
    
  16.                    object = new SingletonObject();
    
  17.                 }
    
  18.             } // End of synchronized block
    
  19.             return object;
    
  20.          }
    
  21.      }
    
  22.  }
    

The above code will be much faster; once the singleton instance is created no synchronization is needed. It just returns the same instance reference. 3. Use static initialization block to create the instance. The JVM makes sure that the static block is executed once when the class is loaded.

  1.  class SingletonObject {
    
  2.      public final static SingletonObject object;
    
  3.  
    
  4.      static {
    
  5.         ...
    
  6.         object = new SingletonObject();
    
  7.      }
    
  8.  
    
  9.      private SingletonObject() {
    
  10.         // Exists only to avoid instantiation.
    
  11.      }
    
  12.  
    
  13.      public static SingletonObject getInstance() {
    
  14.          return object;
    
  15.      }    
    
  16.  }
    

Examples

In Java, the class java.util.Calendar is a singleton. Its constructor is protected and you can get an instance by calling the getInstance() method.

Cost

Beware of this design pattern! It creates the same issues than the global variables in procedural programming so it can be hard to debug.

Creation

It can be expensive. You may have to refactor all the instantiations of the class, unless the class is new.

Maintenance

There is no additional cost.

Removal

This pattern can be easily removed as automatic refactoring operations can easily remove its existence.

Advises

  • Name the method getInstance() to indicate the use of the pattern to the other developers.
  • Use this design pattern for frozen data like configuration or external data. You will not have debugging issues.

Implementation

Implementation in Scala

The Scala programming language supports Singleton objects out-of-the-box. The 'object' keyword creates a class and also defines a singleton object of that type. Singletons are declared just like classes except "object" replaces the keyword "class".

object MySingleton {
  println("Creating the singleton")
  val i : Int = 0
}
Implementation in Java

Traditional simple way using synchronization

This solution is thread-safe without requiring special language constructs:

public class Singleton {
  public volatile static Singleton singleton; // volatile is needed so that multiple thread can reconcile the instance
  private Singleton(){}
  public static Singleton getSingleton() { // synchronized keyword has been removed from here
    if (singleton == null) { // needed because once there is singleton available no need to aquire monitor again & again as it is costly
      synchronized(Singleton.class) {
        if (singleton == null) { // this is needed if two threads are waiting at the monitor at the time when singleton was getting instantiated
          singleton = new Singleton();
        }
      }
    }
    return singleton;
  }
}

Initialization on Demand Holder Idiom

This technique is as lazy as possible, and works in all known versions of Java. It takes advantage of language guarantees about class initialization, and will therefore work correctly in all Java-compliant compilers and virtual machines. The nested class is referenced when getInstance() is called making this solution thread-safe without requiring special language constructs.

 public class Singleton {
   // Private constructor prevents instantiation from other classes
   private Singleton() {}
 
   /**
    * SingletonHolder is loaded on the first execution of Singleton.getInstance()
    * or the first access to SingletonHolder.INSTANCE, not before.
    */
   private static class SingletonHolder {
     private static final Singleton INSTANCE = new Singleton();
   }
 
   public static Singleton getInstance() {
     return SingletonHolder.INSTANCE;
   }
 }

The Enum-way

In the second edition of his book "Effective Java" Joshua Bloch claims that "a single-element enum type is the best way to implement a singleton" for any Java that supports enums. The use of an enum is very easy to implement and has no drawbacks regarding serializable objects, which have to be circumvented in the other ways.

 public enum Singleton {
   INSTANCE;
 }
Implementation in D

Singleton pattern in D programming language

import std.stdio;
import std.string;
class Singleton(T) {
    private static T instance;
    public static T opCall() {
        if(instance is null) {
            instance = new T;
        }
        return instance;
    }
}
class Foo {
    public this() {
        writefln("Foo Constructor");
    }
}
void main(){
    Foo a = Singleton!(Foo)();
    Foo b = Singleton!(Foo)();
}

Or in this manner

// this class should be in a package to make private this() not visible
class Singleton {
    private static Singleton instance;
 
    public static Singleton opCall() {
        if(instance is null) {
            instance = new Singleton();
        }
        return instance;
    }
    private this() {
        writefln("Singleton constructor");
    }
}
void main(){
    Singleton a = Singleton();
    Singleton b = Singleton();
}
Implementation in PHP 5

Singleton pattern in PHP 5:

<?php
class Singleton {
  // object instance
  private static $instance;
  // The protected construct prevents instantiating the class externally.  The construct can be
  // empty, or it can contain additional instructions...
  // This should also be final to prevent extending objects from overriding the constructor with
  // public.
  protected final function __construct() {
    ...
  }
  // The clone and wakeup methods prevents external instantiation of copies of the Singleton class,
  // thus eliminating the possibility of duplicate objects.  The methods can be empty, or
  // can contain additional code (most probably generating error messages in response
  // to attempts to call).
  public function __clone() {
    trigger_error('Clone is not allowed.', E_USER_ERROR);
  }
  public function __wakeup() {
    trigger_error('Deserializing is not allowed.', E_USER_ERROR);
  }
  // This method must be static, and must return an instance of the object if the object
  // does not already exist.
  public static function getInstance() {
    if (!self::$instance instanceof self) {
      self::$instance = new self;
    }
    return self::$instance;
  }
  // One or more public methods that grant access to the Singleton object, and its private
  // methods and properties via accessor methods.
  public function doAction() {
    ...
  }
}
// usage
Singleton::getInstance()->doAction();
?>
Implementation in ActionScript 3.0

Private constructors are not available in ActionScript 3.0 - which prevents the use of the ActionScript 2.0 approach to the Singleton Pattern. Many different AS3 Singleton implementations have been published around the web.

package {
public class Singleton  {
private static var _instance:Singleton = new Singleton();
public function Singleton () {
           if (_instance){
       throw new Error(
                            "Singleton can only be accessed through Singleton.getInstance()"
                        );
                    }
}
public static function getInstance():Singleton {
return _instance;
}
}
}
Implementation in Objective-C

A common way to implement a singleton in Objective-C is the following:

@interface MySingleton : NSObject
{
}
+ (MySingleton *)sharedSingleton;
@end
@implementation MySingleton
+ (MySingleton *)sharedSingleton
{
  static MySingleton *sharedSingleton;
 
  @synchronized(self)
  {
    if (!sharedSingleton)
      sharedSingleton = [[MySingleton alloc] init];
 
    return sharedSingleton;
  }
}
@end

If thread-safety is not required, the synchronization can be left out, leaving the +sharedSingleton method like this:

+ (MySingleton *)sharedSingleton
{
  static MySingleton *sharedSingleton;
  if (!sharedSingleton)
    sharedSingleton = [[MySingleton alloc] init];
  return sharedSingleton;
}

This pattern is widely used in the Cocoa frameworks (see for instance, NSApplication, NSColorPanel, NSFontPanel or NSWorkspace, to name but a few). Some may argue that this is not, strictly speaking, a Singleton, because it is possible to allocate more than one instance of the object. A common way around this is to use assertions or exceptions to prevent this double allocation.

@interface MySingleton : NSObject
{
}
+ (MySingleton *)sharedSingleton;
@end
@implementation MySingleton
static MySingleton *sharedSingleton;
+ (MySingleton *)sharedSingleton
{
  @synchronized(self)
  {
    if (!sharedSingleton)
      [[MySingleton alloc] init];
 
    return sharedSingleton;
  }
}
+(id)alloc
{
  @synchronized(self)
  {
    NSAssert(sharedSingleton == nil, @"Attempted to allocate a second instance of a singleton.");
    sharedSingleton = [super alloc];
    return sharedSingleton;
  }
}
@end

There are alternative ways to express the Singleton pattern in Objective-C, but they are not always as simple or as easily understood, not least because they may rely on the -init method returning an object other than self. Some of the Cocoa "Class Clusters" (e.g. NSString, NSNumber) are known to exhibit this type of behaviour. Note that @synchronized is not available in some Objective-C configurations, as it relies on the NeXT/Apple runtime. It is also comparatively slow, because it has to look up the lock based on the object in parentheses.

Implementation in C#

The simplest of all is:

public class Singleton
{
    // The combination of static and readonly makes the instantiation
    // thread safe.  Plus the constructor being protected (it can be
    // private as well), makes the class sure to not have any other
    // way to instantiate this class than using this member variable.
    public static readonly Singleton Instance = new Singleton();
    // Protected constructor is sufficient to avoid other instantiation
    // This must be present otherwise the compiler provides a default
    // public constructor
    //
    protected Singleton()
    {
    }
}

This example is thread-safe with lazy initialization.

/// Class implements singleton pattern.
public class Singleton
{
    //Use Lazy<T> to lazily initialize the class and provide thread-safe access
    private static readonly Lazy<Singleton> _lazyInstance = new Lazy<Singleton>(() => new Singleton());
 
    public static Singleton Instance 
    { 
        get { return _lazyInstance.Value; } 
    }
 
    private Singleton() { }
}

Example in C# 2.0 (thread-safe with lazy initialization) Note: This is not a recommended implementation because "TestClass" has a default public constructor, and that violates the definition of a Singleton. A proper Singleton must never be instantiable more than once. More about generic singleton solution in C#: http://www.c-sharpcorner.com/UploadFile/snorrebaard/GenericSingleton11172008110419AM/GenericSingleton.aspx

/// Parent for singleton
/// <typeparam name="T">Singleton class</typeparam>
  public class Singleton<T> where T : class, new()
  {
    protected Singleton() { }
    private sealed class SingletonCreator<S> where S : class, new()
    {
      private static readonly S instance = new S();
      //explicit static constructor to disable beforefieldinit      
      static SingletonCreator() { }
      public static S CreatorInstance
      {
        get { return instance; }
      }
    }
    public static T Instance
    {
      get { return SingletonCreator<T>.CreatorInstance; }
    }
  }
/// Concrete Singleton
public