130 likes | 140 Views
Learn how to identify objects, determine their responsibilities, and discover relationships in object-oriented design. Explore attributes, services, and different types of relationships through simplified blackjack game example.
E N D
CMSC 202 A Design Exercise
OO design activities • Finding the Objects • Defining the responsibilities • Services • Attributes • Discovering relationships
Finding Objects • In OO design, objects are the building blocks of our system • Identifying objects is the most important and most difficult step • Use the things to be modeled (nouns) relying on experience and knowledge of the application domain • Natural, direct, reliable • But, only helps us find tangible objects and terminal objects (those with no subclasses) • Other techniques exist
Determining Responsibilities • An object is an abstraction of something in the problem domain reflecting the capabilities of the system to keep information about it, interact with it, or both • In OOD, we are interested in an object for its services. From system perspective, an object is defined by it’s public interface. • But also need to identify the data necessary to support those services • So, an object is the encapsulation of the of the attribute values (data) and associated methods (services)
What’s an Attribute? • From analysis point of view it’s an abstraction of a real world characteristic from the problem domain • From technical point of view, it’s a variable for which each instance of a class (object) holds it’s own value
Identifying Attributes • What data do we believe the object is responsible for knowing and owning? • Look for adjectives and possessive phrases • 40-year old man • Color of the truck • position of the cursor
Specifying attributes • Yourdon/Coad – “Make each attribute capture an atomic concept” • Each attribute is either a single value or tightly related group of variables that the application treats as a whole • Age • Address (street, city, zipcode) • Birthday (month, day, year)
What’s a service? • Public work an object is willing to perform when requested by another object via message passing • Defined by prototypes
Identifying Services • Look for action verbs in the problem. • English sentence form is usually “subject – action verb – object” • Example : “A person hit the ball” • For OOD, this means that the Ball object defines the “receive a hit” service. • Ball must have a prototype service to receive the “hit” message from the person.
Specifying A Service • By defining the prototype • Give a generic name • Define the signature by identifying argument list
Discovering Relationships • Before object A can send a message to object B (to request a service), object A must have a “handle” to object B. • One way for the calling object to have such a handle is to have a relationship (aggregation/composition or link) with the other object. • No object is an island – objects typically depend on other objects for services. These interdependencies are called relationships. • Relationships carry semantic meaning too
Types of relationships • Generalization/specification • “is a” (later) • Aggregation (composition) • “whole-part” or “has a” • Objects composed of other objects • Parts have some functional/structural relationship to the whole • Links between objects (physical or conceptual) • Often seen as verbs -- “keeps track of”
Simplified BlackJack An interactive program to play the game of blackjack. The computer acts as the dealer and will play against the user (like in a casino). The object of the game is to get your cards as close to 21 without going over. Face cards count 10, Aces count 11. A hand begins with the player and dealer each getting two cards. The player asks for up to 3 more cards, one at a time, from the dealer until he wishes to stop or the total is over 21. The dealer then draws up to 3 more cards, one at a time while the total of his hand is less than 21. Once the dealers hand reaches 17 or higher, no more cards are dealt. If the dealer’s hand and player’s hand have the same value, or both are over 21, the hand is a draw. Otherwise, the hand closest to 21 wins. Hands are dealt repeatedly until there are not enough cards in the deck to deal a complete hand.