Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At...

20
Object Conversations Stephen P Levitt School of Electrical and Information Engineering University of the Witwatersrand 2019

Transcript of Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At...

Page 1: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

Object Conversations

Stephen P Levitt

School of Electrical and Information EngineeringUniversity of the Witwatersrand

2019

Page 2: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

Outline

1 Introduction

2 Encapsulation (is not good enough)

3 Information HidingExample

4 Summary

Object Conversations 1 / 19

Page 3: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

Object Conversations

At run-time OO programmes consist of object conversationsGood conversations are those in which objects do not reveal their secretsThis gives us the flexibility to change the internals (representation or logic) ofthese objects without affecting the client code

Object Conversations : Introduction 2 / 19

Page 4: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

Encapsulation

“ The concept of encapsulation as used in an object-oriented context is notessentially different from its dictionary definition. It still refers to buildinga capsule, in this case a conceptual barrier, around some collection ofthings.” — Wirfs-Brock et al, 1990, in Designing Object-Oriented Software

Object Conversations : Encapsulation (is not good enough) 3 / 19

Page 5: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

Encapsulation But No Information Hiding I

class Student{public:Student(const string& surname,

const string& student_num):surname_(surname),student_num_(student_num){}

// public data membersstring surname_;string student_num_;

private:

};

Object Conversations : Encapsulation (is not good enough) 4 / 19

Page 6: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

Encapsulation But No Information Hiding II

// ---- in main ----

Student ada("Lovelace", "4514567N");

// data is encapsulated - no direct access

cout << ada.surname_<< endl;cout << ada.student_num_ << endl;

Object Conversations : Encapsulation (is not good enough) 5 / 19

Page 7: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

Avoid Public Data Members

Having public data members means that part of your class’s state can varyuncontrollably and unpredictably — objects of the class cannot guarantee that theinvariants will be maintained, and this responsibility is now shared with all clients

Object Conversations : Encapsulation (is not good enough) 6 / 19

Page 8: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

Encapsulation - Is this better?

class Student{public:Student(const string& surname,

const string& student_num):surname_(surname),student_num_(student_num){}

string surname() { return surname_; }void surname(string new_surname) { surname_ = new_surname; }

// also get,set for student numberprivate:

string surname_;string student_num_;

};Object Conversations : Encapsulation (is not good enough) 7 / 19

Page 9: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

Providing a Getter for Screen

class Screen {public:Screen(string::size_type height = 8, string::size_type width = 40,

char bkground = '#');

// move the cursor to the specified row and columnvoid move(string::size_type row, string::size_type col);

// write a character on the screen at the current cursor positionvoid set(char c);

// get the character at the specified row and columnchar get(string::size_type row, string::size_type col);

// get the Screen's internal string: _screenstring getScreenString();

}Object Conversations : Encapsulation (is not good enough) 8 / 19

Page 10: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

Testing Screen Using the Getter

This works but is it a good test?

TEST_CASE("Can set character at a specific position") {

Screen myScreen{3,3};string blank_screen = "#########";CHECK(blank_screen == myScreen.getScreenString());

myScreen.move(2,1);myScreen.set('*');

string screen_with_character = "###*#####";CHECK(screen_with_character == myScreen.getScreenString());

}

Object Conversations : Encapsulation (is not good enough) 9 / 19

Page 11: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

Providing Getters and Setters

Using simple getters and setters is a little better than public data. These allowus to:

preserve invariantsadd instrumentation and perform additional work in future

without breaking the calling codeHowever, they also reveal the secrets of the object involvedMaking data members private is only the first step on the road to informationhiding

Object Conversations : Encapsulation (is not good enough) 10 / 19

Page 12: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

Getters and Setters

“ […] getters and setters do not achieve encapsulation or information hiding:they are a language-legitimized way to violate them.””— Coplien et al, 2010, in Lean Architecture

Object Conversations : Encapsulation (is not good enough) 11 / 19

Page 13: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

Information Hiding

“ Information hiding: a module is characterized by the information it hidesfrom other modules, which are called its clients. The hidden informationremains a secret to the client modules.”— Ghezzi et al, 1991, in Fundamentals of Software Engineering

Object Conversations : Information Hiding 12 / 19

Page 14: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

Information Hiding

Source: Object-Oriented Analysis and Design, 2nd ed., G. Booch, 1994

Object Conversations : Information Hiding 13 / 19

Page 15: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

Information Hiding - Example 1

class Premium{public:

// discount flag is true if no claim bonus is// in effect, false otherwisebool getDiscountFlag();void setDiscountFlag(bool flag);void setAmount(double new_amount);double getAmount();

private://...

};

Object Conversations : Information Hiding : Example 14 / 19

Page 16: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

Information Hiding - Example 1 cont.

Add code to AddClaim to cancel the no-claim bonus, that is, increase the premium by15%.

class ClaimsHistory{public:void AddClaim(const Claim& new_claim, Premium& premium){// ..if (withinYearOfPreviousClaim(new_claim.date())){// clients lose their no-claim bonus if it// is in effect

}}

Object Conversations : Information Hiding : Example 15 / 19

Page 17: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

A Better Intention-Revealing Interface For Premium

class Premium{public:

bool isDiscounted();void markAsNotDiscounted();void increaseBy(int percentage);

private://...

};

Object Conversations : Information Hiding : Example 16 / 19

Page 18: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

But Still ...

In ClaimsHistory:

if (premium.isDiscounted()) {if (premium.isPaidMonthly())

premium.increaseBy(14);else

premium.increaseBy(15);}

premium.markAsNotDiscounted();

Object Conversations : Information Hiding : Example 17 / 19

Page 19: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

An Even Better Interface For Premium

class Premium{public:// ClaimsHistory can simply tell Premium what to do// without having to ask it for any informationvoid cancelNoClaimBonus();

private://...

};

Object Conversations : Information Hiding : Example 18 / 19

Page 20: Object Conversations - GitHub Pages · Object Conversations 1 / 19. Object Conversations At run-time OO programmes consist of object conversations Good conversations are those in

The Key Principle for Hiding Information

“ Procedural code gets information then makes decisions. Object-orientedcode tells objects to do things.” — Alec Sharp

Move behaviour close to the data that it operates onIf you make decisions concerning the object’s state outside the object, you force itto reveal its secrets

The Key Principle: Tell, Don’t Ask

Object Conversations : Summary 19 / 19