⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 prototype_lab.cpp

📁 设计模式
💻 CPP
字号:
// Purpose.  Prototype design pattern lab//// Problem.  See the problem statement for the Factory Method lab.  Beyond// the issues discussed there, we would also like to eliminate the "case"// statement that currently exists in main().  We can do that by replacing// the conditional code with a "table-driven" design.//// Assignment.// o Add a second pure virtual function to class FederalFraudvillian -//      virtual FederalFraudvillian* clone() = 0;// o Create a Factory base class with a single "virtual constructor" -//      FederalFraudvillian* createPrevaricator( PrevaricatorType type )// o Add the following enum declaration to the Factory class -//      enum PrevaricatorType { None, PresT, AGT, MPCPT };// o Add a protected array of FederalFraudvillian "prototypical instances" //   to the Factory class -//      FederalFraudvillian* prototypes[4];// o The body of the createPrevaricator() method will simply use its type//   argument as an offset into the prototypes array, call clone() on that//   prototypical instance, and return whatever clone() returns// o Create two classes derived from Factory: DemocratFactory, and//   RepublicanFactory// o These two derived classes will only contain a default constructor.
//   Each constructor will populate the prototype array in the base class
//   with instances of the classes: 0, President, AttorneyGeneral, and
//   MinorityPartyCongressPerson.  Make sure these "prototypes" are of the
//   correct party affiliation.
// o Instantiate a factory object at the top of main() with code like -
//      #ifdef DEMOCRAT//         Factory* factory = new DemocratFactory;//      #else//         Factory* factory = new RepublicanFactory;//      #endif// o Replace the calls to "new" in main() with a single call to -//      createPrevaricator( in )// o The "new"s that were being performed in main() will now be done//   [indirectly] in the clone() method of each FederalFraudvillian derived//   class// o Add a 1-arg constructor to the FederalFraudvillian derived classes.//   Use a "member initialization list" to map the char* argument the//   constructor receives to the Base class's 1-arg constructor.// o Add a clone() method to the FederalFraudvillian derived classes.  The//   body of clone() should create an instance of the class it is a member of//   (being careful to supply the char* argument the constructor is expecting)//   and return the new instance.// o Move as many declarations and definitions as possible below main() to//   demonstrate the minimal coupling that remains#include <iostream.h>#include <string.h>
enum PrevaricatorType { None, PresT, AGT, MPCPT };
class FederalFraudvillian {public:	FederalFraudvillian( char* pp = "Democrat" ) { strcpy( party, pp ); }	virtual void partyLine() = 0;
	virtual FederalFraudvillian* clone() = 0;protected:	char party[20];};
class President : public FederalFraudvillian {public:
	President( char * p ) : FederalFraudvillian(p) {}	void partyLine() { cout << party << ": speak no evil" << endl; }
	FederalFraudvillian* clone()           {    return this;   }};class AttorneyGeneral : public FederalFraudvillian { public:
	 AttorneyGeneral( char *p) : FederalFraudvillian( p ) {}	 void partyLine() { cout << party << ": see no evil" << endl; }
	 FederalFraudvillian* clone()           {   return this;  } };  class MinorityPartyCongressPerson : public FederalFraudvillian { public:
	 MinorityPartyCongressPerson( char *p) : FederalFraudvillian( p ) {}	 void partyLine() { cout << party << ": hear no evil" << endl; }
	 FederalFraudvillian* clone()           {   return this; } }; 
 class Factory {
 public:
	 
	 FederalFraudvillian* createPrevaricator( PrevaricatorType type ) {
		 switch( type )
		 {
		 case None : { return NULL; break; }
		 case PresT: { return prototypes[1]->clone(); break; }
		 case AGT:   { return prototypes[2]->clone(); break; }
		 case MPCPT: { return prototypes[3]->clone(); break; }
		 default: { cout << " incorrect input " << endl ; return NULL;}
		 }
	 }
	 ~Factory() { 
		 delete prototypes[1];
		 delete prototypes[2];
		 delete prototypes[3];
	 }
 protected:
	 FederalFraudvillian* prototypes[4];
	 char * str;
 };
 // None, PresT, AGT, MPCPT 
 class DemocratFactory : public Factory {
 public:
	 DemocratFactory() { str = "DDEMOCRAT" ;	 
	 prototypes[1] = new President( str );
	 prototypes[2] = new AttorneyGeneral( str );
	 prototypes[3] = new MinorityPartyCongressPerson( str );
	 }
 };
 
 class RepublicanFactory : public Factory { 
 public : 
	 RepublicanFactory() { 
		 str = "Republican"; 
		 prototypes[1] = new President( str );
		 prototypes[2] = new AttorneyGeneral( str );
		 prototypes[3] = new MinorityPartyCongressPerson( str );
	 }
 }; void main( void ) {
	 #ifdef DEMOCRAT
         Factory* factory = new RuplicanFactory();
      #else
         Factory* factory = new DemocratFactory();
      #endif	 FederalFraudvillian*  moralMisfits[10];	 int      in, i, total = 0;	 	 cout << "President(1) AG(2) MPCP(3) Go(0): ";	 cin >> in;	 while (in) {		 //      if (in == 1)      moralMisfits[total++] = new President;		 //      else if (in == 2) moralMisfits[total++] = new AttorneyGeneral;		 //      else              moralMisfits[total++] = new MinorityPartyCongressPerson;
         moralMisfits[total++] = factory->createPrevaricator((PrevaricatorType) in );		 cout << "President(1) AG(2) MPCP(3) Go(0): ";		 cin >> in;	 }	 for (i=0; i < total; i++) moralMisfits[i]->partyLine();//	 for (i=0; i < total; i++) delete moralMisfits[i]; }  // no compiler directive - // President(1) AG(2) MPCP(3) Go(0): 1 // President(1) AG(2) MPCP(3) Go(0): 2 // President(1) AG(2) MPCP(3) Go(0): 3 // President(1) AG(2) MPCP(3) Go(0): 1 // President(1) AG(2) MPCP(3) Go(0): 0 // Republican: speak no evil // Republican: see no evil // Republican: hear no evil // Republican: speak no evil  // -DDEMOCRAT - // President(1) AG(2) MPCP(3) Go(0): 1 // President(1) AG(2) MPCP(3) Go(0): 2 // President(1) AG(2) MPCP(3) Go(0): 3 // President(1) AG(2) MPCP(3) Go(0): 1 // President(1) AG(2) MPCP(3) Go(0): 0 // Democrat: speak no evil // Democrat: see no evil // Democrat: hear no evil // Democrat: speak no evil

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -