设计模式2--Abstract Factory Design Pattern In C++

本文介绍了抽象工厂设计模式(AFDP),展示了如何在C++中使用该模式创建和管理相关对象系列,以及其在系统独立性和对象配置方面的优势。通过继承和protected特性,实现在Car类和CarFactory之间的灵活组装。

1.AFDP(抽象工厂设计模式)

        AFDP 定义了一个抽象类来创建相关的家族对象但未指定其具体子类

2.基本原理图

3.为什么/何时使用AFDP

        1.你需要系统独立于对象的创建、组合和表示方式。

        2.显示接口而不是实现。

        3.系统需要配置多个对象系列之一

4.UML图

5.C++程序

 继承中的访问protected 最关键的特性是它允许派生类访问基类中的受保护成员。这意味着如果一个基类中的成员被声明为 protected,则任何直接或间接的派生类都可以访问这些成员。这一点与 private 成员不同,private 成员即使在派生类中也是不可访问的。

Car.cpp

#ifndef __io__
#define __io__
#include <iostream>
#endif

class Tire {
protected:
    std::string name;
    int presure;
public: 
    Tire(std::string n, int presure):name(n), presure(presure){};
    std::string getName() { return name;}
    int getPresure() {return presure;}
};

class SimpleTire:public Tire {
public:
    SimpleTire(): Tire("SimpleTire", 75) {}
};

class LuxuryTire : public Tire {
public:
    LuxuryTire(): Tire("LuxuryTire", 100) {}
};

class Body {
protected:
    std::string name;
    int strength;
public:
    Body(std::string n, int strength): name(n) , strength(strength) {}
    std::string getName() {return name;}
    int getStrenth() {return strength;}
};

class SimpleBody: public Body {
public:
    SimpleBody(): Body("SimpleBody", 75) {}
};


class LuxuryBody: public Body {
public:
    LuxuryBody(): Body("LuxuryBody", 100) {}
};

class Car {
protected:
    std::string name;
    Tire* tire;
    Body* body;
public:
    Car(std::string n): name(n) {}
    void setTire(Tire* t) {tire = t;}
    void setBody(Body* b) {body = b;}
    void printDetails() {
        std::cout << std::endl << "car:" << name << std::endl;
        std::cout << "tire:" << tire->getName() << "pressure:" << tire->getPresure() << std::endl;
        std::cout << "Body:" << body->getName() << "strength:" << body->getStrenth() << std::endl;
    }
};

CarFactory.cpp

#ifndef __io__
#define __io__
#include <iostream>
#endif

class Tire {
protected:
    std::string name;
    int presure;
public: 
    Tire(std::string n, int presure):name(n), presure(presure){};
    std::string getName() { return name;}
    int getPresure() {return presure;}
};

class SimpleTire:public Tire {
public:
    SimpleTire(): Tire("SimpleTire", 75) {}
};

class LuxuryTire : public Tire {
public:
    LuxuryTire(): Tire("LuxuryTire", 100) {}
};

class Body {
protected:
    std::string name;
    int strength;
public:
    Body(std::string n, int strength): name(n) , strength(strength) {}
    std::string getName() {return name;}
    int getStrenth() {return strength;}
};

class SimpleBody: public Body {
public:
    SimpleBody(): Body("SimpleBody", 75) {}
};


class LuxuryBody: public Body {
public:
    LuxuryBody(): Body("LuxuryBody", 100) {}
};

class Car {
protected:
    std::string name;
    Tire* tire;
    Body* body;
public:
    Car(std::string n): name(n) {}
    void setTire(Tire* t) {tire = t;}
    void setBody(Body* b) {body = b;}
    void printDetails() {
        std::cout << std::endl << "car:" << name << std::endl;
        std::cout << "tire:" << tire->getName() << "pressure:" << tire->getPresure() << std::endl;
        std::cout << "Body:" << body->getName() << "strength:" << body->getStrenth() << std::endl;
    }
};

client.cpp

#ifndef __io__
#define __io__
#include <iostream>
#endif

#include "CarFactory.cpp"

#define SIMPLE_CAR 1
// #define LUXURY_CAR 1

int main() {
    #ifdef SIMPLE_CAR
        CarFactory* factory = new SimpleCarFactory;
    #elif LUXURY_CAR
        CarFactory* factory = new LuxuryCarFactory;

    #endif

    Car *car = factory -> buildWholeCar();
    car -> printDetails();

    return 0;
}

 github见GitHub - daichang01/factory-design-in-cplusplus

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值