Design Patterns

Sablier
47.4K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content
Next: UML basics

Object oriented programming

Fundament n°1: Encapsulation

An object is an hermetic capsule. It contains its own data and is responsible for their consistency. In this context, we abolish the global variables.

The goal is to avoid the separation of data and procedures: the procedures are responsible of data consistency. Then, the developer is just responsible for calling the good procedures.

Fundament n°2: Message

The objects communicate by "message passing". It is fundamentally different of a procedure or function call.

In a procedure call, the procedure name is transformed in a memory address. The link is final, and realised by the compiler (static binding).

In a message passing, the object who receives the message define the address of the code to execute. The link is dynamic, and realised at the execution time (dynamic binding). The executed code is a method of the object (it access the attributs).

Fundament n°3: Polymorphism

Different objects can answer to the same message in a different way. Previoussly, it was done by different procedures with different names.

It comes from fundament n°2: the code to execute in response to a message depends of the receiver, the link is realised at execution time (by method research). An object can be replaced by another one, without modifying the caller.

Fundament n°4: Inheritance

It makes it possible to avoid copying methods or attributs if the objects answer to the same messages the same way.

The concept of class is not a fundament: the prototype-based language (like JavaScript) do without.

The method research in class-based languages: the methods are defined in the class who was used to create the instance. If the method is not found, we search in the super-classes.

The method research in prototype-based language: the methods are carryed by the instances, created from other instances. If the method is not found, we search in the parent's prototype.

References and classes

Variables

A variable is a name, given to a reference to an object. It can be an attibute, a parameter, a temporary variable...

The variable is not the object. It reference an object, so two variables can reference the same object.

When we send a message using a variable, we send a message to the object referenced by the variable, we don't send a message to the variable.

Type, Interface

A class represents 3 things :

  • A description (the attributs and methods of his future instances)
  • A type (allows to type the variables and the return types)
  • A "factory" (a mould to make instances)

The concept of type allows to type variables and to controle the messages addressable via this variable.

An interface is a type (abstract) who express formally a set of methods' signatures. A class can then implements an interface (or many). It then have to implement all the corresponding methods.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content