Skip to main content


Showing posts with the label design pattern java

Design patterns - Strategy pattern with Java Example

What is Strategy Design pattern? Strategy design pattern comes under behavioural design pattern . It is also known as Policy pattern. It defines a family of algorithms and encapsulates each algorithm. The algorithm can be interchanged without changing much code. It bury algorithm implementation details in derived classes. It favours composition over inheritance. Derived classes could be implemented using the template design pattern (We will discuss about it later on). Implementation We will take example of Compressing files. For compressing files, we can either use zip or can use rar compression strategy. In Strategy Design pattern, we will have following classes and interface. 1. We will define our Strategy to compress files. package com . gauravbytes.strategy.client ; import ; import ; import java.util.List ; public interface CompressionStrategy { //Compression algorithm public void compressFiles ( List < F