Strategy Pattern #1/95

Strategy Pattern #1/95

Hello Reader,

Today I am starting to share my journey of learning. I will be creating a small brief of all the topics learned/ questions solved/ interesting stuff every day in a small blog post, and will share with everyone. I am aware of the fact, that I am not a good writer, so please let me know how to improve.

NOTE- All the content below, are my understanding of the topic from very specific sources, and I have described based on what I have understood, feel free to discuss, advise and share constructive criticism.

The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

When we have an entity that needs to have multiple types of behaviors for different types of instances but the types of behaviors are of a limited number and can be used for multiple instances, then we can Strategy Pattern to solve our problem.

For example-

If we have a Vehicle class which can give us instances of multiple types of vehicles such as Car, Bike, Bus, Truck, etc. , then some methods added in the Vehicle class can't be used by all its instances/child classes as they have different functionalities associated with them.

In our example, the movement type of every vehicle is different, like Car move swiftly than a bus, but a bike can move around corners where a car will not be able to go, so a move() function written in Vehicle class cant be extended and used in every other child class. Also, overriding the move() method in every other child class will cause a lot of code redundancy.

So, we create a MoveBehavior interface, which acts like a moveBehavior instance variable for every child class, now when we encounter any new move type, we create a new class which implements the MoveBehavior interface, and overrides the move() function with its appropriate implementation. Now we can use these classes as an object for our MoveBehavior variable in the main Vehicle class.

For example -

If we have three movement types - MoveLikeCar, MoveLikeBike, MoveLikeBus, then we create three classes which override the move() function with its appropriate implementation, and they all implement the MoveBehavior interface. So for every new car, bike or bus, we can use these class objects to fix our problem.

This is called Strategy Pattern, where we encapsulate the frequently(but not always) changing behavior types of any class, and use the encapsulated classes as methods when we extend the class and need to use either of the types of behavior encapsulated earlier.

We can instantiate the behaviors in the constructor or, use setters to change the behaviors on the fly.

As shown in the figure below, we know that each specific type of vehicle(car, bus, bike) has its own movement type(MoveBehavior), and service criteria( ServiceBehavior) so we have encapsulated these, and can use it whenever required.

No alt text provided for this image


To view or add a comment, sign in

Others also viewed

Explore topics