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

📄 factorymethod_lab.cpp

📁 设计模式
💻 CPP
字号:
// Purpose.  Factory Method design pattern lab//// Problem.  The client code in main() is doing a good job of depending only// on an abstract base class (FederalFraudvillian), and not getting coupled// to any of the concrete derived classes.  But, coupling still exists where// each individual concrete instance is created.  A "factory" abstraction// implemented with factory methods (or "virtual constructors") would allow// this last bit of coupling to be eliminated.  We would like to remove all// dependencies on the President, AttorneyGeneral, and
// MinorityPartyCongressPerson classes so that their declarations (and inline
// definitions) can be moved below the client code in main().  [Note: the
// ordering of declarations and definitions demonstrates nicely the
// dependencies that exist within the code.]  We would also like to support
// "equal opportunity political satire" by abstracting a "political party"
// factory base class, and then instantiating one of two different derived
// classes: DemocratFactory or RepublicanFactory.//// Assignment.// o Create a Factory base class with createPresident(), etc, factory methods//   (pure virtual functions)// o Create two classes derived from Factory: DemocratFactory, and//   RepublicanFactory// 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 calls to createPresident(), etc// o The "new"s that were being performed in main() should now be done in the//   Factory derived classes's factory methods// o Add a 1-arg constructor to the classes President, AttorneyGeneral, and//   MinorityPartyCongressPerson.  Use a "member initialization list" to map//   the char* argument the constructor receives to the Base class's 1-arg//   constructor.// o The Factory derived classes will provide the value for the argument you//   just added// o Move as many declarations and definitions as possible below main() to//   demonstrate the minimal coupling that remains#include <iostream.h>#include <string.h>class FederalFraudvillian {public:   FederalFraudvillian( char* pp = "Democrat" ) { strcpy( party, pp ); }   virtual void partyLine() = 0;protected:   char party[20];};class President : public FederalFraudvillian {public:
	President(char *p) : FederalFraudvillian(p) {}   void partyLine() { cout << party << ": speak no evil" << endl; }};class AttorneyGeneral : public FederalFraudvillian {public:
	AttorneyGeneral(char *p) : FederalFraudvillian(p) {}   void partyLine() { cout << party << ": see no evil" << endl; }};class MinorityPartyCongressPerson : public FederalFraudvillian {public:
	MinorityPartyCongressPerson(char *p) : FederalFraudvillian(p) {}   void partyLine() { cout << party << ": hear no evil" << endl; }};
class Factory {
public:
    virtual FederalFraudvillian* createPresident(int){ return 0;}
};

class DemocratFactory : public Factory {
public:
	FederalFraudvillian * createPresident(int in);
};
FederalFraudvillian *DemocratFactory::createPresident(int in)
{
	  if (in == 1)      return new President("Democrat");
      else if (in == 2) return new AttorneyGeneral("Democrat");
      else              return new MinorityPartyCongressPerson("Democrat");
}

class RepublicanFactory : public Factory {
public:
	FederalFraudvillian * createPresident(int);
};
FederalFraudvillian *RepublicanFactory::createPresident(int in)
{
	 if (in == 1)      return new President("Republican");
      else if (in == 2) return new AttorneyGeneral("Republican");
      else              return new MinorityPartyCongressPerson("Republican");
}
void main( void ) {   FederalFraudvillian*  moralMisfits[10];   int      in, i, total = 0; //  #ifdef DEMOCRAT
 //     Factory* factory = new DemocratFactory;
//   #else
	  Factory* factory = new RepublicanFactory();
//   #endif   cout << "President(1) AG(2) MPCP(3) Go(0): ";   cin >> in;   while (in) {      moralMisfits[total] = factory->createPresident(in);
	  total++;      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 + -