Consider the task of making a cheese pizza. In particular, suppose that the pizza should be made according to the following recipe. Using a premixed ball of crust dough, flatten the dough to the desired thickness. Next, pour two cups of pizza sauce onto the crust. Spread the sauce evenly across the crust. Spread three cups of mozzarella cheese on top. Cook the pizza for 15 minutes at 400 degrees Farenheit. Cooking recipes are algorithms because they explain a procedure for preparing food. The cheese pizza recipe is no exception. This recipe can be recast as a five-stage algorithm: Subtask 1. Using a premixed ball of crust dough, flatten the dough to desired thickness. 2. Pour two cups of pizza sauce onto the crust. 3. Spread the sauce evenly across the crust. 4. Spread three cups of mozzarella cheese on top. 5. Set pizza oven to 400 degrees Farenheit and cook pizza for 15 minutes. The first step in any object-oriented design is to identify the objects. In this case, the objects seem to be: dough (crust) sauce cheese oven Often a software developer can find existing software classes from which to construct the necessary objects. It is important to discover such classes early, because they impact the way the design needs to proceed toward implementation. The classes for the objects are: ` ______________________________________ _____________________________ | Dough | | Sauce | |_____________________________________| |_____________________________| |void FlattenToDesiredThickness() | |void PourIntoMeasuringCup() | |void SpreadTopping() | |_____________________________| |void PlaceInPizzaOven() | |void RemoveFromOvenAfter15Minutes() | |_____________________________________| _______________________________ _____________________________ | PizzaOven | | Cheese | |_______________________________| |_____________________________| |void TurnOn() | |void PourIntoMeasuringCup() | |void TurnOff() | | | |void SetOvenTo400() | | | |_______________________________| |_____________________________| _______________________________ | MeasuringCup | |_______________________________| |void PourOntoDough() | |_______________________________| We can generalize these methods by introducing some attributes as well as constructors and some parameters: _____________________________________ ______________________________ | Dough | | Sauce | |_____________________________________| |______________________________| | - int time | | | |_____________________________________| | + Sauce() | | | | | | + Dough() | | + void PourInto(MeasuringCup)| | | |______________________________| | + void FlattenToDesiredThickness() | | + void SpreadTopping() | | + void PlaceInOven(PizzaOven) | | + void RemoveFromOven(PizzaOven,int)| |_____________________________________| _______________________________ ______________________________ | PizzaOven | | Cheese | |_______________________________| |______________________________| | - int temperature | | | | | | + PizzaOven() | | + void PourInto(MeasuringCup)| | | |______________________________| | + void TurnOn() | | + void TurnOff() | | + void SetOven(int) | |_______________________________| _______________________________ | MeasuringCup | |_______________________________| | | | + MeasuringCup() | | | | + void PourOver(Dough) | |_______________________________| The plus sign (+) prefix on a method is commonly used and it means that this method is for use outside the class (a public method). A negative sign (-) prefix on a method means that it is a private method, only to be used inside the class itself. We also introduce parameters for the some of the methods. Parameters are indicated in a class diagram by the name of a type (class) inside the parenthesis following a method. The use of parameters often stems from the need to involve multiple objects in a single method. For example, the act of pouring pizza sauce into a measuring cup involves two objects--the sauce and the cup. Using a parameter permits one object to be passed to a method performed upon a second object. As a second example, the following instruction is used to cause a Dough object, called crust, to be placed into the PizzaOven named oven: crust.PlaceInOven(oven); Notice that a new object was introduced into the algorithm that was not obvious from the reading of the recipe, MeasuringCup. It will be necessary to use measuring cups to transfer sauce and cheese to the dough. The next stage of the design (this type of design is referred to as top-down design) is to refine each of the subtasks. The algorithm we now get is as follows: //1. Using a premixed ball of crust dough, flatted the dough to the desired //thickness. Dough crust = new Dough(); crust.FlattenToDesiredThickness(); //2. Pour two cups of pizza sauce onto the crust MeasuringCup sauceCup = new MeasuringCup(); Sauce mySauce = new Sauce(); mySauce.PourInto(sauceCup); sauceCup.PourOnto(crust); mySauce.PourInto(sauceCup); sauceCup.PourOnto(crust); //3. Spread the sauce evenly across the crust crust.SpreadTopping(); //4. Spread three cups of mozzarella cheese on top MeasuringCup cheeseCup = new MeasuringCup(); Cheese mozzarella = new Cheese(); for(int i = 0; i < 3; ++i) { mozzarella.PourInto(cheeseCup); cheeseCup.PourOnto(crust); } crust.SpreadTopping(); //5. Set Pizza oven to 400 degrees Farenheit and cook pizza for 15 minutes PizzaOven oven = new PizzaOven(); oven.TurnOn(); oven.SetOven(400); crust.PlaceInOven(oven); crust.RemoveFromOven(oven,15); oven.TurnOff();