Factory Methon 模式
来源:互联网 发布:c语言入门教材 编辑:程序博客网 时间:2024/06/05 01:11
Factory Methon 本质在于,基类提供一个虚函数“挂钩”,用于产生适当的“产品”。
每个派生类可以重写继承的虚函数,为自己产生适当的产品。实际上我们具备了一个未知对象来产生另一个未知对象的能力。《c++必知必会》
http://home.earthlink.net/~huston2/dp/FactoryMethodDemosCpp
下面是创建对象,同上面的创建对象还是有差别的。
每个派生类可以重写继承的虚函数,为自己产生适当的产品。实际上我们具备了一个未知对象来产生另一个未知对象的能力。《c++必知必会》
http://home.earthlink.net/~huston2/dp/FactoryMethodDemosCpp
#include <iostream>
using namespace std;
/* Abstract base class declared by framework */
class Document {
public:
Document( char* fn ) { strcpy( name, fn ); }
virtual void Open() = 0;
virtual void Close() = 0;
char* GetName() { return name; }
private:
char name[20];
};
/* Concrete derived class defined by client */
class MyDocument : public Document {
public:
MyDocument( char* fn ) : Document(fn) { }
void Open() { cout << " MyDocument: Open()" << endl; }
void Close() { cout << " MyDocument: Close()" << endl; }
};
/* Framework declaration */
class Application {
public:
Application() : _index(0) { cout << "Application: ctor" << endl; }
/* The client will call this "entry point" of the framework */
void NewDocument( char* name ) {
cout << "Application: NewDocument()" << endl;
/* Framework calls the "hole" reserved for client customization */
_docs[_index] = CreateDocument( name );
_docs[_index++]->Open(); }
void OpenDocument() { }
void ReportDocs();
/* Framework declares a "hole" for the client to customize */
virtual Document* CreateDocument( char* ) = 0;
private:
int _index;
/* Framework uses Document's base class */
Document* _docs[10];
};
void Application::ReportDocs() {
cout << "Application: ReportDocs()" << endl;
for (int i=0; i < _index; i++)
cout << " " << _docs[i]->GetName() << endl; }
/* Customization of framework defined by client */
class MyApplication : public Application {
public:
MyApplication() { cout << "MyApplication: ctor" << endl; }
/* Client defines Framework's "hole" */
Document* CreateDocument( char* fn ) {
cout << " MyApplication: CreateDocument()" << endl;
return new MyDocument( fn ); }
};
int main() {
/* Client's customization of the Framework */
MyApplication myApp;
myApp.NewDocument( "foo" );
myApp.NewDocument( "bar" );
myApp.ReportDocs();
return 0;
}
using namespace std;
/* Abstract base class declared by framework */
class Document {
public:
Document( char* fn ) { strcpy( name, fn ); }
virtual void Open() = 0;
virtual void Close() = 0;
char* GetName() { return name; }
private:
char name[20];
};
/* Concrete derived class defined by client */
class MyDocument : public Document {
public:
MyDocument( char* fn ) : Document(fn) { }
void Open() { cout << " MyDocument: Open()" << endl; }
void Close() { cout << " MyDocument: Close()" << endl; }
};
/* Framework declaration */
class Application {
public:
Application() : _index(0) { cout << "Application: ctor" << endl; }
/* The client will call this "entry point" of the framework */
void NewDocument( char* name ) {
cout << "Application: NewDocument()" << endl;
/* Framework calls the "hole" reserved for client customization */
_docs[_index] = CreateDocument( name );
_docs[_index++]->Open(); }
void OpenDocument() { }
void ReportDocs();
/* Framework declares a "hole" for the client to customize */
virtual Document* CreateDocument( char* ) = 0;
private:
int _index;
/* Framework uses Document's base class */
Document* _docs[10];
};
void Application::ReportDocs() {
cout << "Application: ReportDocs()" << endl;
for (int i=0; i < _index; i++)
cout << " " << _docs[i]->GetName() << endl; }
/* Customization of framework defined by client */
class MyApplication : public Application {
public:
MyApplication() { cout << "MyApplication: ctor" << endl; }
/* Client defines Framework's "hole" */
Document* CreateDocument( char* fn ) {
cout << " MyApplication: CreateDocument()" << endl;
return new MyDocument( fn ); }
};
int main() {
/* Client's customization of the Framework */
MyApplication myApp;
myApp.NewDocument( "foo" );
myApp.NewDocument( "bar" );
myApp.ReportDocs();
return 0;
}
下面是创建对象,同上面的创建对象还是有差别的。
// A factory method is a static method of a class that
// returns an object of that class' type. But unlike a
// constructor, the actual object it returns might be
// an instance of a subclass. Another advantage of a
// factory method is that it can return existing
// instances multiple times. [Mark Davis]
#include<iostream>
#include<vector>
using std::vector;
using std::cout;
using std::endl;
using std::cin;
class Stooge {
public:
// Factory Method
static Stooge* make_stooge( int choice );
virtual void slap_stick() = 0;
};
class Larry : public Stooge {
public:
void slap_stick() {
cout << "Larry: poke eyes "; }
};
class Moe : public Stooge {
public:
void slap_stick() {
cout << "Moe: slap head "; }
};
class Curly : public Stooge {
public:
void slap_stick() {
cout << "Curly: suffer abuse "; }
};
Stooge* Stooge::make_stooge( int choice ) {
if (choice == 1)
return new Larry;
else if (choice == 2)
return new Moe;
else
return new Curly;
}
int main( void ) {
vector<Stooge*> roles;
int choice;
while (true) {
cout << "Larry(1) Moe(2) Curly(3) Go(0): ";
cin >> choice;
if (choice == 0)
break;
roles.push_back( Stooge::make_stooge( choice ) );
}
for (int i=0; i < roles.size(); i++)
roles[i]->slap_stick();
for (int i=0; i < roles.size(); i++)
delete roles[i];
return 0;
}
// returns an object of that class' type. But unlike a
// constructor, the actual object it returns might be
// an instance of a subclass. Another advantage of a
// factory method is that it can return existing
// instances multiple times. [Mark Davis]
#include<iostream>
#include<vector>
using std::vector;
using std::cout;
using std::endl;
using std::cin;
class Stooge {
public:
// Factory Method
static Stooge* make_stooge( int choice );
virtual void slap_stick() = 0;
};
class Larry : public Stooge {
public:
void slap_stick() {
cout << "Larry: poke eyes "; }
};
class Moe : public Stooge {
public:
void slap_stick() {
cout << "Moe: slap head "; }
};
class Curly : public Stooge {
public:
void slap_stick() {
cout << "Curly: suffer abuse "; }
};
Stooge* Stooge::make_stooge( int choice ) {
if (choice == 1)
return new Larry;
else if (choice == 2)
return new Moe;
else
return new Curly;
}
int main( void ) {
vector<Stooge*> roles;
int choice;
while (true) {
cout << "Larry(1) Moe(2) Curly(3) Go(0): ";
cin >> choice;
if (choice == 0)
break;
roles.push_back( Stooge::make_stooge( choice ) );
}
for (int i=0; i < roles.size(); i++)
roles[i]->slap_stick();
for (int i=0; i < roles.size(); i++)
delete roles[i];
return 0;
}
- Factory Methon 模式
- 设计模式 笔记 工厂方法 Factory Methon
- Factory模式
- Factory模式
- Factory模式
- Factory模式
- Factory 模式
- FACTORY 模式
- Factory模式
- Factory模式
- Factory模式
- Factory模式
- Factory模式
- Factory 模式
- Factory模式
- Factory模式
- Factory 模式
- Factory模式
- 安装sqlserver"以前进行的程序安装创建了挂起的文件操作"的解决办法
- 了解ORM的一个好地方
- 对当前目录下所有文件进行压缩代码
- 一个合格的程序员该做的事情
- JBuilder2005实战JSP之切换控制
- Factory Methon 模式
- 简单的c#文本文件读写
- CSS样式表常用小技巧
- 动态提示的下拉框
- JAVA环境变量设置之心得
- 一女大学生用2元进著名外企(很有启发哦)
- 对联效果
- 深入浅出理解索引结构(一)
- QQ菜单效果