Skip to main content

Posts

Showing posts with the label Strategy design pattern

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 java.io.File ; import java.io.FileNotFoundException ; import java.util.List ; public interface CompressionStrategy { //Compression algorithm public void compressFiles ( List < F