15
Lesson 23 of 44

Advanced OOP and patterns

As programs grow, classes need contracts and structure. In this lesson you will see properties with validation, abstract classes, special methods so your objects behave like containers and functions, and the most useful design patterns in Python: Strategy, Factory, Observer, State and Command. You will learn to share responsibilities and to add behavior without rewriting what already works.

  • 5 theory articles
  • 10 practice exercises
Start lesson

Fundamentals concepts 5

Properties and encapsulation

In Python attributes are public by convention: there is no private . A leading underscore ( _balance ) warns that something is…

Theory
Read more

Abstract classes and contracts

An abstract class defines *what* its subclasses must be able to do without saying *how*. It is created by inheriting from abc.ABC…

Theory
Read more

Strategy and Factory patterns

A design pattern is a known solution to a recurring problem. In Python many of them are much shorter than in other languages…

Theory
Read more

Observer and State patterns

An object publishes events and others subscribe to react, without the emitter knowing who they are. It is the basis of the event…

Theory
Read more

Callable objects, containers and Command

Special methods (those that start and end with double underscores) let your classes integrate with the language syntax: len(x) …

Theory
Read more

Practice exercises 10

Temperature with properties

A property ( @property ) looks like an attribute but runs code when read or assigned: it is used to validate and to expose…

Exercise Medium
Solve it

Rectangle with computed properties

Derived data (area, perimeter…) is not stored: it is computed when requested through a read-only property, so it never gets out…

Exercise Medium
Solve it

Shapes with an abstract class

An abstract class ( abc.ABC with @abstractmethod ) defines a contract: it forces subclasses to implement certain methods and…

Exercise Medium
Solve it

Discount strategies

The Strategy pattern separates “which algorithm I use” from “where I use it”: in Python a dictionary that maps a name to a…

Exercise Medium
Solve it

Factory with a class registry

A factory creates objects from a name. With a registry (a dictionary) and a decorator, each class registers itself, and the…

Exercise Medium
Solve it

Event system (Observer)

In the Observer pattern some objects ( subscribers ) register to be notified when another one (the emitter ) publishes an event…

Exercise Hard
Solve it

A polynomial called like a function

With __call__ an object can be called as if it were a function, and at the same time store data. It is useful for functions with…

Exercise Medium
Solve it

Sparse matrix with magic methods

The special methods __getitem__ , __setitem__ , __len__ , __contains__ and __iter__ make your class behave like a native…

Exercise Hard
Solve it

States of an order

The State pattern represents each state as a class that knows which other states can be reached. That way the valid transitions…

Exercise Hard
Solve it

Editor with undo (Command)

In the Command pattern each action is an object with a method to execute it and another to undo it. By storing the executed…

Exercise Hard
Solve it