Member-only story

Best way to implement Strategy and Factory Design Patterns in Spring Boot

Daniel Zielinski
3 min readFeb 21, 2023
  1. Strategy Design Pattern: The Strategy pattern is a behavioral pattern that allows you to define a family of algorithms, encapsulate each one, and make them interchangeable. In other words, it defines a set of algorithms and allows them to be used interchangeably within an object, based on the need at runtime. This pattern is useful when you have a set of related algorithms, but you want to switch between them dynamically.
  2. Factory Design Pattern: The Factory pattern is a creational pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. In other words, it defines an interface for creating objects, but allows subclasses to decide which class to instantiate. This pattern is useful when you have a set of related classes, and you want to abstract the process of creating objects from the client.

Imagine the situation that you are a owner of many stores. Each store is managed by different worker. If you are logging in to the system as a payer you want to see all your stores, but when the worker is logging in to the system, worker only want to see stores that has access to.

Let’s solve that problem with strategy and factory design patterns. Check below example customer model.

--

--

Daniel Zielinski
Daniel Zielinski

No responses yet