410 likes | 426 Views
Learn about the principles of object-oriented design and the Unified Modeling Language (UML) in this chapter. Understand how objects can model real-world items and how to represent object-oriented designs using UML.
E N D
Chapter 2Thinking About Objects, Introduction to Object Technology and the Unified Modeling Language
Thinking About Objects: Introduction to Object Technology and the Unified Modeling Language • Object orientation • Unified Modeling Language (UML) • Graphical language that uses common notation • Allows developers to represent object-oriented designs
Thinking About Objects (cont.) • Objects • Reusable software components that model real-world items • Look all around you • People, animals, plants, cars, etc. • Attributes • Size, shape, color, weight, etc. • Behaviors • Babies cry, crawl, sleep, etc.
Thinking About Objects (cont.) • Object-oriented design (OOD) • Models real-world objects • Models communication among objects • Encapsulates data (attributes) and functions (behaviors) • Information hiding • Communication through well-defined interfaces • Object-oriented language • Programming is called object-oriented programming (OOP) • Java
Thinking About Objects (cont.) • Object-Oriented Analysis and Design (OOAD) • Essential for large programs • Analyze program requirements, then develop solution • Elevator-simulation case study
Thinking About Objects: Examining the Problem Statement • Emphasize object-oriented programming (OOP)
Thinking About Objects: Examining the Problem Statement • Program Goal • Software simulator application • 2-floor elevator simulator • Models actual elevator operation • Elevator graphics displayed to user • Graphical user interface (GUI) • User can control elevator
Thinking About Objects: Examining the Problem Statement • Elevator Simulation • Model people using elevator • Elevator door, floor door, elevator button, floor button, elevator shaft, bell, floor, backgrounds • Operate accordingly or by request to avoid “injuring” person and make useless operations • Create person objects • Simulation rules • Elevator visits floor which person requests for elevator service • One person per elevator • 5 seconds to move from floors
Thinking About Objects: Examining the Problem Statement • Application GUI • First Floor/Second Floor buttons create person on respective floors • Disable button if floor occupied by a person already • Unlimited number of passenger creations • Animation requirements • Passenger walking and pressing floor button • Elevator moving, doors opening and closing • Illumination of elevator lights and buttons during operation • Incorporating sounds • Footsteps when person walks • Button pressing clicks • Elevator bell rings upon elevator arrival, elevator music • Doors creak when opening and closing
Thinking About Objects: Examining the Problem Statement • Designing elevator system • Specified in requirements document through OOD analysis • UML • Design used to implement Java code • How system should be constructed to complete tasks • System Structure • System is a set of interactive components to solve problems • Simplified by subsystems • Simulator, GUI, display • Describes system’s objects and inter-relationships • System behavior describes how system changes through object interaction
Thinking About Objects: Examining the Problem Statement • UML diagram types • System structure • Class diagram • Models classes, or “building blocks” of a system • Person, elevator, floor, etc. • Object diagrams • Snapshot (model) of system’s objects and relationships at specific point in time • Component diagrams • Model components such as graphics resources and class packages that make up the system • Deployment diagrams • Model hardware, memory and runtime resources
Thinking About Objects: Examining the Problem Statement • System behavior • Statechart diagrams • Model how object changes state • Condition/behavior of an object at a specific time • Activity diagrams • Flowchart modeling order and actions performed by object • Collaboration diagrams • Emphasize what interactions occur • Sequence diagrams • Emphasize when interactions occur • Use-case diagrams • Represent interaction between user and system • Clicking elevator button
Thinking About Objects: Identifying the Classes in a Problem Statement • Identifying classes in a System • Nouns of system to implement elevator simulation
Thinking About Objects: Identifying the Classes in a Problem Statement • Not all nouns pertain to model (not highlighted) • Company and building not part of simulation • Display, audio, and elevator music pertain to presentation • GUI, user of application, First and Second Floor buttons • How user controls model only • Capacity of elevator only a property • Energy preservation not modeled • Simulation is the system • Elevator and elevator car are same references • Disregard elevator system for now
Thinking About Objects: Identifying the Classes in a Problem Statement • Nouns highlighted to be implemented in system • Elevator button and floor button separate functions • Capitalize class names • Each separate word in class name also capitalized • ElevatorModel, ElevatorShaft, Elevator, Person, Floor, ElevatorDoor, FloorDoor, ElevatorButton, FloorButton, Bell, and Light
Thinking About Objects: Identifying the Classes in a Problem Statement • Using UML to model elevator system • Class diagrams models classes and relationships • Model structure/building blocks of system • Representing class Elevator using UML • Top rectangle is class name • Middle contains class’ attributes • Bottom contains class’ operations
Thinking About Objects: Identifying the Classes in a Problem Statement • Class associations using UML • Elided diagram • Class attributes and operations ignored • Class relation among ElevatorShaft, Elevator and FloorButton • Solid line is an association, or relationship between classes • Numbers near lines express multiplicity values • Indicate how many objects of class participate association
Thinking About Objects: Identifying the Classes in a Problem Statement • Diagram shows two objects of class FloorButton participate in association with one object of ElevatorShaft • FloorButton has two-to-one relationship with ElevatorShaft
Thinking About Objects: Identifying the Classes in a Problem Statement • Associations can be named • In diagram, “Requests” indicates association and arrow indicates direction of association • One object of FloorButton requests one object of class Elevator • Similar context with “Resets” and “Signals Arrival” • Aggregation relationship • Implies whole/part relationship • Some object “has” some object • Object attached with diamond is “owner” • Object on other end is the “part” • In diagram, elevator shaft “has an” elevator and two floor buttons
Thinking About Objects: Identifying the Classes in a Problem Statement Class diagram for the elevator model.
Thinking About Objects: Identifying the Classes in a Problem Statement • The complete class diagram for elevator model • Several of many and aggregates • ElevatorModel aggregates one ElevatorShaft and two Floor • Elevator is aggregation of ElevatorDoor, ElevatorButton and Bell • Several of many associations • Person “presses” buttons • Person also “rides” Elevator and “walks” across Floor
Thinking About Objects: Identifying Class Attributes • Classes have attributes (data) • Implemented in Java programs as variables • Attributes of real-world objects • Radio (object) • Station setting, volume setting, AM or FM (attributes) • Identify attributes • Look for descriptive words and phrases in problem statement • Each identified word and phrase is a candidate attribute • e.g., “the elevator is moving” • “is moving” corresponds to boolean attribute moving • e.g., “the elevator takes five seconds to travel between floors” • corresponds to int attribute travelTime • int travelTime = 5; (in Java)
Identifying Class Attributes (cont.) • UML class diagram • Class attributes are place in the middle compartment • Attributes are written language independently • e.g., attribute open of class ElevatorDoor • open : Boolean = false • May be coded in Java as • booleanopen = false;
Thinking About Objects: Identifying Class Operations • Class operations • Also known as behaviors • Service the class provides to “clients” (users) of that class • e.g., radio’s operations • Setting its station or volume
Thinking About Objects (cont.) • Deriving class operations • Use problem statement • Identify verbs and verb phrases • Verbs can help determine class operations
Thinking About Objects (cont.) • Deriving class operations • Verbs can help determine class operations • e.g., verb phrase “the elevator resets its button” • Elevator informs ElevatorButton to reset • ElevatorButton needs method resetButton • e.g., verb phrase “the elevator opens its door” • Elevator informs ElevatorDoor to open • ElevatorDoor needs method openDoor
Thinking About Objects (cont.) • Deriving class operations • Not all verbs determine class operations • e.g., verb phrase “the elevator arrives at a floor” • Elevator decides when to arrive • (after traveling 5 seconds) • i.e., no object causes Elevator to arrive • Elevator does not need to provide “arrival” service for other objects • arriveElevator is not a valid method (operation) • We do not include method arriveElevator
Thinking About Objects (cont.) • Store methods (operations) in UML class diagram • Place class methods in bottom compartment of that class
ElevatorModel numberOfPeople : Integer=0 addPerson( ) : void ElevatorShaft <none yet> <none yet> ElevatorDoor open : Boolean = false addPerson( ) : void Person ID : Integermoving : Boolean = true doorOpened( ) : void Floor floorNumber : Integercapacity : Integer = 1 <none yet> Light lightOn : Boolean = false turnOnLight( ) : voidturnOffLight( ) : void Elevator moving : Boolean = falsesummoned : Boolean = falsecurrentFloor : Integer = 1destinationFloor : Integer = 2capacity : Integer = 1travelTime : Integer = 5 ride( ) : voidrequestElevator( ) : voidenterElevator( ) : voidexitElevator( ) : voiddepartElevator( ) : void ElevatorButton pressed : Boolean = false resetButton( ) : voidpressButton( ) : void Bell <none yet> ringBell( ) : void FloorButton pressed : Boolean = false resetButton( ) : voidpressButton( ) : void ElevatorDoor open : Boolean = false openDoor( ) : voidcloseDoor( ) : void
Thinking About Objects: Starting to Program the Classes for the Elevator Simulation • Visibility • Apply member-access modifiers to class members • public methods • to provide services to clients • private variables • To promote encapsulation
Thinking About Objects (cont.) • Class diagram (UML) • Member-access modifiers • public • Denoted by plus sign (+) • private • Denoted by minus sign (-)
ElevatorModel ElevatorShaft ElevatorDoor - numberPeople : Integer = 0 <none yet> - open : Boolean = false + addPerson( ) : void <none yet> + openDoor( ) : void + closeDoor( ) : void Person Floor Light - ID : Integer - floorNumber : Integer - moving : Boolean = true - lightOn : Boolean = false - capacity : Integer = 1 + doorOpened( ) : void + turnOnLight( ) : void <none yet> + turnOffLight( ) : void ElevatorButton Bell - pressed : Boolean = false <none yet> + resetButton( ) : void + ringBell( ) : void + pressButton( ) : void FloorButton FloorDoor - pressed : Boolean = false - open : Boolean = false + resetButton( ) : void + openDoor( ) : void + pressButton( ) : void + closeDoor( ) : void Complete class diagram with visibility notations. Elevator - moving : Boolean = false - summoned:Boolean = false - currentFloor : Integer = 1 - destinationFloor:Integer = 2 - capacity : Integer = 1 - travelTime : Integer = 5 + ride( ) : void + requestElevator( ) : void + enterElevator( ) : void + exitElevator( ) : void + departElevator( ) : void
Thinking About Objects (cont.) • Implementation • Forward engineering • Transform design (i.e., class diagram) to code
Thinking About Objects (cont.) • We generate “skeleton code” with our design • Use class Elevator as example • Four steps: • Use name in first compartment to declare public class • Empty constructor • Use second compartment to declare member variables • Use class diagram associations for object references • Use third compartment to declare methods
Thinking About Objects (cont.)Step 1 public class Elevator {public Elevator() {} }
Thinking About Objects (cont.)Step 2 public class Elevator {// class attributesprivate boolean moving;private boolean summoned;privateint currentFloor = 1;privateint destinationFloor = 2;private int capacity = 1;private int travelTime = 5;// class constructorpublic Elevator() {}}
Thinking About Objects (cont.)Step 3 public class Elevator {// class attributesprivate boolean moving;private boolean summoned;privateint currentFloor = 1;privateint destinationFloor = 2;private int capacity = 1;private int travelTime = 5;// class objectsprivate ElevatorDoor elevatorDoor;private ElevatorButton elevatorButton;private Bell bell;// class constructorpublic Elevator() {}}
Thinking About Objects (cont.)Step 4 public class Elevator {// class attributesprivateboolean moving;private boolean summoned;privateint currentFloor = 1;privateint destinationFloor = 2;privateint capacity = 1;private int travelTime = 5;// class objectsprivate ElevatorDoor elevatorDoor;private ElevatorButton elevatorButton;private Bell bell;// class constructorpublic Elevator() {}// class methodspublic void ride() {}public void requestElevator() {}public void enterElevator() {}public void exitElevator() {}public void departElevator() {}}