Advanced Object-Oriented Programming

Cyriel Mallart
Ludovic Deneuville
Rémi Pépin

Outline

  1. Advanced OOP Concepts
    • Recap
    • Abstract Classes
    • Bridge Pattern
  2. Software Engineering
    • Definition
    • Single Responsibility Principle
    • Design Patterns

A Few Reminders

The three principles of object-oriented programming:

  • Encapsulation: an object contains attributes and methods.
  • Inheritance: an object can inherit attributes and methods from another class to redefine them. It can also add other attributes/methods.
  • Polymorphism: a method can be associated with different code depending on the parameters passed or the object it belongs to.

An Example to Illustrate

Automatic data processing application:

  • Multiple data sources: surveys, web scraping, administrative files, etc.
  • Multiple data formats: csv, xml, json, etc. (we’ll come back to this)
  • Multiple processing algorithms: exploratory statistics, regression, “machine learning,” etc.

Example of a Class Diagram

An Example with Inheritance

Abstract Classes

Abstract Class: a class whose implementation is not complete and which is not instantiable.

Allows passing a contract. Child classes will have to implement what is missing.

Advantages:

  • We know what child classes must do 👍
  • We can generate code 🙏
  • Limits the risk of error!! 👌

For Example

And in Python?

  • No native support for abstract classes 😱
  • Abstract Base Classes (abc) module to solve the problem 🦾
  • Already included in your Python distribution 😌
    • Step 1: Import the module 🧳
    • Step 2: Inherit from ABC 👨‍👩‍👧‍👦
    • Step 3: Define abstract methods 📝
    • Step 4: ???
    • Step 5: Profit 💰💰

What If We Added Data Formats?

Currently, 3 data formats in our application:

  • CSV: Comma Separated Values (tabular)
  • XML: eXtended Markup Language (tag format)
  • JSON: JavaScript Object Notation (key-value format)

For Example

Do you see a problem?

The Power of OOP

  • Currently 4 * 3 “concrete” classes to define 😱
  • Reading the format is dependent on the source 😵

BUT

  • We can externalize this processing! 😌
  • Aggregation relationship 🤯

Le bridge pattern

Work Smart, Not Hard

  • Composition + inheritance: 9 classes 😎
  • Inheritance: 17 classes 😫
  • We can easily add types and formats 🥳

Pattern Bridge

Découpage d’une grosse classe en un groupe de petites classes avec leur propre hiérarchie qu’il faut ensuite assembler.

In Summary

  • Use the power of OOP 💣
  • Prefer specific objects (inheritance) over if/elif/else 🐱‍🏍
  • Abstract classes are blueprints for future classes 👷‍♀️👷‍♂️
  • OOP allows creating more readable, scalable, and maintainable code 👑

Software Engineering

What is Software Engineering?

  • Observation: coding blindly does not result in a quality application.
  • But stacking bricks blindly does not result in a house, even if you have a plan.
  • Need to plan, document, test…

Why It’s Important: Parallel with Building a House

  • You have the construction plan for a house (provided by the architect)
  • But implementing this plan requires technical knowledge
  • Need to redraw diagrams for specific areas (arches, stairs, etc.)
  • This is not wasted time!

Writing quality code is like doing precision craftsmanship; it requires tools, experience, and methods.

Some Basic Principles

  • Decompose a program into simple coherent modules
  • Modules expose methods that can be used/overridden by other modules but remain protected from unintended modifications
  • Each module should be a black box for others
  • If we keep the same inputs/outputs, we can change a module without risk
  • Prefer abstractions + inheritance over if/elif/else

A Mantra

Important

Low coupling, high cohesion

  • Low inter-class coupling: modifying one class should impact others as little as possible.
  • High intra-class cohesion: each class should be a coherent set of attributes and methods.

Why Respect Low Coupling, High Cohesion?

  • Teamwork 🦸‍♀️🧙‍♂️👨‍💼👩‍🔬
  • Code readability 📖
  • Debugging 🐞

Limit the risk of errors when modifying code (avoid spaghetti code) 🍝

Revisiting the Bridge Pattern

  • The “Source” part handles processing related to the source.
  • The “File” part handles file reading.
  • Only inputs/outputs matter.
  • We can add a “Processing” part for additional processing.
  • No unnecessary if/elif/else. Each part of our code handles only one thing

Chaque partie de notre code s’occupe d’une seule chose

Design Patterns: Definition

“In computer science, and more specifically in software development, a design pattern is a characteristic arrangement of modules, recognized as a best practice in response to a software design problem. It describes a standard solution, usable in the design of different software.” — Source

Design Patterns: In a Nutshell

  • Best practices
  • Standard solutions to design problems
  • Robust solutions
  • Independent of technology
  • Independent of the business

Is a tool that is there to help you

Design Patterns: Example

Recurring Problem:

  • Creating complex objects that are a composition of independent characteristics
  • In other words: decouple abstraction from its implementation so they can vary independently

In Summary

  • Creating a complex application requires complex code 🧩
  • Without a design phase, we’re heading for trouble 🧱
  • There are ready-made solutions to common problems 🧰

Important

Faible couplage, forte cohérence