天天看点

Head First工厂模式qt类图定义设计原则代码

工厂模式

  • 类图
    • 简单工厂模式
    • 抽象工厂模式
  • 定义
  • 设计原则
  • 代码
    • PizzaIngredientFactory类:
    • NYPizzaIngredientFactory 类:
    • NYPizzaStore类:
    • Pizza类:
    • Cheese类:
      • RegglanoCheese 类:
    • Clams类:
      • FreshClams 类:
    • Dough类:
      • ThinCrustDough 类:
    • Sauce类:
      • MarinarsSauce 类:
    • Veggies类:
      • Garlic 类:
      • Mushroom类:
      • RedPepper 类:
      • Onion 类:
    • Pepperoni类:
      • SlicedPepperoni 类:
    • ChessePizza类:
    • 测试:

类图

简单工厂模式

Head First工厂模式qt类图定义设计原则代码

抽象工厂模式

Head First工厂模式qt类图定义设计原则代码
Head First工厂模式qt类图定义设计原则代码

定义

定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法模式让一个类的实例化延迟到其子类。

设计原则

依赖倒置原理

代码

PizzaIngredientFactory类:

#ifndef PIZZAINGREDIENTFACTORY_H
#define PIZZAINGREDIENTFACTORY_H

#include "dough.h"
#include "sauce.h"
#include "cheese.h"
#include "veggies.h"
#include "pepperoni.h"
#include "clams.h"

#include <QVector>

class PizzaIngredientFactory
{
public:
    virtual Dough* createDough() = 0;
    virtual Sauce* createSauce() = 0;
    virtual Cheese* createCheese() = 0;
    virtual QVector<Veggies *> createVeggies() = 0;
    virtual Pepperoni* createPepperoni() = 0;
    virtual Clams* createClam() = 0;
};

#endif // PIZZAINGREDIENTFACTORY_H

           

NYPizzaIngredientFactory 类:

#ifndef NYPIZZAINGREDIENTFACTORY_H
#define NYPIZZAINGREDIENTFACTORY_H

#include "pizzaingredientfactory.h"

class NYPizzaIngredientFactory : public PizzaIngredientFactory
{
public:
    Dough* createDough() override;
    Sauce* createSauce() override;
    Cheese* createCheese() override;
    QVector<Veggies *> createVeggies() override;
    Pepperoni* createPepperoni() override;
    Clams* createClam() override;
};

#endif // NYPIZZAINGREDIENTFACTORY_H

           
#include "nypizzaingredientfactory.h"
#include "thincrustdough.h"
#include "marinarssauce.h"
#include "garlic.h"
#include "onion.h"
#include "mushroom.h"
#include "redpepper.h"
#include "regglanocheese.h"
#include "freshclams.h"
#include "slicedpepperoni.h"

Dough* NYPizzaIngredientFactory::createDough()
{
    return new ThinCrustDough();
}

Sauce* NYPizzaIngredientFactory::createSauce()
{
    return new MarinarsSauce();
}

Cheese* NYPizzaIngredientFactory::createCheese()
{
    return new RegglanoCheese();
}

QVector<Veggies *> NYPizzaIngredientFactory::createVeggies()
{
    QVector<Veggies *> VeggiesVector;
    VeggiesVector.append(new Garlic);
    VeggiesVector.append(new Onion);
    VeggiesVector.append(new Mushroom);
    VeggiesVector.append(new RedPepper);
    return VeggiesVector;
}

Pepperoni* NYPizzaIngredientFactory::createPepperoni()
{
    return new SlicedPepperoni();
}

Clams* NYPizzaIngredientFactory::createClam()
{
    return new FreshClams();
}


           

NYPizzaStore类:

#ifndef NYPIZZASTORE_H
#define NYPIZZASTORE_H

#include "pizzastore.h"

class NYPizzaStore : public PizzaStore
{
public:
    Pizza* createPizza(QString type) override;
};

#endif // NYPIZZASTORE_H

           
#include "nypizzastore.h"
#include "nypizzaingredientfactory.h"
#include "chessepizza.h"
#include "pepperonipizza.h"
#include "clampizza.h"
#include "veggiepizza.h"

