The course is part of this learning path
In this course, we will learn the concepts of microservice and spring framework with a focus on design patterns.
- Learn about various design patterns
- 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
- Basic Java knowledge
Hello, dear friends. In this video, we will examine the Strategy Design Pattern. So, let's begin. 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 of algorithms to use. Strategy lets the algorithm vary independently from 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, classes or class instances and object-oriented programming languages, or accessing the language implementations 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 which 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 can see in this UML, the Strategy pattern has three participants. Strategy is an interface defining the common operation we intend to perform. ConcreteStrategy 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 Collections.sort method to be the context and the java.util.Comparator instance that we pass in as the strategy for sorting objects.
I think it's time to create a project using the Strategy Design Pattern. So, let's create a new project and name it Strategy. For the Strategy pattern, I think the most popular example is the calculator project. So, let's make a little calculator with the Strategy Design Pattern. First, we need an interface as strategy. Now, we will add a float calculation method in the interface. This method will take two parameters. Now, let's start to define the operation classes with Addition. Implement Strategy override method. Go on with Subtraction; override method again. Multiplication class. Last one will be Division. And done. Now, we will create a context class that will ask from strategy interface to execute the type of strategy. Define strategy, define constructor with strategy parameter. Also, we need an execute method to make a calculation, return calculation. Done. Let's make some calculations. In main, first, we need two numbers that we will get them from user. Now, create a context instance with Addition; execute.
Now, change with Subtraction and execute again. Go on with Multiplication. Last one is Division. And done. Let's run and try. First number is 20. And look, we can see the results of our calculations. We now have a little calculator created using the Strategy pattern. If you want, you can take the operator from the user and with conditions, you can make a real calculator. But I'm not going to take the time to do this right now because my goal is to teach Java project implementation patterns. So, you guys can go have that fun on your own time. We have now finished the Strategy Design Pattern. We'll continue with a State Design Pattern in the following video. So, I'll see you there my friends.
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.