OOD Case Study (For parallel treatment, see Chapter 2 of the text)

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Advertisements

©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
Object-Oriented Application Development Using VB.NET 1 Chapter 5 Object-Oriented Analysis and Design.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Introduction To System Analysis and Design
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 1 Principles of Programming and Software Engineering.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Chapter 10 Classes Continued
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
1 Creating Classes. 2 Writing Classes Thus far, we have mainly used existing classes in the Java library  (also main classes for executing) True object-oriented.
Chapter 2: Algorithm Discovery and Design
Object-Oriented Analysis and Design
Games and Simulations O-O Programming in Java The Walker School
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
Programming Languages and Paradigms Object-Oriented Programming.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Implementing the 8-puzzle Design (no corresponding text chapter)
Software Engineering Design & UML.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Introduction to Generics
Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Chapter 16: UML Class Diagrams
Chapter 16 UML Class Diagrams 1CS6359 Fall 2012 John Cole.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
CMSC 345 Fall 2000 OO Design. Characteristics of OOD Objects are abstractions of real-world or system entities and manage themselves Objects are independent.
Chapter 3: Introducing the UML
The Hashemite University Computer Engineering Department
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
11 Using the Keyboard in XNA Session 9.1. Session Overview  Discover more detail on how the XNA keyboard is implemented  Find out how to use arrays.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
Kyung Hee University Class Diagramming Notation OOSD 담당조교 석사과정 이정환.
Unit 1 - Introducing Abstract Data Type (ADT) Part 1.
1 M206 Chapter 31: An Overview of Software Development 1.Defining the problem 2.Analyzing the requirement – constructing initial structural model 3.Analyzing.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Principles of Programming & Software Engineering
Java Programming: Guided Learning with Early Objects
Principles of Programming and Software Engineering
About the Presentations
Object Oriented Programming
Presentation transcript:

OOD Case Study (For parallel treatment, see Chapter 2 of the text)

Elements of Class Design  Static class diagram  UML: Unified Modeling Language  Pseudocode for operations  Can be vague when it comes to details  Should be precise about control and function

Criteria for Good Class Design  Correctness: solves the problem  Simplicity  Cohesion: everything in a class directed at a single purpose  Coupling/Information Hiding: classes should never depend upon knowing the internal implementations of other classes  Error Handling: design should be robust.

Problem Statement: The 8-Puzzle The 8-puzzle is a device consisting of eight numbered and sliding tiles arranged in a 3x3 grid. An 8-puzzle problem is an initial state and a final state along with a list of legal actions that can be performed on states up downleft right initial final

