Design Patterns

All about the internals and the implementation of design patterns in programming.

Strategy Pattern – Swift

Strategy Pattern – Swift

Reading Time: 3 minutes

This time we will try to create an example of the Strategy Pattern. Strategy Pattern is one of the 23 classic OOP design patterns defined inĀ Design Patterns: Elements of Reusable Object-Oriented Software. This pattern is used usually to alter between few “strategies” and defers this decision to be made in run-time. Usually a good example for Strategy Pattern in use is for hosing between algorithms based on client’s decision. Algorithms could be different sort algorithms, validation algorithms, etc.

Example – Validation

This example written in Swift language is written to demonstrate how easy is to choose a strategy, in compile time but even better, in run-time. We will create validation strategies to make it easier to choose between validation algorithms on strings input.


Lets describe in a short how would we use the strategy pattern, easiest way is to follow this diagram:

In short, each part is responsible for:


  • Holds the actual ConcreteStrategy object, defined by the client, but exposes only the Strategy class API.
  • Can instantiate a default Strategy.
  • Can expose its data to the concrete strategies (not in this example)


  • Defines the interface of the strategies, or in our example is just swift protocol.


  • Implements the actual algorithm


After I read again my example I realize that a better example would not be to choose between 2 types of validation (Email, Password) but between 2 algorithms of the same validation type. But you can see here how robust this design pattern is, you can use it however your problem domain forces you. (I will fix it soon)

If you want to watch for more design patterns examples I wrote, please visit here.


Design Patterns Series

Design Patterns Series

Reading Time: 3 minutes

Design Patterns are the set of tools a professional developer and software engineer should have in his head when tackling a problem. They provide the ad hoc and abstract solutions and guide lines out of the box to the most common problems in the software engineering life cycle. During my studies we have talked about some of them and used them here and there, but we never really dived deep into them. I decided that I have to cover these holes that I have about some of them and try to make small examples and implementation of them in various languages. The book that I use and learn from tis probably the most known software engineering books – Design Patterns: Elements of Reusable Object-Oriented Software. Do your self a favor, buy it and read it.

The Design Patters Series will show how I solve problems I had during my few years of experience as a developer. Most of the examples will be written in statically typed languages (Swift, Java). That’s because those languages provide concepts such as interfaces and protocols which make the code more declarable and help find bugs during compile time. I would try my best to provide examples also with dynamically typed languages (Python, JavaScript).

List of examples I made: