首页 > 其他分享 >三大工厂模式 简单工厂模式,工厂方法模式,抽象工厂模式 手撕代码

三大工厂模式 简单工厂模式,工厂方法模式,抽象工厂模式 手撕代码

时间:2023-09-07 21:01:17浏览次数:40  
标签:int create factory 模式 工厂 produce height public 三大

1.简单工厂模式

#include <bits/stdc++.h>
using namespace std;

class produce {
private:
    int width;
    int height;
public:
    produce(int width,int height):width(width) , height(height){}
    virtual ~produce(){}
};

class A : public produce {
public:
    A(int width, int height) : produce(width, height) {
        cout << "工厂生产了一件商品 A" << endl;
    }
};

class B : public produce {
public:
    B(int width, int height) : produce(width, height) {
        cout << "工厂生产了一件商品 B" << endl;
    }
};

class C : public produce {
public:
    C(int width, int height) : produce(width, height) {
        cout << "工厂生产了一件商品 C" << endl;
    }
};

class factory {
public:
    produce* create_product(string type) {
        produce* new_product = nullptr;
        if (type == "A")
            new_product = new A(50, 50);
        else if (type == "B")
            new_product = new B(100, 100);
        else if (type == "C")
            new_product = new C(200, 200);
        return new_product;
    }
};

int main()
{
    factory fact;
    produce* p1 = fact.create_product("A");
    produce* p2 = fact.create_product("B");
    produce* p3 = fact.create_product("C");

    delete p1;
    delete p2;
    delete p3;

    system("pause");

    return 0;
}

2.工厂方法模式

#include <bits/stdc++.h>
using namespace std;

class produce {
private:
    int width;
    int height;
public:
    produce(){}
    virtual ~produce(){}
};

class A : public produce {
public:
    A() : produce() {
        cout << "工厂生产了一件商品 A" << endl;
    }
};

class B : public produce {
public:
    B() : produce() {
        cout << "工厂生产了一件商品 B" << endl;
    }
};

class C : public produce {
public:
    C() : produce() {
        cout << "工厂生产了一件商品 C" << endl;
    }
};

class factory_father {
public:
    virtual produce* create_produce() = 0;  
    virtual ~factory_father() {};
};

class A_factory:public factory_father {
public:
    virtual produce* create_produce() {
        return new A();
    }
};

class B_factory:public factory_father {
public:
    virtual produce* create_produce() {
        return new B();
    }
};

class C_factory:public factory_father {
public:
    virtual produce* create_produce() {
        return new C();
    }
};

int main()
{
    factory_father* p1 = new A_factory();
    p1->create_produce();

    factory_father* p2 = new B_factory();
    p2->create_produce();

    factory_father* p3 = new C_factory();
    p3->create_produce();

    system("pause");

    return 0;
}

3.抽象工厂模式

#include <bits/stdc++.h>
using namespace std;

class produce {
private:
    int width;
    int height;
public:
    produce(){}
    virtual ~produce(){}
};

class factory_father {
public:
    virtual produce* create_produce_A() = 0;
    virtual produce* create_produce_B() = 0;
    virtual produce* create_produce_C() = 0;
    virtual ~factory_father() {};
};


class this_A : public produce {
public:
    this_A() :produce() {
        cout << "this平台生产了A" << endl;
    }
};
class this_B : public produce {
public:
    this_B() :produce() {
        cout << "this平台生产了B" << endl;
    }
};
class this_C : public produce {
public:
    this_C() :produce() {
        cout << "this平台生产了C" << endl;
    }
};

class that_A : public produce {
public:
    that_A() :produce() {
        cout << "that平台生产了A" << endl;
    }
};
class that_B : public produce {
public:
    that_B() :produce() {
        cout << "that平台生产了B" << endl;
    }
};
class that_C : public produce {
public:
    that_C() :produce() {
        cout << "that平台生产了C" << endl;
    }
};

class this_factory : public factory_father {
public:
    virtual produce* create_produce_A() {
        return new this_A();
    }
    virtual produce* create_produce_B() {
        return new this_B();
    }
    virtual produce* create_produce_C() {
        return new this_C();
    }
};

class that_factory : public factory_father {
public:
    virtual produce* create_produce_A() {
        return new that_A();
    }
    virtual produce* create_produce_B() {
        return new that_B();
    }
    virtual produce* create_produce_C() {
        return new that_C();
    }

};

