Start course
4h 56m

This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.

Learning Objectives

  • Understand what design patterns are
  • Learn about the myriad of design patterns that you can use in your projects

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.


Basic knowledge of Java programming.



Hello, dear friends. In this video, we'll examine the strategy design pattern. 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 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 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 see in this UML, the strategy pattern has three participants. StrategyIF as 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 the strategy for sorting objects. I think it's time to create a project using the strategy design pattern. Let's create a new project and name it Strategy. For the strategy pattern, I think the most popular example is the calculator project. Let's make a little calculator with strategy design pattern. First, we need an interface as Strategy. Now, we will add a float calculation method in interface. This method will take two parameters. Now, let's start to define operation classes with Addition, implements Strategy. Override method. Go on with Subtraction. Override method again. Multiplication class. Last one will be Division. Done. Now, we will create a Context class that will ask from Strategy interface to execute the type of strategy.

Define private Strategy strategy; Define constructor with strategy parameter. Also, we need an execute method to make a calculation; return strategy.calculate (first, second). Done. Let's make some calculations. In main, first, we need two numbers that we will get from the user. Context context=new  Context(new Addition()). Execute. Now, change with context=new Context(new Subtraction() and execute again. Go on with context=new Context(new Multiplication((). Last one is context=new Context(new Division). 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 won't do this because my goal is to teach Java project implementation patterns. We have now finished the strategy design pattern. We'll continue with the state design pattern in the following video. See you in the next video.


About the Author
Learning Paths

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.

Covered Topics