What are the uses of Strategy Design patterns in Java?
Software design pattern:
The software design pattern is like a template to solve many different problems in different situations. It is also reused in solving the common problems which occur frequently. It is generally used by software engineers when designing applications to solve problems. The design pattern is grouped into three broad categories such as creational pattern, structural pattern and behavioural pattern. Let us see all these one by one.
Creational pattern:
A creational pattern is a type of design pattern which will create objects according to the situation. Creational patterns solve the design problem by controlling the object. There are five common parts of the creational pattern are:
- Abstract factory pattern- it is generally used to make multiple products without exposing the details of implementation.
- Builder pattern- it is used to provide an interface to create parts that ultimately make up a product.
- Factory method pattern -which allows a class to defer instantiation to subclasses.Prototype pattern- It creates objects referring to the existing objects by cloning. This pattern just declares the method to clone itself.
- Singleton pattern- It is generally used when you want to ensure that only once the object has been made available to the other classes.
Structural pattern:
The structural pattern is mostly about combining classes and objects to form a bigger structure or it can be said that it is just a design pattern to simplify the relationship between entities. A few examples of the structural pattern are
- Adapter pattern: It matches the interfaces of distinct classes. This makes thing work after they are made.
- Bridge pattern: This separates the object’s interface from getting implemented.
- Facade pattern: This is just a single class which represents whole subsystems.
Behavioural pattern:
The behavioural design pattern is a design pattern that recognises the common communication patterns between the objects and realises these patterns. Let us discuss a few types of behavioural patterns.
- Chain of responsibility: It allows more than one class to handle a request, independently of any other chain. Once the request is handled, it completes the whole chain.
- Command pattern: This pattern encapsulates a request as an object and lets you customise the client’s request with a queue or log request.
- Strategy design pattern: It is a pattern that is responsible for the change in the behaviour of the class by changing the algorithm at the run time without modifying the class.
There are few interesting uses of strategy design patterns. Let us discuss what it is.
What are the uses of strategy design patterns in java?
The interesting thing is it can be used in real-time.
- By using this pattern a client can choose to run any algorithm one wants. For example, software like Winzip has different algorithms to compress a file so this pattern allows the client to choose any algorithm at run time.
- Email service providers allow them to use various formats such as plain text or html. So, a client can choose anything.