Download presentation
Presentation is loading. Please wait.
1
12 OBJECT-ORIENTED DESIGN CHAPTER
Copyright © 2013 by John Wiley & Sons. All rights reserved. Slides by Rick Giles
2
OO Class Design Encapsulation – access to the data and code is tightly controlled by an interface. Cohesion – the degree in which a class has a single, well-defined role or responsibility. Has to do with how an individual class is designed. Coupling – the degree to which one class knows about or uses members of another class. Has to do with how classes interact with each other. Copyright © 2013 by John Wiley & Sons. All rights reserved.
3
Cohesion (1) A class should represent a single concept
The public interface of a class is cohesive if all of its features are related to the concept that the class represents Copyright © 2013 by John Wiley & Sons. All rights reserved.
4
Cohesion (2) This class lacks cohesion:
public class CashRegister { public static final double NICKEL_VALUE = 0.05; public static final double DIME_VALUE = 0.1; public static final double QUARTER_VALUE = 0.25; … public void enterPayment(int dollars, int quarters, int dimes, int nickels, int pennies) } It involves two concepts: cash register and coin Copyright © 2013 by John Wiley & Sons. All rights reserved.
5
Cohesion (3) Better: Make two classes:
public class Coin { public Coin(double aValue, String aName) { } public double getValue() { } } public class CashRegister { public void enterPayment(int coinCount, Coin coinType) { } } Copyright © 2013 by John Wiley & Sons. All rights reserved.
6
Coupling (1) If many classes depend on each other, the coupling between classes is high Good practice: minimize coupling between classes Change in one class may require update of all coupled classes Using a class in another program requires using all classes on which it depends Copyright © 2013 by John Wiley & Sons. All rights reserved.
7
Coupling (2) Copyright © 2013 by John Wiley & Sons. All rights reserved.
8
Good Class Design Tight encapsulation – hide implementation behind and interface Instance variables are protected Getter/Setter methods provide access High Cohesion – classes whose members support a single, well-focused role or responsibility Loose Coupling – classes are well encapsulated, minimize references to each other. Use only APIs, not knowledge of implementation. Copyright © 2013 by John Wiley & Sons. All rights reserved.
9
Benefits of Good Class Design
Tight encapsulation and Loose Coupling – ability to modify implemented code without breaking the code of others who use our code. Gives maintainability, flexibility and extensibility to code. High Cohesion – classes are easier to maintain and reuse Copyright © 2013 by John Wiley & Sons. All rights reserved.
10
Class Relationships weak strong Dependency (“knows about”)
Needs another class to do its job Local variable, parameter variable Association (“knows about”) Dependency plus An attribute is object of another class Aggregation (“has a”) Association plus whole/part relationship Composition (“has a”) Aggregation plus one object manages lifetime of another object Inheritance (“is a”) Copyright © 2013 by John Wiley & Sons. All rights reserved.
11
A class depends on another if it uses objects of that class
Dependency A class depends on another if it uses objects of that class “knows about” relationship CashRegister depends on Coin to determine the value of the payment public class CashRegister { public void enterPayment(int coinCount, Coin coinType) { } } Copyright © 2013 by John Wiley & Sons. All rights reserved.
12
Dependency Relationship
Copyright © 2013 by John Wiley & Sons. All rights reserved.
13
Association An association is a structural relationship that indicates that an object of one class has a reference to an object of another class. An association supports data sharing between classes. Multiplicity information can be linked to an association to show how many instances of class A are linked with instances of class B. Multiplicity information can be linked to both ends of association relationships. Copyright © 2013 by John Wiley & Sons. All rights reserved.
14
Aggregation A class contains an instance of another class
public class Address { } public class Person { private Address personAddress; public Person(Address add) { personAddress = add;} } Models a “has-a” relationship Person doesn’t manage lifetime of Address In main method: //object instantiated outside of class Address myAddress = new Address(); Person me(myAddress);
15
Aggregation (1) A class aggregates another of its objects contain objects of another class “has-a” relationship Example: a quiz is made up of questions Class Quiz aggregates class Question Copyright © 2013 by John Wiley & Sons. All rights reserved.
16
Aggregation (2) Finding out about aggregation helps in implementing classes Example: since a quiz can have any number of questions, use an array or array list for collecting them public class Quiz { private ArrayList<Question> questions; . . . } Copyright © 2013 by John Wiley & Sons. All rights reserved.
17
Composition A class contains an instance of another class.
public class Engine { } public class Car { private Engine e; public Car() { e = new Engine(); } //object instantiated // inside class } Models a “part-of” relationship Car manages the lifetime of Engine
18
Aggregation v. Composition
House:Room Building:Floor Bowl:Bowling Ball Read:Book Desk:Drawer DVDWriter:DVD
19
Inheritance (1) Inheritance is the relationship between a more general class (superclass) and a more specialized class (subclass) “is-a” relationship Example: every car is a vehicle; every car has tires Class Car is a subclass of class Vehicle; class car aggregates class Tire Copyright © 2013 by John Wiley & Sons. All rights reserved.
20
Inheritance (2) public class Car extends Vehicle { private Tire[] tires; } Copyright © 2013 by John Wiley & Sons. All rights reserved.
21
UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has three main sections. ClassName Attributes Methods UML diagrams are easily converted to Java class files The class name should concisely reflect what the class represents.
22
UML Relationship Symbols
Composition Solid Solid Diamond Association None Copyright © 2013 by John Wiley & Sons. All rights reserved.
23
Object Oriented Program Design
To discover classes, look for nouns in the problem description Example: Print an invoice Candidate classes: Invoice LineItem Customer Copyright © 2013 by John Wiley & Sons. All rights reserved.
24
Finding Classes & Responsibilities
Finding the classes Get written description of the problem domain Identify all nouns, each is a potential class Refine list to include only classes relevant to the problem Identify the responsibilities Things a class is responsible for knowing Things a class is responsible for doing
25
OOP Oriented Analysis Problem
Problem Domain Joe’s Automotive Shop services foreign cars, and specializes in servicing cars made by Mercedes, Porsche, and BMW. When a customer brings a car to the shop, the manager gets the customer’s name, address, and telephone number. The manager then determines the make, model and year of the car, and gives the customer a service quote. The service quote shows the estimated parts charges, estimated labor charges, sales tax, and total estimated charges. Identify class or classes need for this problem Identify responsibilities of the class or classes
26
Identify All of the Nouns
Joe’s Automotive Shop services foreign cars, and specializes in servicing cars made by Mercedes, Porsche, and BMW. When a customer brings a car to the shop, the manager gets the customer’s name, address, and telephone number. The manager then determines the make, model and year of the car, and gives the customer a service quote. The service quote shows the estimated parts charges, estimated labor charges, sales tax, and total estimated charges.
27
Identify All of the Nouns
Joe’s Automotive Shop foreign cars cars Mercedes Porsche BMW customer car shop manager customer name address telephone number manager make model year car customer service quote estimated parts charges estimated labor charges sales tax total estimated charges
28
Refine List of Nouns Some of the nouns really mean the same thing.
Some nouns might represent items that we do not need to be concerned with in order to solve the problem. Some of the nouns might represent objects, not classes. Some of the nouns might represent simple values that can be stored in a primitive variable and do not require a class.
29
Which nouns really mean the same thing?
Joe’s Automotive Shop foreign cars cars Mercedes Porsche BMW customer car shop name address telephone number manager make model year service quote estimated parts charges estimated labor charges sales tax total estimated charges
30
Which nouns really mean the same thing?
Joe’s Automotive Shop foreign cars cars Mercedes Porsche BMW customer car shop name address telephone number manager make model year service quote estimated parts charges estimated labor charges sales tax total estimated charges
31
Which nouns represent items that we don’t need to solve the problem?
cars Mercedes Porsche BMW customer car shop name address telephone number manager make model year service quote estimated parts charges estimated labor charges sales tax total estimated charges
32
Which nouns represent items that we don’t need to solve the problem?
cars Mercedes Porsche BMW customer car shop name address telephone number manager make model year service quote estimated parts charges estimated labor charges sales tax total estimated charges
33
Which nouns represent objects, not classes?
cars Mercedes Porsche BMW customer car name address telephone number make model year service quote estimated parts charges estimated labor charges sales tax total estimated charges
34
Which nouns represent objects, not classes?
cars Mercedes Porsche BMW customer car name address telephone number make model year service quote estimated parts charges estimated labor charges sales tax total estimated charges
35
cars customer car name address telephone number make
Which nouns represent simple values that can be stored in primitive variables and don’t require a class? cars customer car name address telephone number make model year service quote estimated parts charges estimated labor charges sales tax total estimated charges Would you use a group of related values to represent the item’s state? Are there any obvious actions to be performed by the item?
36
Which nouns represent simple values that can be stored in primitive variables and don’t require a class? cars customer car name address telephone number make model year service quote estimated parts charges estimated labor charges sales tax total estimated charges
37
Finding Classes Responsibilities
Finding the classes Get written description of the problem domain Identify all nouns, each is a potential class Refine list to include only classes relevant to the problem Identify the responsibilities Things a class is responsible for knowing Things a class is responsible for doing
38
Customer Class Responsibilities
Things a class is responsible for knowing Things a class is responsible for doing name address telephone number make model year estimated parts charges estimated labor charges sales tax total estimated charges
39
Customer Class Responsibilities
Things a class is responsible for knowing the customer’s name the customer’s address the customer’s telephone number Things a class is responsible for doing create an object of the Customer class set and get the customer’s name set and get the customer’s address set and get the customer’s telephone number
40
Customer Class UML Diagram
41
Car Class Responsibilities
Things a class is responsible for knowing Things a class is responsible for doing name address telephone number make model year estimated parts charges estimated labor charges sales tax total estimated charges
42
Car Class Responsibilities
Things a class is responsible for knowing the car’s make the car’s model the car’s year Things a class is responsible for doing create an object of the Car class set and get the car’s make set and get the car’s model set and get the car’s year
43
Car Class UML Diagram
44
ServiceQuote Class Responsibilities
Things a class is responsible for knowing Things a class is responsible for doing name address telephone number make model year estimated parts charges estimated labor charges sales tax total estimated charges
45
ServiceQuote Class Responsibilities
Things a class is responsible for knowing the service quote’s estimated parts charges the service quote’s estimated labor charges Things a class is responsible for doing create an object of the ServiceQuote class set and get the estimated parts charges set and get the estimated labor charges calculate sales tax calculate total estimated charges
46
ServiceQuote UML Diagram
47
Class Collaboration Collaboration – two classes interact with each other If an object is to collaborate with another object, it must know something about the second object’s methods and how to call them
48
Examples of Collaboration
ServiceQuote object calls the Customer object’s getName, getAddress and getPhone methods ServiceQuote object calls the Car object’s getMake, getModel and getYear methods
49
The CRC Card Method A CRC card describes a class, its responsibilities, and its collaborating classes. For each responsibility of a class, its collaborators are the other classes needed to fulfill it Copyright © 2013 by John Wiley & Sons. All rights reserved.
50
CRC Card Copyright © 2013 by John Wiley & Sons. All rights reserved.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.