WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Design Patterns Β· Basic Β· question 8 of 100

What is the difference between the Strategy pattern and the State pattern?

πŸ“• Buy this interview preparation book: 100 Design Patterns questions & answers β€” PDF + EPUB for $5

Both the Strategy pattern and the State pattern are part of the behavioral design patterns in software design. They both involve dynamically changing the behavior of an object at runtime depending on its internal state.

However, the main difference lies in their intent and implementation.

The Strategy pattern is used to define a family of algorithms, encapsulate each one of them, and make them interchangeable. It allows the algorithm to vary independently from the client that uses it. The pattern consists of four main components:

- Context: the object that needs to change its behavior dynamically.

- Strategy: an interface or abstract class that defines a set of algorithms or behaviors to implement.

- Concrete strategies: a set of concrete classes that implement the strategy or algorithm defined by the Strategy interface.

- Client: the object that creates a Context object and sets a specific strategy for it.

Here is an example of the Strategy pattern in Java:

public interface CompressionStrategy {
   public void compressFile(String fileName);
}

public class ZipCompressionStrategy implements CompressionStrategy {
   public void compressFile(String fileName) {
      System.out.println(fileName + " compressed using zip compression");
   }
}

public class RarCompressionStrategy implements CompressionStrategy {
   public void compressFile(String fileName) {
      System.out.println(fileName + " compressed using rar compression");
   }
}

public class CompressionContext {
   private CompressionStrategy strategy;
   
   public void setCompressionStrategy(CompressionStrategy strategy) {
      this.strategy = strategy;
   }
   
   public void createArchive(String fileName) {
      strategy.compressFile(fileName);
   }
}

public class Client {
   public static void main(String args[]) {
      CompressionContext ctx = new CompressionContext();
      ctx.setCompressionStrategy(new ZipCompressionStrategy());
      ctx.createArchive("sample.txt");
      ctx.setCompressionStrategy(new RarCompressionStrategy());
      ctx.createArchive("sample.txt");
   }
}

In this example, we have a Context class called CompressionContext, which takes a CompressionStrategy object and uses it to compress files. We have two Concrete strategies: ZipCompressionStrategy and RarCompressionStrategy. The Client creates a CompressionContext object and sets a specific CompressionStrategy for it.

On the other hand, the State pattern is used when an object needs to change its behavior based on its internal state. It allows an object to alter its behavior when its internal state changes. The pattern consists of four main components:

- Context: the object that has an internal state and whose behavior changes based on that state.

- State: an interface or abstract class that defines a set of behaviors for a Context object in a particular state.

- Concrete states: a set of concrete classes that implement the State interface, each with a specific behavior for a particular state.

- Client: the object that creates a Context object and changes its state as necessary.

Here is an example of the State pattern in Java:

public interface State {
   public void doAction(Context context);
}

public class StartState implements State {
   public void doAction(Context context) {
      System.out.println("Player is in start state");
      context.setState(this);
   }
   
   public String toString(){
      return "Start State";
   }
}

public class StopState implements State {
   public void doAction(Context context) {
      System.out.println("Player is in stop state");
      context.setState(this);
   }
   
   public String toString(){
      return "Stop State";
   }
}

public class Context {
   private State state;

   public Context(){
      state = null;
   }

   public void setState(State state){
      this.state = state;     
   }

   public State getState(){
      return state;
   }
}

public class Client {
   public static void main(String[] args) {
      Context context = new Context();

      StartState startState = new StartState();
      startState.doAction(context);

      System.out.println(context.getState().toString());

      StopState stopState = new StopState();
      stopState.doAction(context);

      System.out.println(context.getState().toString());
   }
}

In this example, we have a Context class that has an internal state, initially set to null. We have two Concrete states: StartState and StopState, each with a specific behavior for the Context object in that state. The Client creates a Context object and sets a specific State for it.

In summary, the Strategy pattern is used to define a family of interchangeable algorithms, while the State pattern is used to change the behavior of an object based on its internal state.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Design Patterns interview β€” then scores it.
πŸ“ž Practice Design Patterns β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Design Patterns questions & answers β€” PDF + EPUB for $5

All 100 Design Patterns questions Β· All topics