Design Patterns
239 pág.

Design Patterns


DisciplinaEngenharia de Software I5.641 materiais57.743 seguidores
Pré-visualização50 páginas
as a final step, but as far as the Abstract Factory is
concerned, the product gets returned immediately.
\u2022 Builder often builds a Composite.
\u2022 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.
\u2022 Sometimes creational patterns are complementary: Builder can use one of the other patterns to implement which
components are built. Abstract Factory, Builder, and Prototype can use Singleton in their implementations.
\u2022 Builders are good candidates for a fluent interface.
Examples
Java
 /** "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"); }
 }
Builder pattern 25
 
 /** "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 Waiter {
 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();
 }
 }
 
 
 /** A customer ordering a pizza. */
 class BuilderExample {
 public static void main(String[] args) {
 Waiter waiter = new Waiter();
 PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder();
 PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder();
 
 waiter.setPizzaBuilder( hawaiianPizzaBuilder );
 waiter.constructPizza();
 
 Pizza pizza = waiter.getPizza();
 }
 }
C#
using System;
namespace BuilderPattern
{
 // Builder - abstract interface for creating objects (the product, in
 this case)
 abstract class PizzaBuilder
Builder pattern 26
 {
 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";
 }
Builder pattern 27
 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;
 }
Builder pattern 28
 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();
 }
 }
}
C++
#include <string>
#include <iostream>
using namespace std;
// &quot;Product&quot;
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 << &quot;Pizza with &quot; << dough_ << &quot; dough, &quot; << sauce_ << &quot; sauce and &quot;
 << topping_ << &quot; topping. Mmm.&quot; << endl;
 }
private:
Builder pattern 29
 string dough_;
 string sauce_;
 string topping_;
};
// &quot;Abstract Builder&quot;
class PizzaBuilder {
public:
 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(&quot;cross&quot;);
 }
 void buildSauce() {
 pizza_.sauce(&quot;mild&quot;);
 }
 void buildTopping() {
 pizza_.topping(&quot;ham+pineapple&quot;);
 }
};
class SpicyPizzaBuilder : public PizzaBuilder {
public:
 void buildDough() {
 pizza_.dough(&quot;pan baked&quot;);
 }
 void buildSauce() {
 pizza_.sauce(&quot;hot&quot;);
 }
Builder pattern 30
 void buildTopping() {
 pizza_.topping(&quot;pepperoni+salami&quot;);
 }
};
//----------------------------------------------------------------
class Cook {
public:
 Cook()
 : pizzaBuilder_(nullptr)
 { }
 ~Cook() {