CSCI 102 Lecture #9 Factory Class Example

Purpose/Overview

The purpose of this program is to demonstrate the power and flexibility of a factory class. The user will input names of shapes from the console and behind the scenes a factory will instantiate and return randomly sized shapes based on the input from the user. When the user finishes entering shapes, the program will also use polymorphism to print out the type and area of each shape that was created.

Requirements

The application shall accepts names of shapes from the user via the console.

The application shall gracefully report and handle invalid shape names without crashing.

The application shall use user input shape names to generate corresponding shape objects via a factory class.

The application shall provide a way for the user to indicate they are done entering shapes.

The application, upon normal exit, shall display a summary to the user of all the shapes that were created and their areas.

Global Data/Functions

There is no global data in this application. All the data is contained within objects.

Objects

See the individual object documentation pages for more info on each object.

Point - A class representing a 2D (x,y) point in space. Shape - An abstract base class used to represent a Shape. Triangle - A class to represent a triangle shape in space. Inherits from Shape. Rectangle - A class to represent a rectangle shape in space. Inherits from Shape. Circle - A class to represent a circle shape in space. Inherits from Shape. ShapeFactory - A factory class used for generating Shape pointers from strings.

High-level architecture

The main method of the program reads user input strings and passes them to the ShapeFactory object. The ShapeFactory object is responsible for knowing how to create Shapes from strings and it will return Shape pointers. Triangle, Rectangle and Circle are only ever reference inside the ShapeFactory. Everywhere else the abstract Shape interface is used to pass around data and polymorphically call functions on the individual shapes. Each Shape contains a Point object that represents its physical location in 2D space (its center point).

The main flow of the code is:

1. Prompt the user for a string 2. Pass the string to the ShapeFactory to create a Shape* 3a. If the shape string is bad, do nothing and reprompt the user. Go to 2. 3b. If the shape string is good, add the created shape to a vector. Go to 2. 3c. If the input string is empty, end the program and dump a summary of created shapes.

Function Descriptions

All functions are described in detail on the documentation pages for the classes that either contain them or are friends of them.

User Interface

The only user interface is that the user will be prompted to enter string representations of shapes (e.g. "triangle") and the application will handling all the heavy lifting of creating randomly sized shapes. If the user enters a bad shape, they will be reprompted. If the user enters nothing, the program will end and dump a summary.

Testing

Test the main code with all the known existing shape types (Triangle, Circle, Rectangle) and make sure that it works. Also try entering the same shape multiple times (e.g. make 3 Circles).

Test that the main code handles bad shape names gracefully. If a user enters a string like "asdfsda" that is not a known shape, check that the application displays an error and reprompts.

Test that when the user enters an empty string, the program prints a summary and terminates.

 All Classes Functions Variables Friends

Generated by  doxygen 1.6.2
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees