90 likes | 211 Views
OO Design Principles. What is a Good Design SW?. During software development, software requirements can keep changing: From clients: clients might change what they want, From developers: developers might change the development environment
E N D
OO Design Principles lecture 08, OO Design Principle
What is a Good Design SW? • During software development, software requirements can keep changing: • From clients: clients might change what they want, • From developers: developers might change the development environment • A good design software system should make the changes easy. Software System Can Be Changed During the Development Process !!! lecture 08, OO Design Principle
Principle 1 • Minimize the accessibility of Class and Members lecture 08, OO Design Principle
Information Hiding in Java • Use private members and appropriate accessors and mutators wherever possible. • For example: • Replace public double speed; • With private double speed; public double getSpeed() { return speed;} public void setSpeed(double newSpeed) { speed = newSpeed;} lecture 08, OO Design Principle
Advantage of Mutator/Accessor • You can put constraints on values • If users of your code access the fields directly, then they would be responsible for checking constraints. public void setSpeed(double newSpeed) { if (newSpeed < 0) { sendErrorMsg(….); speed = Math.abs(newSpeed); } speed = newSpeed; } lecture 08, OO Design Principle
Advantage of Mutator/Accessor • You can change your internal representation without changing interfaces. // using KPH not KMP to represent speed public void setSpeed(double newSpeed) { speedInKPH = convert(newSpeed); } public double getSpeed() { return (convert’(speedInKPH)); } lecture 08, OO Design Principle
Advantage of Mutator/Accessor • You can perform arbitrary side effects • If users of your code to accessed the fields directly, then they would be responsible for executing side effects // speed can be represented by different formats public void setSpeed(double newSpeed) { speed = newSpeed; notifyObservers(); } lecture 08, OO Design Principle