8-Puzzle Problem Statement (cont'd)  You are to write a program that simulates the solving of an 8- puzzle problem.  Your program must display an initial state of the 8-puzzle and then prompt the player for an action. After displaying the initial state, the program repeatedly prompts for actions, displaying the results of each action until the final state is achieved.  If an action is not possible, that is, it attempts to move a tile into an occupied space or off the edge of the board, the program must issue a diagnostic message and reprompt the player. When the final state is reached, the number of actions made is displayed. Illegal attempted moves are not counted.

Important Aspects of the Problem Statement  It describes the problem and defines precisely what constitutes a solution  It makes clear the "rules of the game"  It uses diagrams and examples  It does not attempt to describe how the problem is to be solved, only what the problem is  It specifies the program behavior in sufficient detail

Identifying Potential Classes: Collect Problem Statement Nouns State 8-puzzle Device Board Grid Tile Space Action Action List Solver Problem Message Player

Analyzing Potential Classes State: holds all information about an 8-puzzle state including how to move tiles 8-puzzle, Device, Board, Grid: redundant Tile: unnecessary Space: part of a State Action: contains a function for moving a tile Action List: collection of Actions, one for each legal move Problem: creates the Action List. Contains initial State, final State, and Action List Solver: contains the Problem, knows how to prompt and check for solution Message: unnecessary Player: unnecessary

A Primitive Class Diagram Problem State Action ActionList Solver

Characterizing Classes  Q: How do you determine which class to start with?  A: Identify classes that do not depend on others for their definition:  The Solver object requires a Problem to solve  The Problem object requires an Initial State, Final State, and ActionList  The ActionList object requires Actions  The Action object requires a State on which to perform itself  This leaves the State class as a good one with which to begin.

Class Diagram with Dependencies Solver Problem ActionList State Action depends on

Elements of a Complete Class Diagram  All data specified with types  All operations specified with return types, parameters, and parameter types  All operations specified as public or private (data should never be public)  Class dependencies indicated via annotated connectors and arrows

Example: Solver Class Solver -problem: Problem +Solver(p: Problem) +solve(): void -promptActionName(): String

Notes on the Solver Class  A ``-'' prefix indicates the data or operation is private, a ``+'' prefix indicates public.  The constructor for a class has the same name as the class.  The Solver constructor requires a Problem object from which to get the initial and final States, and the Action List.  The solve operation is public since it will be called from outside the class.  The promptActionName operation is private because it is used only by solve.

Another Example: The Problem Class Problem -startState: State -finalState: State -actionList: ActionList +Problem(startTileNums: StringArray) +getStartState(): State +getFinalState(): State +getActionList(): ActionList -createActionList(): void

Notes on the Problem Class  The Problem constructor receives an array of strings from which to build the start State.  getStartState, getFinalState, and getActionList are data accessors used by the Solve class.  createActionList is a private operation that builds the ActionList by using the tile motion operations from the State class

Relationship of the Solver and Problem Classes Solver Problem -startState: State -finalState: State -actionList: ActionList problem 1 1

Class Associations  The Solver and Problem classes are related by association, a relationship between instances of classes.  The label ``problem'' is the role name; the role that the Problem class plays for the Solver class.  When there is a role name, it can be left out of the box in the interests of simplicity.  The numbers at either end of the line are multiplicities; in this case there is always one solver and one problem

Pseudocode for the solve() Operation void solve() current = get start state from problem final = get final state from problem alist = get action list from problem count = 0 while current is not final do display current action = get action from user if action is valid then new = apply action to current current = new increment count else issue message display current and count congratulate

Notes on the Pseudocode  Nonexecutable  Control structures used ( while, if...then)  Loop details not explicit  Code blocks indicated through indentation (personal preference)

State Class  Data: Representation of the 8-puzzle board Representation of the blank position  Operations: Create a new state Display the current configuration of the puzzle Check if state is equal to another Move blank up, down, left or right...

New Class Diagram Solver Problem -actionList: ActionList Problem 1 1 State 1 startState finalState 2

Action Class  Data: Name (up, down, left, right) Operation to perform the action--What, an operation as data??  Operations: Create a new action Execute the action operation...

Action Class Notes  Operations for moving the tile are already in the State class--why a separate Action class?  Because we want to abstract out as much of the generic design as possible for use with other problems  Abstraction is facilitated by having an ActionList composed of Actions.  To put an action on a list, it has to be regarded as data.  In C++, we will do this using pointers to static methods.

ActionList Class  Data: The actions The total number of actions  Operations: Create a new action list Add an action to the action list Find an action by name...

New Class Diagram Solver ActionList Action Problem State problem startState finalState actionList * 1 1

Comments on New Diagram  The relationship between ActionList and Action is one of aggregation, indicated by a diamond at the end of the line.  The multiplicity ``*'' means ``any number''.

The Art of OOD There is no one correct design for a given problem. You may wish to emphasize classes other than those chosen here. You should design your solutions in a way which is natural to you. However you should strive for SIMPLICITY. Also, you should design for REUSE (difficult).

Advantages of Design Simplicity  Easier to understand  Easier to implement  Easier to debug  Easier to maintain

Designing For Reuse  The general State class was chosen rather than the more specific 8-puzzle  The Action, ActionList, and Solver classes do not know anything about the 8-puzzle problem domain, so they can be applied to other domains  Advantage: Code can become part of a class library and used for other purposes

Example Class Definition in C++ class SolverInfo { private: Problem problem; public: SolverInfo(Problem p); void solve(); private: static String promptActionName(); };

Notes On the "SolverInfo" Class 1 Q: Why ``SolverInfo'' and not just ``Solver''?  Because Solver will be defined to be a pointer to a class object SolverInfo 2 No methods are defined in-line (defined in a separate file) 3 No data members are public 4 All public methods are part of the class interface

Example Main Program #include "ProblemInfo.H" #include "SolverInfo.H" int main(Integer argc, StringArray problemArgs) { Problem p = new ProblemInfo(problemArgs); Solver s = new SolverInfo(p); s->solve(); } Note that this program makes no mention of the 8-puzzle, and thus could be used on other problems suitably defined.