Pattern > Créateurs

Pattern > Structuraux

Pattern > Comportementaux

Références

L'actualité

Librairie

L'information

Design pattern > Créateurs > Builder

Ce patron sépare le processus de construction d'un objet du résultat obtenu. Permet d'utiliser le m ême processus pour obtenir différents résultats. C'est une alternative au pattern factory. Au lieu d'une méthode pour créer un objet, à laquelle est passée un ensemble de paramètres, la classe factory comporte une méthode pour créer un objet - le builder. Cet objet comporte des propriétés qui peuvent être modifiées et une méthode pour créer l'objet final en tenant compte de toutes les propriétés. Ce pattern est particulièrement utile quand il y a de nombreux paramètres de création, presque tous optionnels.


Builder (VehicleBuilder)
  • specifies an abstract interface for creating parts of a Product object
ConcreteBuilder (MotorCycleBuilder, CarBuilder, ScooterBuilder)
  • constructs and assembles parts of the product by implementing the Builder interface
  • defines and keeps track of the representation it creates
  • provides an interface for retrieving the product
Director (Shop)
  • constructs an object using the Builder interface
Product (Vehicle)
  • represents the complex object under construction. ConcreteBuilder builds the product's internal representation and defines the process by which it's assembled
  • includes classes that define the constituent parts, including interfaces for assembling the parts into the final result
			
---------------------------
Vehicle Type: Scooter
 Frame  : Scooter Frame
 Engine : none
 #Wheels: 2
 #Doors : 0

---------------------------
Vehicle Type: Car
 Frame  : Car Frame
 Engine : 2500 cc
 #Wheels: 4
 #Doors : 4

---------------------------
Vehicle Type: MotorCycle
 Frame  : MotorCycle Frame
 Engine : 500 cc
 #Wheels: 2
 #Doors : 0