90 likes | 224 Views
In this lecture, we explore essential object-oriented design principles that guide the creation of adaptable software systems. As software requirements frequently evolve due to client demands and changing development environments, a robust design enables seamless modifications throughout the development process. We examine the significance of information hiding, minimizing class accessibility, and employing accessors and mutators. These practices help enforce constraints, allow internal representation changes without affecting interfaces, and provide the flexibility to manage side effects safely. Implementing these principles ensures higher code maintainability and reliability.
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