Strategy Design Pattern in Java

The strategy pattern is a behavioral design pattern that enables an algorithm’s behavior to be selected at runtime without causing tight coupling. The strategy pattern defines a family of algorithms, encapsulates each algorithm, and makes algorithms interchangeable. Strategy pattern lets the algorithm vary independently from clients that use it.

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Class Diagram

Example

Suppose we can have slightly different temperatures at different time(i.e., morning, noon, afternoon, evening etc.) in a day. So we do not know what would be the temperature at different time, until we reach to morning or evening, that is, run-time. This is the whole point of Strategy pattern.

Implementation

Define a interface DayTemperature, which has one method displayTemperature()