C++ - Design pattern - Memento

The Memento design pattern allows an object to retrieve last states of another object. This is used for example in the undo mechanism. So it’s a very interesting pattern that doesn’t use any interface. But instead it uses an opaque pointer in order to store old states of the main object. Let’s see that in this Memento design pattern tutorial. First of all GitHub: https://github.com/badprog/badprog-design-pattern-memento This pattern may seem obscur at first glance and maybe you have some questions such as: Why do we have to use three classes to save states of an object? ...

June 6, 2018

C++ - Design pattern - Strategy

The Strategy design pattern allows to change behaviour of a program depending on a context. It means that a single class can manage many others without knowing that those classes even exist. Interesting isn’t it? Let’s see that in this Strategy design pattern tutorial. First of all GitHub: https://github.com/badprog/badprog-design-pattern-strategy In this example we are going to use a Context as a manager, an IStrategy as interface, Strategy1 and Strategy2 as dashboards. ...

May 13, 2018

C++ - Design pattern - Proxy

The Proxy design pattern is in charge of managing another class. It’s like a firewall that you cannot overstep. There are several reasons to use a Proxy instead of directly another class. For example it can check if a client accessing an object has permission to do that or it can substitute to this object until someone really needs it (in order to avoid useless memory loading). So it’s a more complex object than the real one. ...

May 10, 2018

C++ - Design pattern - Mediator

The Mediator design pattern allows to unlink objects from each other. It’s like a manager that centralizes every action performed by a component in order to dispatch these events to all other objects that need to know information. It could be seen as a design pattern quite accessible in term of complexity. Let’s try it in an example. First of all GitHub: https://github.com/badprog/badprog-design-pattern-mediator What’s interesting in this design pattern is that each Ship doesn’t know who are other Ships. ...

May 8, 2018

C++ - Standard library - move

The move template function allows to change an object owner. We’re not going to dive in the heart of the move semantics. Instead we’re going to see a really easy example of how it works. Let’s get started. First of all In this move tutorial we’re going to create 3 Ships, check their memory address then pass them to a Manager in order to add them to the Manager list. ...

April 29, 2018

C++ - Design pattern - Decorator

The Decorator design pattern allows an object to be decorated in different ways not by inheriting from another class but by adding features directly to this object. It’s main interest is to avoid multiplication of classes that would be almost the same as the main one. This design pattern has an interesting architecture even if not so easy to understand at first. Let’s see how this Decorator design pattern works. ...

April 11, 2018

C++ - Design pattern - Prototype

The Prototype design pattern is used in order to clone an object. Main goal to prototype design pattern is to create new objects with default values. This cloning operation is actually a copy of an object and can be made with a shallow or a deep copy. Let’s see that in this Prototype design pattern tutorial. First of all Find the complete code on GitHub: https://github.com/badprog/badprog-design-pattern-prototype There are two different ways to copy an object: shallow and deep. ...

April 5, 2018

C++ - Design pattern - Factory method

The Factory method design pattern is a really helpful one and a great dive into the world of design patterns. In our example we are going to use an interface and some concrete classes. If by reading this you are still interested in the concept, let’s see the Factory method design pattern in this tutorial. First of all GitHub: https://github.com/badprog/badprog-design-pattern-singleton-factory-method In the following tutorial we are going to use an interface class called IShip that three other classes will inherit: ShipTiny, ShipNormal and ShipEnormous. ...

March 4, 2018

C++ - Design pattern - Singleton

This Singleton design pattern is certainly the most known and maybe the most easiest to understand. Its goal is to guarantee that an object will be created only once through all the program. So this object has to be unique, can’t even be created twice nor copied and it must be accessible through the entire application. This is for example the case when you have a logging class that must be accessible by almost every class in your program. ...

February 14, 2018

C++ - Design pattern - Observer

The Observer design pattern allows an object (often called Subject) to inform other objects (Observers) that an event has occurred. It’s particularly useful when you have data from a Subject but you don’t know how this data will be used by the Observers. For example, data from a Subject can be used to display a text in an Observer and a chart in another Observer (even if data is the same). ...

January 31, 2018