设计模式1--factory design patterns in c++

0. why factory design

/*
Factory design pattern is widely known design pattern and many people know exactly how to use it.
But when it comes to the question why factory design pattern then so many people really fail to explain the real reason why to use factory design pattern.


*/
# include <iostream>
# include <string>

class Item{};
class Iphone13 : public Item {};
class Iphone14 : public Item {};
class Pen : public Item {};

class ItemFactory {
public:
    Item* getItem (std::string type) {
        if (type == "Iphone13") return new Iphone13();
        if (type == "Iphone14") return new Iphone14();
        if (type == "pen") return new Pen();
        return nullptr;
    }
};

//client (user of apple mobile class) give app one single interface
class Amazon {
public:
    ItemFactory factory;
    Amazon() {}
    //  Item* getItem (std::string type) {
    //     if (type == "Iphone13") return new Iphone13();
    //     if (type == "Iphone14") return new Iphone14();
    //     if (type == "pen") return new Pen();
    //     return nullptr;
    // }
    Item * getItem(std::string type) {
        return factory.getItem(type);
    }
};

int main() {
    Amazon amazon;
    Item *it = amazon.getItem("Iphone13");
    return 0;
}

1.factory design 

在软件行业中,设计模式是针对软件设计中常见问题的通用可重复解决方案
类型:
1.创意设计模式6
2.行为设计模式12
3.结构设计模式8
什么是FDP
1.它为你创建对象,而不是你直接启动对象。
2. FDP也称为“虚拟构造函数” 虚拟构造函数 c++不支持虚拟构造函数
如何实施FDP
1.定义一个接口或抽象类来创建对象,但让子类决定启动哪个类

Object.cpp 

#include <iostream>
#include <string>

class Toy {
protected:
    std::string name;
    float price;
public: 
    virtual void  prepareParts() = 0;
    virtual void combineParts() = 0;
    virtual void assembleParts() = 0;
    virtual void applyLable() = 0;
    virtual void showProduct() = 0;

};

class Car: public Toy {
    //make all constrctor private or protected !!
    public:
        void prepareParts() {std::cout << "preparing car parts" << std::endl;}
        void combineParts() {std::cout << "combining car parts" << std::endl;}
        void assembleParts() {std::cout << "assmbling car parts"<< std::endl;}
        void applyLable() {
            std::cout << "applying car label" << std::endl;
            name = "Car";
            price = 10;
        }
        void showProduct() {std::cout << "Name: " << name << std::endl << "price:" << price <<std::endl;}
};

class Bike: public Toy {
    //make all constrctor private or protected !!
    public:
        void prepareParts() {std::cout << "preparing Bike parts" << std::endl;}
        void combineParts() {std::cout << "combining Bike parts" << std::endl;}
        void assembleParts() {std::cout << "assmbling Bike parts"<< std::endl;}
        void applyLable() {
            std::cout << "applying Bike label" << std::endl;
            name = "Bike";
            price = 3;
        }
        void showProduct() {std::cout << "Name: " << name << std::endl << "price:" << price <<std::endl;}
};

class Plane: public Toy {
    //make all constrctor private or protected !!
    public:
        void prepareParts() {std::cout << "preparing Plane parts" << std::endl;}
        void combineParts() {std::cout << "combining Plane parts" << std::endl;}
        void assembleParts() {std::cout << "assmbling Plane parts"<< std::endl;}
        void applyLable() {
            std::cout << "applying Plane label" << std::endl;
            name = "Plane";
            price = 30;
        }
        void showProduct() {std::cout << "Name: " << name << std::endl << "price:" << price <<std::endl;}
};

Toyfactory.cpp 

#ifndef __io__
#define __io__

#include <iostream>
#endif

#include "Object.cpp"

class ToyFactory {
public:
    static Toy * createToy(int type) {
        Toy* toy = nullptr;
        switch (type) {
            case 1:{
                toy = new Car;
                break;
            }

            case 2: {
                toy = new Bike;
                break;
            }
            case 3: {
                toy = new Plane;
                break;
            }
            default: {
                std::cout << "invalid toy type please re-enter type" << std::endl;
                return nullptr;
            }  
        }
        toy-> prepareParts();
        toy->combineParts();
        toy -> assembleParts();
        toy -> applyLable();

        return toy;
    }

};

 client.cpp

/*
in software industries, a design pattern is a general repeatable solution to a commonly occrring problem in software design

types:
1. creational degisn pattern 6  
2. behavioral design pattern 12
3. structural design pattern 8

what is FDP
1. it creates object for you , rather you initiating object directly.
2. FDP is also known as "virtual constructor" 虚拟构造函数   c++ doesn't support virtual constructor

how to implement FDP
1.define an interface or an abstract class for creating an object but let the subclasses decide which class to initiate
*/

# include <iostream>


#include "Toyfactory.cpp"

int main() {
    //client code starts
    int type;
    while(1) {
        std::cout << std::endl << "enter type or zero for exit" << std::endl;
        std::cin >> type;
        if (!type) {
            break;
        }
        Toy *v = ToyFactory::createToy(type);
        if (v) {
            v -> showProduct();
            delete v; //删除它是因为在迭代器中迭代执行此操作
        }
    }
    std::cout << "exit:" << std::endl;
    return 0;
}

运行:

(base) daichang@daichang-ASUS:~/Desktop/myWS/factory-design-in-cplusplus/src/1_factoryDesign$ g++ client.cpp 
(base) daichang@daichang-ASUS:~/Desktop/myWS/factory-design-in-cplusplus/src/1_factoryDesign$ ./a.out 

enter type or zero for exit
3
preparing Plane parts
combining Plane parts
assmbling Plane parts
applying Plane label
Name: Plane
price:30

enter type or zero for exit
2
preparing Bike parts
combining Bike parts
assmbling Bike parts
applying Bike label
Name: Bike
price:3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值