Pizza* NYPizzaStore::createPizza(QString type)
{
    Pizza *pizza = nullptr;
    PizzaIngredientFactory *ingredientFactory =
            new NYPizzaIngredientFactory();

    if(type == "cheese") {

        pizza = new ChessePizza(ingredientFactory);
        pizza->setName("New York Style Cheese Pizza");

    } else if(type == "pepperoni") {

        pizza = new PepperoniPizza(ingredientFactory);
        pizza->setName("New York Style Pepperoni Pizza");
    } else if(type == "clam") {

        pizza = new ClamPizza(ingredientFactory);
        pizza->setName("New York Style Clam Pizza");
    } else if(type == "veggie") {

        pizza = new VeggiePizza(ingredientFactory);
        pizza->setName("New York Style Veggie Pizza");
    }
    return pizza;
}

           

Pizza类:

#ifndef PIZZA_H
#define PIZZA_H

#include <QString>
#include <QVector>

#include "dough.h"
#include "sauce.h"
#include "veggies.h"
#include "cheese.h"
#include "pepperoni.h"
#include "clams.h"

class Pizza
{
public:
    virtual void prepare() = 0;
    virtual void bake();
    virtual void cut();
    virtual void box();
    virtual QString getName();
    virtual void setName(QString name);
    virtual QString toString();

    QString name;
    Dough* dough;
    Sauce* sauce;
    QVector<Veggies *> VeggiesVector;
    Cheese* cheese;
    Pepperoni* pepperoni;
    Clams* clam;
    QVector<QString> toppings;


};

#endif // PIZZA_H

           
#include "pizza.h"

#include <QDebug>

void Pizza::bake()
{
    qDebug() << "Bake for 25 minutes at 350";
}

void Pizza::cut()
{
    qDebug() << "Cutting the pizza into diagonal slices";
}

void Pizza::box()
{
    qDebug() << "Place pizza in official OizzaStore box";
}

QString Pizza::getName()
{
    return name;
}

void Pizza::setName(QString name)
{
    this->name = name;
}

QString Pizza::toString()
{

}

           

Cheese类:

#ifndef CHEESE_H
#define CHEESE_H


class Cheese
{
public:
    Cheese();
};

#endif // CHEESE_H

           
#include "cheese.h"

Cheese::Cheese()
{

}

           

RegglanoCheese 类:

#ifndef REGGLANOCHEESE_H
#define REGGLANOCHEESE_H

#include "cheese.h"

class RegglanoCheese : public Cheese
{
public:
    RegglanoCheese();
};

#endif // REGGLANOCHEESE_H
           
#include "regglanocheese.h"

#include <QDebug>

RegglanoCheese::RegglanoCheese()
{
    qDebug() << "RegglanoCheese";

}

           

Clams类:

#ifndef CLAMS_H
#define CLAMS_H


class Clams
{
public:
    Clams();
};

#endif // CLAMS_H

           
#include "clams.h"

Clams::Clams()
{

}

           

FreshClams 类:

#ifndef FRESHCLAMS_H
#define FRESHCLAMS_H

#include "clams.h"

class FreshClams : public Clams
{
public:
    FreshClams();
};

#endif // FRESHCLAMS_H

           
#include "freshclams.h"
#include <QDebug>

FreshClams::FreshClams()
{
    qDebug() << "FreshClams";
}

           

Dough类:

#ifndef DOUGH_H
#define DOUGH_H


class Dough
{
public:
    Dough();
};

#endif // DOUGH_H

           
#include "dough.h"

Dough::Dough()
{

}

           

ThinCrustDough 类:

#ifndef THINCRUSTDOUGH_H
#define THINCRUSTDOUGH_H

#include "dough.h"

class ThinCrustDough : public Dough
{
public:
    ThinCrustDough();

};

#endif // THINCRUSTDOUGH_H
           
#include "thincrustdough.h"

#include <QDebug>

ThinCrustDough::ThinCrustDough()
{
    qDebug() <<  "ThinCrustDough";
}

           

Sauce类:

#ifndef SAUCE_H
#define SAUCE_H


class Sauce
{
public:
    Sauce();
};

