( Index )
Month

C/C++ Users Group

October 2001

Object-Oriented Analysis and Design

by Mike Redlich

"Object-oriented design is easy - once you learn how to identify the right objects." (1)

This month's presentation focuses on object-oriented analysis and design based on a two-part article published in the May and June 1998 issues of the C/C++ Users Journal.

"This two-article series presents a problem I use both to teach and test OO design. It is a simple but rich problem, strong on 'design,' minimizing language, tool, and even inheritance concerns. The problem represents a realistic work situation where circumstances change regularly." (2)

The application that demonstrates the problem is a coffee-machine, where a total of four designs were developed and implemented. Each design and interaction diagram will be quickly presented and evaluated, and the source code for the final design will be reviewed.

Coffee Machine Design No. 4 Coffee Machine Application

"Brewing a good cup of Java takes careful design, even in C++." (3)

Sample code:

/*----------------------------------------------------------------------------*
 * class CoffeeMachine:
 * Abstraction of the outer machine, holding all the parts.
 * Responsible for constructing machine, capturing external input.
 *----------------------------------------------------------------------------*/
class CoffeeMachine
	{
	private:
		Cashbox *pCashbox;
		Selector *pSelector;

	public:
		CoffeeMachine(void)
			{
			DispenserRegister *pDispenserRegister = new DispenserRegister;
			ProductRegister *pProductRegister = new ProductRegister;
			pCashbox = new Cashbox;
			pSelector = new Selector(pCashbox,pProductRegister,pDispenserRegister);

			// load-up the ingredients - normally would obtain externally:
			Ingredient *cup = new Ingredient("cup");
			Ingredient *coffee = new Ingredient("coffee");
			Ingredient *creamer = new Ingredient("creamer");
			Ingredient *sugar = new Ingredient("sugar");
			Ingredient *water = new Ingredient("water");
			Ingredient *bouillionPowder = new Ingredient("bouillion powder");

			pDispenserRegister->addDispenser(new Dispenser(cup,30));
			pDispenserRegister->addDispenser(new Dispenser(coffee,10));
			pDispenserRegister->addDispenser(new Dispenser(creamer,10));
			pDispenserRegister->addDispenser(new Dispenser(sugar,10));
			pDispenserRegister->addDispenser(new Dispenser(bouillionPowder,10));
			pDispenserRegister->addDispenser(new Dispenser(water,30));

			// load-up the Products - normally would obtain externally:
			Product *black = new Product("black",35,new Recipe(cup,coffee,water));
			Product *white = new Product("white",35,new Recipe(cup,coffee,creamer,water));
			Product *sweet = new Product("sweet",35,new Recipe(cup,coffee,sugar,water));
			Product *whiteSweet = new Product("whiteSweet",35,new Recipe(cup,coffee,sugar,creamer,water));
			Product *bouillion = new Product("bouillion",25,new Recipe(cup,bouillionPowder,water));

			pProductRegister->addProduct(black);
			pProductRegister->addProduct(white);
			pProductRegister->addProduct(sweet);
			pProductRegister->addProduct(whiteSweet);
			pProductRegister->addProduct(bouillion);
			}
		~CoffeeMachine(void)
			{
			delete pSelector;
			delete pCashbox;
			}
		bool oneAction(void)
			{
			char cmdline[BUFSIZ];
			char action[BUFSIZ];
			int value;

			puts("\n______________________________________");
			puts("\tPRODUCT LIST: all 35 cents,except bouillion (25 cents)");
			puts("\t1=black, 2=white, 3=sweet, 4=white & sweet, 5=bouillion");
			puts("\tSample commands: insert 25, select 1.	Your command:");
			gets(cmdline);
			sscanf(cmdline,"%s %d",action,&value);
			if(strcmp(action,"insert") == 0)
				pCashbox->deposit(value);
			else if(strcmp(action,"select") == 0 && value >= 1 && value <= 5)
				pSelector->select( value-1 );
			else if(strcmp(action,"quit") == 0)
				return false;
			else
				printf(" Did not understand request %s %d.\n",action,value);
			return true;
			}
	};

References:

  1. Alistair Cockburn, "Object-Oriented Analysis and Design, Part 1", C/C++ Users Journal, May 1998
  2. Alistair Cockburn, "Object-Oriented Analysis and Design, Part 1", C/C++ Users Journal, May 1998
  3. Alistair Cockburn, "Object-Oriented Analysis and Design, Part 2", C/C++ Users Journal, June 1998

Random Access starts promply at 7:30pm. Hope to see y'all then.


"Random Access" questions start at 7:30 Tuesday night.

SOURCE CODE

Source Code Files

For help, email me at b a r n o l d @ i e e e . o r g
Back to C++ Main Page