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

📄 chain_lab.cpp

📁 设计模式
💻 CPP
字号:
// Purpose.  Chain of Responsibility design pattern lab// // Problem.  Four Slot objects are being used, and the client has to know// about and interact with all of them.  It would be preferable to decouple// the client from its many servers by encapsulating all the Slot objects// in a single pipeline, or chain, abstraction.  The advantages are:// "request handling" algorithms are hidden in the pipeline abstraction, and// client complexity is reduced because it can now "launch and leave" all// requests with the head of the pipeline and the pipeline "divides and// conquers" the request across its collection of processing elements.// // Assignment.// o Add a Slot* data member to Slot that will point to each Slot's successor//   in the chain// o Modify the constructors for Slot, Quarter, Dime, Nickel, and Penny to//   accept a Slot* and initialize the Slot* data member (use an "=0" default//   argument)// o Change accept() to delegate to the successor if the conditional expression//   does not succeed (accept() can now have a void return type)// o Add a report() member function to the Slots hierarchy that encapsulates//   the functionality of the final four cout's in main().// o Encapsulate the slots array set-up in a free function.  Create the "chain"//   in this function and return it to main().// o Change main() to eliminate all knowledge of, or interaction with, the//   number and type of Slot objects.#include <iostream.h>#include <stdlib.h>#include <time.h>class Slot {public:   Slot( int val ,Slot *n = NULL) {
	  next = n;      value_ = val;      count_ = 0; }   int accept( int coin ) {      if (coin == value_) {         count_++;         return 1; }      else next->accept( coin ); }
   void setNext( Slot *n ) {
	   next = n; }   int getCount() {      return count_; }private:
   Slot *next;   int  value_;   int  count_;};class Quarter : public Slot {public:   Quarter(Slot *n = NULL) : Slot( 25 , n ) { }};class Dime : public Slot {public:   Dime(Slot *n = NULL) : Slot( 10 , n ) { }};class Nickel : public Slot {public:   Nickel(Slot *n = NULL) : Slot( 5 , n ) { }};class Penny : public Slot {public:   Penny( Slot *n = NULL) : Slot( 1 , n ) { }};int pickCoin(){   static int choices[4] = { 1, 5, 10, 25 };   return choices[rand() % 4];}

void  report(int total,Slot **slots) {
   cout << "\ntotal deposited is $" << total/100 << "."
      << (total%100 < 10 ? "0" : "") << total%100 << endl;
   cout << "quarters - " << slots[0]->getCount() << endl;
   cout << "dimes    - " << slots[1]->getCount() << endl;
   cout << "nickels  - " << slots[2]->getCount() << endl;
   cout << "pennies  - " << slots[3]->getCount() << endl;
}
void chain( Slot ** slots) {
   slots[0] = new Quarter;
   slots[1] = new Dime( slots[0] );
   slots[2] = new Nickel( slots[1] );
   slots[3] = new Penny( slots[2] );
   slots[0]->setNext( slots[3] );
}
void main( void )
{
   Slot*    slots[4];
   chain( slots );
   int      i, coin, total;   time_t   t;   srand((unsigned) time(&t));   for (i=0, total=0; i < 10; i++)   {      coin = pickCoin();      cout << coin << " ";      total += coin;  //    for (int j=0; j < 4; j++)  //       if (slots[j]->accept( coin )) break;
	  slots[0]->accept( coin );   }   report(total,slots);}// 10 5 25 5 1 25 5 25 1 25 // total deposited is $1.27// quarters - 4// dimes    - 1// nickels  - 3// pennies  - 2// // 25 25 10 5 1 5 10 5 1 5 // total deposited is $0.92// quarters - 2// dimes    - 2// nickels  - 4// pennies  - 2// // 5 10 25 10 25 1 25 10 1 25 // total deposited is $1.37// quarters - 4// dimes    - 3// nickels  - 1// pennies  - 2

⌨️ 快捷键说明

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