1 / 7

OO Design Principles

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

margot
Download Presentation

OO Design Principles

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OO Design Principles lecture 08, OO Design Principle

  2. 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

  3. Principle 1 • Minimize the accessibility of Class and Members lecture 08, OO Design Principle

  4. 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

  5. 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

  6. 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

  7. 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

More Related