#endif // SAUCE_H

           
#include "sauce.h"

Sauce::Sauce()
{

}

           

MarinarsSauce 类:

#ifndef MARINARSSAUCE_H
#define MARINARSSAUCE_H

#include "sauce.h"

class MarinarsSauce : public Sauce
{
public:
    MarinarsSauce();
};

#endif // MARINARSSAUCE_H
           
#include "marinarssauce.h"

#include <QDebug>

MarinarsSauce::MarinarsSauce()
{
    qDebug() << "MarinarsSauce";
}

           

Veggies类:

#ifndef VEGGIES_H
#define VEGGIES_H


class Veggies
{
public:
    Veggies();
};

#endif // VEGGIES_H
           
#include "veggies.h"

Veggies::Veggies()
{

}

           

Garlic 类:

#ifndef GARLIC_H
#define GARLIC_H

#include "veggies.h"

class Garlic : public Veggies
{
public:
    Garlic();
};

#endif // GARLIC_H

           
#include "garlic.h"

#include <QDebug>

Garlic::Garlic()
{
    qDebug() << "Garlic";
}

           

Mushroom类:

#ifndef MUSHROOM_H
#define MUSHROOM_H

#include "veggies.h"

class Mushroom : public Veggies
{
public:
    Mushroom();
};

#endif // MUSHROOM_H

           
#include "mushroom.h"

#include <QDebug>

Mushroom::Mushroom()
{
    qDebug() << "Mushroom";
}

           

RedPepper 类:

#ifndef REDPEPPER_H
#define REDPEPPER_H

#include "veggies.h"

class RedPepper : public Veggies
{
public:
    RedPepper();
};

#endif // REDPEPPER_H

           
#include "redpepper.h"

#include <QDebug>

RedPepper::RedPepper()
{
    qDebug() << "RedPepper";
}

           

Onion 类:

#ifndef ONION_H
#define ONION_H

#include "veggies.h"

class Onion : public Veggies
{
public:
    Onion();
};

#endif // ONION_H

           
#include "onion.h"

#include <QDebug>

Onion::Onion()
{
    qDebug() << "Onion";
}

           

Pepperoni类:

#ifndef PEPPERONI_H
#define PEPPERONI_H


class Pepperoni
{
public:
    Pepperoni();
};

#endif // PEPPERONI_H
           

SlicedPepperoni 类:

#ifndef SLICEDPEPPERONI_H
#define SLICEDPEPPERONI_H

#include "pepperoni.h"

class SlicedPepperoni : public Pepperoni
{
public:
    SlicedPepperoni();
};

#endif // SLICEDPEPPERONI_H
           
#include "slicedpepperoni.h"
#include <QDebug>

SlicedPepperoni::SlicedPepperoni()
{
    qDebug() << "SlicedPepperoni";
}

           

ChessePizza类:

#ifndef CHESSEPIZZA_H
#define CHESSEPIZZA_H

#include "pizza.h"
#include "pizzaingredientfactory.h"

class ChessePizza : public Pizza
{
public:
    ChessePizza(PizzaIngredientFactory* ingredientFactory);
    void prepare() override;

    PizzaIngredientFactory* ingredientFactory;

};

#endif // CHESSEPIZZA_H

           
#include "chessepizza.h"

#include <QDebug>

ChessePizza::ChessePizza(PizzaIngredientFactory* ingredientFactory)
{
    this->ingredientFactory = ingredientFactory;
}

void ChessePizza::prepare()
{
    qDebug() << "Preparing " + name;
    dough = ingredientFactory->createDough();
    sauce = ingredientFactory->createSauce();
    cheese = ingredientFactory->createCheese();
    VeggiesVector = ingredientFactory->createVeggies();
    pepperoni = ingredientFactory->createPepperoni();
    clam = ingredientFactory->createClam();
}

           

测试:

#include "mainwindow.h"

#include "nypizzastore.h"
#include "chicagopizzastore.h"

#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    PizzaStore *nyStore = new NYPizzaStore();

    nyStore->orderPizza("cheese");

}

MainWindow::~MainWindow()
{
}
           

继续阅读