cplusplus.co.il

Archive for the ‘design’ Category

The State design pattern is a very useful design pattern. In this article we will exploit it to provide a very slick and elegant implementation of a Finite State Machine (FSM).

First of all, a FSM consists of a finitie number of states and a predefined set of rules defining the transitions between all these states. Each state can either ACCEPT or REJECT the input – the FSM accepts the input IFF it ends up on an accepting state at the end of execution. In our design, each state will be modeled by a distinct class derived from an abstract base State class. Each state class will also contain all of its relevant transition logic. This architecture will enable us to provide a very flexibe, powerful, yet intuitive and simple, implementation of any FSM.

Read the rest of this entry »

When we approach the process of designing our software, there are many issues we must address in order to be succesful. One of these issues (and you are free to disagree) is: How will we be able to test (or debug) our hand crafted piece of code?

This post will attempt to at least provide one way of writing code that is a little easier to debug and test, by making use of the factory design pattern.

Read the rest of this entry »

A final (or frozen) class is a class that can’t be further extended by inheritance. In many languages we have different keywords for such behaviour, such as “final” in Java, or “sealed” in c#. Unfortunetly we don’t have such construct in c++; but we can use other mechanisms to easily achieve this goal.

Read the rest of this entry »

Sometimes you would like to create an abstract class, but there is no method you could naturally declare as pure virtual (=0) in order to achieve that (in a tag interface, for example).

In such cases the destructor may be the perfect candidate: as it should always be declared virtual if inheritance is considered, it may as well be pure virtual.

But then we have a hazard on our hands – since destructor (and constructor) invocation in inheriting classes is recursive, we will end up with a pure virtual method call – with no suitable code to execute. How do we solve this issue?

Read the rest of this entry »


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 27 other subscribers

Categories