image
Implementing Strategy Pattern
Start course
Difficulty
Beginner
Duration
53m
Students
144
Ratings
3/5
Description

In this course, we will learn the concepts of microservice and spring framework with a focus on Domain-Driven Design.

Learning Objectives

  • Domain-Driven Design

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud

Prerequisites

  • Basic Java knowledge
Transcript

Hello there, in this video, we will examine the Strategy Pattern. Let's begin. We talked about strategy pattern in the design pattern section. First, let's recap briefly here and then look at how to implement the strategy pattern. In computer programming, the strategy pattern, also known as the policy pattern, is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives runtime instructions as to which in a family the algorithms to use. Strategy lets the algorithm vary independently from the clients that use it. That popularized the concept of using design patterns to describe how to design flexible and reusable object-oriented software. Deferring the decision about which algorithm to use until runtime allows the calling code to be more flexible and reusable. Typically, the strategy pattern stores a reference to some code in a data structure and retrieves it.

This can be achieved by mechanisms such as the native function pointer, the first-class function, class or class instances in object-oriented programming languages, or accessing the language implementation's internal storage of code via reflection. We define multiple algorithms and let client applications pass the algorithm to be used as a parameter. In the strategy pattern, we create objects that represent various strategies and a context object whose behavior varies as per its strategy object. The strategy object changes the executing algorithm of the context object. As you see in this UML, the strategy pattern has three participants. Strategy is an interface defining the common operation we intend to perform. Concrete strategy classes are the implementation classes that use different algorithms to carry out the operation defined in the strategy interface.

Context is anything that requires changing behaviors and holds a reference to a strategy. One popular example of the strategy pattern in JDK is the usage of java.util.Comparator in the collections.sort() method. We can think of the collections.sort() method as the context and the java.util.Comparator instance that we pass in as the strategy for sorting objects. Now, let's look at the implementation of the strategy pattern. We said that the strategy pattern allows us to change the behavior of an algorithm at runtime. Typically, we start with an interface to apply an algorithm and then implement it multiple times for each possible algorithm. For example, let's say we have a requirement to apply different types of discounts to purchases based on whether it's Christmas, Easter, or New Year. First, we need to create a discounter interface which will be implemented by each of our strategies like this. After that, let's say we want to apply a 50% discount at Easter and a 10% discount at Christmas.

Let's implement our interface for each of these strategies like that. Now, we will create a context class that will ask the discounter interface to execute the type of strategy like this. In this class, we create an instance of the discounter interface and define a constructer with a discounter parameter, and also, we need an apply method to apply the discount in this context class. After that, we can test our code like this. So, as you can see, we can apply the strategy pattern to a small project this way. If you remember, we learned this pattern practically in the design pattern section. I think it's a little more reinforced with this example. So, let's take a short break here, and I'll see you in the next video.

 

About the Author
Students
3940
Courses
64
Learning Paths
5

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.