ch7DesignClasses

ch7DesignClasses - Chapter 7 Designing Classes A class...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 7 Designing Classes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Choosing Classes A class represents a single concept Concepts from mathematics: Point Rectangle Ellipse Concepts from real life BankAccount Purse Actors (end in -er, -or) Random (better called RandomNumberGenerator) Utility (helper)classes--no objects, only static methods Math All nouns Does some kind of work for you
Background image of page 2
Cohesion Cohesive = public interface closely related to the single concept that the class represents This class lacks cohesion: public class Purse { public Purse(){. ..} public void addNickels(int count){. ..} public void addDimes(int count){. ..} public void addQuarters(int count){. ..} public double getTotal(){. ..} public static final double NICKEL_VALUE =0.05; public static final double DIME_VALUE =0.1; public static final double QUARTER_VALUE =0.25; . .. }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Cohesion Two concepts: purse and coin Solution: Make two classes: public class Coin { public Coin(double aValue,String aName){. ..} public double getValue(){. ..} } public class Purse { public Purse(){. ..} public void add(Coin aCoin){. ..} public double getTotal(){. ..} } Purse Coin
Background image of page 4
Coupling A class depends on another if it calls one of its methods Purse depends on Coin because it calls getValue on coins Coin does not depend on Purse High Coupling = many class dependencies Minimize coupling to minimize the impact of interface changes
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dependency Relationship between Purse and Coin Classes
Background image of page 6
High and Low Coupling between Classes
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Review of a class-setup
Background image of page 8
Encapsulation Hiding the implementation details of a class (making all fields and helper methods private ) is called encapsulation . Encapsulation helps in program maintenance and team development. A class encapsulates a small set of well-defined tasks that objects of a class can perform.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
public vs. private Public constructors and methods of a class constitute its interface with classes that use it — its clients . All fields are usually declared private — they are hidden from clients. Static constants occasionally may be public . “Helper” methods that are needed only inside the class are declared private .
Background image of page 10
public vs. private (cont’d) public class MyClass { // Constructors: public MyClass (. ..) { . .. } ... // Public methods: public < sometype > myMethod (. ..) { . .. } ... // Private methods: private < sometype > myMethod (. ..) { . .. } ... // Private fields: private < sometype > myField; ... }
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Constructors A constructor is like a method for creating objects of the class. A constructor often initializes an object’s fields. Constructors do not have a return type (not even void ) and they do not return a value. All constructors in a class have the same name — the name of the class. Constructors may take arguments.
Background image of page 12
Constructors (cont’d) If a class has more than one constructor, they are “overloaded” and must have different numbers and/or types of arguments.
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 60

ch7DesignClasses - Chapter 7 Designing Classes A class...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online