Builder

El patró de Builder (Constructor) és un patró de disseny. La intenció és fer una abstracció de la construcció d'objectes que tenen diferents implementacions d'aquests. Permetent construir diferents representacions dels objectes centralitzant aquest procés en un punt.

Sovint, el Builder construeix el Composite, un patró d'estructura.

Diagrama de classes

[modifica]

Builder

[modifica]

Interfície abstracta per crear objectes (productes).

Concrete Builder

[modifica]

Implementació del Builder Construeix i reuneix les parts necessàries per construir els productes.

Director

[modifica]

La classe Director és la responsable de gestionar la seqüència correcta de la creació d'objectes. Es rep un Builder concret com un paràmetre i executa les operacions necessàries en ella.

Producte

[modifica]

És l'objecte final que crearà el Director usant el Builder

Exemple

[modifica]

Java

[modifica]
/** "Product" */
class Pizza {
	private String dough = "";
	private String sauce = "";
	private String topping = "";

	public void setDough(String dough) {
		this.dough = dough;
	}

	public void setSauce(String sauce) {
		this.sauce = sauce;
	}

	public void setTopping(String topping) {
		this.topping = topping;
	}
}

/** "Abstract Builder" */
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();
}
/** "ConcreteBuilder" */
class HawaiianPizzaBuilder extends PizzaBuilder {
	public void buildDough() {
		pizza.setDough("cross");
	}

	public void buildSauce() {
		pizza.setSauce("mild");
	}

	public void buildTopping() {
		pizza.setTopping("ham+pineapple");
	}
}

/** "ConcreteBuilder" */
class SpicyPizzaBuilder extends PizzaBuilder {
	public void buildDough() {
		pizza.setDough("pan baked");
	}

	public void buildSauce() {
		pizza.setSauce("hot");
	}

	public void buildTopping() {
		pizza.setTopping("pepperoni+salami");
	}
}
/** "Director" */
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();
	}
}

/** Donant el tipus de pizza a construir. */
public class BuilderExample {
	public static void main(String[] args) {
		Cook cook = new Cook();
		PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder();
		PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();

		cook.setPizzaBuilder(hawaiianPizzaBuilder);
		cook.constructPizza();

		Pizza pizza = cook.getPizza();
	}
}

Python

[modifica]
# Producte
class Pizza:
 def __init__(self):
 self.dough = None
 self.sauce = None
 self.topping = None

 def set_dough(self, dough):
 self.dough = dough

 def set_sauce(self, sauce):
 self.sauce = sauce

 def set_topping(self, topping):
 self.topping = topping

# Abstract Builder
class PizzaBuilder:
 def __init__(self):
 self.pizza = None

 def get_pizza(self):
 return self.pizza

 def create_new_pizza_product(self):
 self.pizza = Pizza()

# ConcreteBuilder
class HawaiianPizzaBuilder(PizzaBuilder):
 def build_dough(self):
 self.pizza.set_dough("cross")

 def build_sauce(self):
 self.pizza.set_sauce("mild")

 def build_topping(self):
 self.pizza.set_topping("ham+pineapple")

# Director
class Cook:
 def __init__(self):
 self.pizza_builder = None

 def set_pizza_builder(self, pb):
 self.pizza_builder = pb

 def get_pizza(self):
 return self.pizza_builder.get_pizza()

 def construct_pizza(self):
 self.pizza_builder.create_new_pizza_product()
 self.pizza_builder.build_dough()
 self.pizza_builder.build_sauce()
 self.pizza_builder.build_topping()

# A given type of pizza being constructed.
if __name__ == '__main__':
 cook = Cook()
 cook.set_pizza_builder(HawaiianPizzaBuilder())
 cook.construct_pizza()
 pizza = cook.get_pizza()

Enllaços externs

[modifica]
  • Article de JavaWorld Construir interfícies sense getters i setters Arxivat 2009-04-29 a Wayback Machine. (Allen Holub) demostracions el codi font de Java complet per a un Builder.(anglès)