int main()
{
    factory_father* p1 = new this_factory();
    p1->create_produce_A();
    p1->create_produce_B();
    p1->create_produce_C();

    factory_father* p2 = new that_factory();
    p2->create_produce_A();
    p2->create_produce_B();
    p2->create_produce_C();

    system("pause");
    return 0;
}

 

标签:int,create,factory,模式,工厂,produce,height,public,三大
From: https://www.cnblogs.com/yzqiang/p/17686045.html

相关文章

  • 金蝶云·星空部署模式建议
    经历了从金蝶云·星空从单点部署到集群,再到替换SAP的过程,SAP实施商是IBM,管理咨询+系统实施,如今项目已经成功上线,所以对金蝶的相关知识也做了整理和归档。系统实施过程中,部署架构是非常关键的一个环节,不同的企业规模和业务模式,将决定了采用不同的部署模式,对此结合金蝶原厂部署建议......
  • vue-router 路由模式有几种?
    VueRouter提供了三种路由模式:######1:Hash模式(默认):在URL中使用带有#符号的哈希值来管理路由。例如:http://xxxx.com/#/path。在Hash模式下,当URL的哈希值发生变化时,浏览器不会向服务器发送请求,而是通过监听hashchange事件来进行路由导航。######2:History模式:使用HTM......
  • springboot策略模式
    一.定义接口publicinterfacePearlTaskService{IntegergetTaskType();Map<String,Integer>execute(LonguserId,GameTaskgameTask,StringgameCode);}二.定义抽象类@Slf4jpublicabstractclassPearlTaskStrategyimplementsPearlTaskService{protec......
  • 不用额外插件?RunnerGo内置压测模式怎么选
    我们在做性能测试时需要根据性能需求配置不同的压测模式如:阶梯模式。使用jmeter时我们需要安装插件来配置测试模式,为了方便用户使用,RunnerGo内嵌了压测模式这一选项,今天给大家介绍一下RunnerGo的几种压测模式和怎么根据性能需求选择合适的压测模式。RunnerGo提供了以下五种压测模式......
  • 装饰模式(decorator)
    装饰器模式(Decorator)1、作用引入一个设计原则:类应该对扩展开发,对修改关闭。简单的说,就是允许我们的我们的类进行扩展,在不修改现有代码的情况下,适应新的行为改变。当实现好的类有了新的需求,不考虑重用的话,会直接修改(现在写代码都这样,没有满足开闭原则的修改关闭原则),如果考虑代......
  • 享元模式(flyweight)
    享元模式(flyweight)1、作用一些对象在使用一次后就可以销毁了,比如画一个圈,这个对象调用draw()函数后,这个对象就没有作用,除非再次画相同的圈。但是在应用中需要画很多圈,如果每次画一次圈都构造一个对象,这样内存消耗很多,构造销毁也很费时,这个时候就可以考虑一下享元模式,这样可以节......
  • 代理模式(Proxy)
    代理模式(Proxy)1、作用代理模式是包装一个对象,控制对它的访问,实现逻辑合实现的解耦。2、实现方式代理模式跟装饰器模式、适配器模式、外观模式都有类似的地方,都通过关联关系封装了其他类型的对象,但是使用的目的不一样。代理:包装一个对象,控制对它的访问。装饰者:包装另一个对象......
  • 策略模式(strategy)
    策略模式(Strategy)1、作用策略模式的主要目的主要是将算法的定义(strategy类)和使用分开(context类),也就是将算法的行为和环境分开,将算法的定义放在专门的策略类中,每一个策略类封装一个实现算法。而使用算法的环境中针对抽象策略编程,而不是针对实现编程,符合依赖倒置原则。2、实现方......
  • 模板模式(template)
    模板模式(Template)1、作用做一件是的方法很多,但做这件都可以归纳为几个步骤。这个时候可以使用模板模式,在模板类中,定义做事的步骤,将多种实现做事的细节延迟到子类中去实现。即:定义一个操作中的算法的骨架(模板函数),而将一些步骤延迟到子类中(基本函数)。模板方法使得子类可以不改变......
  • 外观模式(facade)
    外观模式(Facade)1、作用(1)、将各个子系统的接口汇聚在一起,定义一个统一的接口方便客户使用,满足“迪米特法则”,即客户不需要了解子系统,只需要知道Facade模式封装的类即可以使用所有子系统。(2)、隔离用户和子系统,将用户与子系统解耦,当子系统有修改是,顶多修改Facade模式封装的类,无需......