Przejdź do zawartości

Kody źródłowe/Budowniczy (wzorzec projektowy)

Z Wikibooks, biblioteki wolnych podręczników.
Budowniczy (wzorzec projektowy) • Kod źródłowy
Budowniczy (wzorzec projektowy)
Kod źródłowy
Kody źródłowe programów stosujących wzorzec projektowy budowniczego
Wikipedia
Zobacz w Wikipedii hasło Budowniczy

Java

[edytuj]
/** "Produkt" */
class Pizza {
  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; }
}


/** "Abstrakcyjny budowniczy" */
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();
}

/** "Konkretny budowniczy" */
class HawaiianPizzaBuilder extends PizzaBuilder {
  public void buildDough()   { pizza.setDough("cross"); }
  public void buildSauce()   { pizza.setSauce("mild"); }
  public void buildTopping() { pizza.setTopping("ham+pineapple"); }
}

/** "Konkretny budowniczy" */
class SpicyPizzaBuilder extends PizzaBuilder {
  public void buildDough()   { pizza.setDough("pan baked"); }
  public void buildSauce()   { pizza.setSauce("hot"); }
  public void buildTopping() { pizza.setTopping("pepperoni+salami"); }
}


/** "Nadzorca" */
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();
  }
}


/** Klient zamawiający pizzę. */
class BuilderExample {
  public static void main(String[] args) {
    Waiter waiter = new Waiter();
    PizzaBuilder hawaiian_pizzabuilder = new HawaiianPizzaBuilder();
    PizzaBuilder spicy_pizzabuilder = new SpicyPizzaBuilder();

    waiter.setPizzaBuilder( hawaiian_pizzabuilder );
    waiter.constructPizza();

    Pizza pizza = waiter.getPizza();
  }
}

C#

[edytuj]
//Implementation in C#.
class Pizza
{
    string dough;
    string sauce;
    string topping;
    public string Dough   { get { return dough; }   set { dough = value; } }
    public string Sauce   { get { return sauce; }   set { sauce = value; } }
    public string Topping { get { return topping; } set { topping = value; } }

    public override string ToString()
    {
        return string.Format(
                "Pizza with Dough as {0}, Sauce as {1} and Topping as {2}",
                Dough,
                Sauce,
                Topping);
    }
}

//Abstract Builder
abstract class PizzaBuilder
{
    protected Pizza pizza;
    public Pizza Pizza { get { return pizza; } }
    public void CreateNewPizza() { pizza = new Pizza(); }

    public abstract void BuildDough();
    public abstract void BuildSauce();
    public abstract void BuildTopping();
}

//Concrete Builder
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
class SpicyPizzaBuilder : PizzaBuilder
{
    public override void BuildDough()   { pizza.Dough   = "pan baked"; }
    public override void BuildSauce()   { pizza.Sauce   = "hot"; }
    public override void BuildTopping() { pizza.Topping = "pepparoni+salami"; }
}

/** "Director" */
class Waiter
{
    private PizzaBuilder pizzaBuilder;

    public PizzaBuilder PizzaBuilder
    {
        get { return pizzaBuilder; } set { pizzaBuilder = value; }
    }
    public Pizza Pizza { get { return pizzaBuilder.Pizza; } }

    public void ConstructPizza()
    {
        pizzaBuilder.CreateNewPizza();
        pizzaBuilder.BuildDough();
        pizzaBuilder.BuildSauce();
        pizzaBuilder.BuildTopping();
    }
}

public class TestPizza {
    private static void BuildAndDisplayPizza(Waiter waiter)
    {
        waiter.ConstructPizza();
        System.Console.WriteLine(waiter.Pizza);
    }

    public static void Main()
    {
        Waiter waiter = new Waiter();

        waiter.PizzaBuilder = new HawaiianPizzaBuilder();
        BuildAndDisplayPizza(waiter);

        waiter.PizzaBuilder = new SpicyPizzaBuilder();
        BuildAndDisplayPizza(waiter);
    }
}

C++

[edytuj]
// Implementation in C++.

#include <iostream>
#include <memory>
#include <string>

// Product
class Pizza
{
private:
    std::string dough;
    std::string sauce;
    std::string topping;
    
public:
    Pizza() { }
    ~Pizza() { }

    void SetDough(const std::string& d) { dough = d; };
    void SetSauce(const std::string& s) { sauce = s; };
    void SetTopping(const std::string& t) { topping = t; }

    void ShowPizza()
    {
        std::cout << " Yummy !!!" << std::endl
        << "Pizza with Dough as " << dough
        << ", Sauce as " << sauce
        << " and Topping as " << topping
        << " !!! " << std::endl;
    }
};
// Abstract Builder
class PizzaBuilder
{
protected:
    std::auto_ptr<Pizza> pizza;
public:
    PizzaBuilder() {}
    virtual ~PizzaBuilder() {}
    std::auto_ptr<Pizza> GetPizza() { return pizza; }

    void createNewPizzaProduct() { pizza.reset (new Pizza); }

    virtual void buildDough()=0;
    virtual void buildSauce()=0;
    virtual void buildTopping()=0;

};

// ConcreteBuilder
class HawaiianPizzaBuilder : public PizzaBuilder
{
public:
    HawaiianPizzaBuilder() : PizzaBuilder() {}
    ~HawaiianPizzaBuilder(){}

    void buildDough() { pizza->SetDough("cross"); }
    void buildSauce() { pizza->SetSauce("mild"); }
    void buildTopping() { pizza->SetTopping("ham and pineapple"); }
};
// ConcreteBuilder
class SpicyPizzaBuilder : public PizzaBuilder
{
public:
    SpicyPizzaBuilder() : PizzaBuilder() {}
    ~SpicyPizzaBuilder() {}

    void buildDough() { pizza->SetDough("pan baked"); }
    void buildSauce() { pizza->SetSauce("hot"); }
    void buildTopping() { pizza->SetTopping("pepperoni and salami"); }
};

// Director
class Waiter
{
private:
    PizzaBuilder* pizzaBuilder;
public:
    Waiter() : pizzaBuilder(NULL) {}
    ~Waiter() { }
    
    void SetPizzaBuilder(PizzaBuilder* b) { pizzaBuilder = b; }
    std::auto_ptr<Pizza> GetPizza() { return pizzaBuilder->GetPizza(); }
    void ConstructPizza()
    {
        pizzaBuilder->createNewPizzaProduct();
        pizzaBuilder->buildDough();
        pizzaBuilder->buildSauce();
        pizzaBuilder->buildTopping();
    }
};
   
// A customer ordering two pizza.
int main()
{ 
    Waiter waiter;
   
    HawaiianPizzaBuilder hawaiianPizzaBuilder;
    waiter.SetPizzaBuilder (&hawaiianPizzaBuilder);
    waiter.ConstructPizza();
    std::auto_ptr<Pizza> pizza = waiter.GetPizza();
    pizza->ShowPizza();
    
    SpicyPizzaBuilder spicyPizzaBuilder;
    waiter.SetPizzaBuilder(&spicyPizzaBuilder);
    waiter.ConstructPizza();
    pizza = waiter.GetPizza();
    pizza->ShowPizza();
  
    return EXIT_SUCCESS;
}