Java – Strategy Design Pattern

Strategy is a behavioral design pattern that allows changing algorithm behavior at runtime, based on actions such as user input or choices. Java example In this example we will use this pattern to switch between the states of a side scrolling platform character. Based on user inputs, if the Left Arrow keyboard button is pressed … Read more

Java – Template Design Pattern

The Template is a behavioral design pattern. It consists of a method that cannot be overridden which contains methods that represent the steps of an algorithm. These methods can and will be overridden (if necessary) in the subclasses. Java example For the example let’s consider the wizard that configures a game. A general approach asks … Read more

Java – Observer Design Pattern

The observer is a behavioural design pattern. It revolves around an object that is considered the subject of the observation and various other objects that are considered it’s observers. Java example We have a store that has a stock of items. Every time an action is performed such as adding or removing items from the … Read more

Java – Flyweight Design Pattern

The flyweight is a structural design pattern useful when you need to create lots of instances of a heavy-weight object. With the flyweight you can instead leverage parts of your code by either sharing the common resources that objects use or share the among resources the ‘heavy’ part of the object. Java example Let’s consider … Read more

Java – Factory Design Pattern

Factory method is a creational design pattern. It allows creation of objects from classes that share a common interface or base class. Usually, instead of specifying the class, strings are used to chose the correct instantiation. Java example Let’s pretend we have some ‘data’ and we can write it to disk either as binary, zipped … Read more

Java – Decorator Design Pattern

The decorator design pattern can be used to adhere to the single responsibility principle. When a new functionality needs to be added to a class, instead of modifying the class and adding the new code (adding more responsibility to it) you can use the decorator pattern. This works by taking the class that implements the … Read more