Factory Methon 模式

来源:互联网 发布:c语言入门教材 编辑:程序博客网 时间:2024/06/05 01:11
Factory Methon  本质在于,基类提供一个虚函数“挂钩”,用于产生适当的“产品”。
每个派生类可以重写继承的虚函数,为自己产生适当的产品。实际上我们具备了一个未知对象来产生另一个未知对象的能力。《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;
}

下面是创建对象,同上面的创建对象还是有差别的。
// 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;
}


原创粉丝点击