The Secret Life of Objects: Information Hiding

Introduction

Messages are the core

struct Rectangle {
double height;
double length;
};
double area(Rectangle r)
{
// Code that computes the area of a rectangle
}
void scale(Rectangle r, double factor)
{
// Code that changes the rectangle r, mutating its components directly
}
trait Shape {
def area: Double
def scale(factor: Double): Shape
}
case class Rectangle(height: Double, length: Double) extends Shape {
// Definition of functions declared abstract in Shape trait
}

Information hiding and class definition

def scale(height: Double, length: Double, factor: Double): (Double, Double) = {
(height \* factor, length \* factor)
}
def area(height: Double, length: Double): Double = {
height * length
}
type Rectangle = (Double, Double)
case class Rectangle(height: Double, length: Double) {
def scale(factor: Double): Rectangle = Rectangle(height * factor, length)
val area: Double = height * length
}
trait Shape {
def scale(factor: Double): Shape
def area: Double
}
case class Rectangle(height: Double, length: Double) extends Shape {
// Definition of functions declared abstract in Shape trait
}
case class Square(length: Double) extends Shape {
// Definition of functions declared abstract in Shape trait
}
case class Circle(ray: Double) extends Shape {
// Definition of functions declared abstract in Shape trait
}

Information hiding and dependency degree

Degree of dependency between classes A and B

Conclusion

References

--

--

--

Computer science addicted.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Hacking Docker filesystems revisited

Service Worker Caching Strategies Based on Request Types

How to test Meta-Box marketplace on BSC testnet

My favorite extensions for web development 🤘 SB

My favorite extensions for web development

How to setup ELK Stack

Solving The Min Height Binary Search Tree in Python

[Analogy] Statistics — Alpha, Beta, and Power

Teamwork meme we shall all experience at some point in life

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Riccardo Cardin

Riccardo Cardin

Computer science addicted.

More from Medium

How not to hate your personal projects.

Five Qualities of Successful Remote Software Developers

Great People Do Great Things

How to choose the best programming language?