C 工廠模式的一點構想

2021-09-29 15:26:35 字數 4142 閱讀 1129

1.工廠需求

2.**實現

schoolfactory.h

#ifndef _schoolfactory_h__

#define _schoolfactory_h__

#include #include #include class cschool;

typedef cschool* (*createclass)();

typedef std::vectorvecschool;

typedef std::mapmapcreateschool;

class cschoolfactory

} };

static cfreeschoolfactory m_s_objfreefactory;

private:

cschoolfactory();

~cschoolfactory();

cschoolfactory(const cschoolfactory&);//禁類賦值

cschoolfactory& operator=(const cschoolfactory&);//禁類拷貝

};/*申明*/

#define declare_school(schooltypename) \

public: \

static cschool* createschool();

/*定義*/

#define implement_school(schooltypename) \

cschool* schooltypename::createschool()

/*模擬靜態成員變數的申明和定義*/

/*註冊*/

#define register_school(schooltypename) \

cschoolfactory::getinstance()->registerschool(#schooltypename, (createclass)schooltypename::createschool);

/*內部註冊*/

#define inner_register_school(schooltypename) \

registerschool(#schooltypename, (createclass)schooltypename::createschool);

#endif/*_schoolfactory_h__*/

schoolfactory.cpp

#include "stdafx.h"

#include "schoolfactory.h"

#include "school.h"

cschoolfactory::cschoolfactory()

cschoolfactory::~cschoolfactory()

cschool* cschoolfactory::createschool(std::string strschooltypename)

return nullptr;

}void cschoolfactory::registerschool(std::string strschooltypename, createclass pfncreateclass)

cschoolfactory* cschoolfactory::getinstance()

void cschoolfactory::destoryschool() }}

/*定義*/

cschoolfactory* cschoolfactory::m_s_pobjfactory = new cschoolfactory();

cschoolfactory::cfreeschoolfactory cschoolfactory::m_s_objfreefactory;

school.h

#ifndef _school_h__

#define _school_h__

#include "schoolfactory.h"

/***********學校公用類***************====*/

class cschool

;/***********==小學類*********************/

class cprimary : public cschool

;/***********==初中類*********************/

class csecondary : public cschool

;/***********==高中類*********************/

class csenior : public cschool

;/***********==大學類*********************/

class cuniversity : public cschool

;#endif /*_school_h__*/

school.cpp

#include "stdafx.h"

#include "school.h"

implement_school(cschool)

cschool::cschool()

cschool::~cschool()

int cschool::getgradenum()

void cschool::setgradenum(int igradenum)

void cschool::test()

/***********==小學類*********************/

implement_school(cprimary)

cprimary::cprimary()

cprimary::~cprimary()

void cprimary::test()

/***********==初中類*********************/

implement_school(csecondary)

csecondary::csecondary()

csecondary::~csecondary()

void csecondary::test()

/***********==高中類*********************/

implement_school(csenior)

csenior::csenior()

csenior::~csenior()

void csenior::test()

/***********==大學類*********************/

implement_school(cuniversity)

cuniversity::cuniversity()

cuniversity::~cuniversity()

void cuniversity::test()

main.cpp

// factorytest.cpp : 定義控制台應用程式的入口點。

#include "stdafx.h"

#include "school.h"

#include "schoolfactory.h"

#define school "cschool"

#define primary "cprimary"

#define secondary "csecondary"

#define senior "csenior"

#define university "cuniversity"

int _tmain(int argc, _tchar* ar**)

3.實現結果

每天進步一點 C 設計模式 工廠模式

工廠模式可以分為簡單工廠與抽象工廠,這兩個都屬於建立型設計模式。那麼下面我們簡單的說說2個之間的不同 1.1 簡單工廠 概念 在實際開發中,需求是變化的,我們所使用的某乙個物件,並不能在編譯時完全確定,需要讓物件延遲建立,這時候,我們就可以使用簡單工廠。因此我們說簡單工廠解決的是 單一物件的建立 問...

每天學一點flash(51) 工廠模式

乙個工廠可以生產很多的產品,各種各樣五花八門的產品。這些產品是通過工廠生產出來的,乙個工廠可以生產這些產品。這樣我們可以聯想到自己所寫的 工廠模式解決了建立例項化的過程。典型的例子就是計算器的問題。面試人員同樣很喜歡把這樣的題目考察應聘者。看是否有一種熟悉的物件導向思維,把 封裝,實現 的重用性,繼...

設計模式的一點思考

建立型 builder 當我們要建立的物件很複雜的時候 通常是由很多其他的物件組合而成 我們要將複雜物件的建立過程和這個物件的表示 展示 分離開來,這樣做的好處就是通過一步一步的進行複雜物件的構建,由於在每一步的構造過程中可以引入引數,使得同樣的構建過程可以建立不同的表示。abstractfacto...