Greetings!
Strategy pattern is a behavioral pattern. It is one of the easiest patterns to learn and one of the most used pattern.
Source code for this blog post can be found in my github account. [source-code]
Let's see the GoF definition;
"Algorithm" in this definition is not necessarily a mathematical algorithm. It is something that do a thing. Basically algorithm is just another name for a strategy.
This leads us to;
When we start OOP, we are fond of inheritance. But when it comes to separating the code into more manageable way, we choose composition over inheritance.
We achieve this by extracting the volatile parts of our code and encapsulate them as objects.
We start by creating our FileService
Let's create the LocalFileService
We are going to use this in our StorgeService
All set. Let's run our application.
Remember;
Happy coding!
Strategy pattern is a behavioral pattern. It is one of the easiest patterns to learn and one of the most used pattern.
Source code for this blog post can be found in my github account. [source-code]
Let's see the GoF definition;
"Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it."
"Algorithm" in this definition is not necessarily a mathematical algorithm. It is something that do a thing. Basically algorithm is just another name for a strategy.
This leads us to;
- A class should be configured with an algorithm instead of implementing an algorithm directly.
- An algorithm should be selected an exchanged at run time.
When we start OOP, we are fond of inheritance. But when it comes to separating the code into more manageable way, we choose composition over inheritance.
"Consider 'has-a' instead of 'is-a'"
We achieve this by extracting the volatile parts of our code and encapsulate them as objects.
"Separate the parts of the code that will change the most."
Working Example
Let's take a practical example. We need to have file storage service which can handle save, retrieve operations. There, we can have a class and simply put the saving logic into that class. May be, from the begining it is not clear. Let's say we choose to save files in local storage. But what if we want to save files in AWS? It is clear that we should manage it in separate class. We can start with local file storage and easily swith to AWS.We start by creating our FileService
package com.slmanju.patterns;
public interface FileService {
void save();
void retrieve();
}
Let's create the LocalFileService
package com.slmanju.patterns;
public class LocalFileService implements FileService {
@Override
public void save() {
System.out.println("Save file in local disk.");
}
@Override
public void retrieve() {
System.out.println("Retrieving file from local disk.");
}
}
We are going to use this in our StorgeService
package com.slmanju.patterns;
public class StorageService {
private FileService fileService;
public StorageService(FileService fileService) {
this.fileService = fileService;
}
public void setFileService(FileService fileService) {
this.fileService = fileService;
}
public void save() {
fileService.save();
}
public void retrieve() {
fileService.retrieve();
}
}
All set. Let's run our application.
package com.slmanju.patterns;
public class App {
public static void main(String[] args) {
FileService localFileService = new LocalFileService();
StorageService storageService = new StorageService(localFileService);
storageService.save();
storageService.retrieve();
System.out.println("------");
FileService awsFileService = new AwsFileService();
storageService.setFileService(awsFileService);
storageService.save();
storageService.retrieve();
}
}
Remember;
- Encapsulate what varies.
- Favor composition over inheritance.
- Program to interface, not implementation.
Happy coding!
Awesome Blog...Thanks for sharing, Waiting for next update...
ReplyDeleteHibernate Training in Chennai
Spring Hibernate Training
Spring and Hibernate Training
Hibernate Training in OMR
hibernate training in Porur
Spring Training in Chennai
clinical sas training in chennai
DOT NET Training in Chennai
QTP Training in Chennai
LoadRunner Training in Chennai
Wonderful Post!!! Thanks for sharing this great blog with us.
ReplyDeleteAndroid Training in Chennai
android training center in chennai
android classes in chennai
Android Course in Chennai
Android training in Guindy
Android Training in Tambaram
Python Training in Chennai
Big data training in chennai
SEO training in chennai
JAVA Training in Chennai
This blog shares some useful information. keep sharing this blog.
ReplyDeleteAviation Academy in Chennai
Aviation Courses in Chennai
Best Aviation Academy in Chennai
Air Hostess Training in Chennai
Air Hostess Training Institute in Chennai
Air Hostess Academy in Chennai
Air Hostess Course in Chennai
Airport Management Courses in Chennai
Airport Management Training in Chennai
Airline Courses in Chennai
Airport Ground Staff Training Courses in Chennai
Airport Ground Staff Training in Chennai
Ground Staff Training in Chennai
Thanks for giving excellent Message.Waiting for next article
ReplyDeleteHtml5 Training in Chennai
Html training in chennai
html course in chennai
html5 training in vadapalani
Html5 Training in Velachery
DOT NET Training in Chennai
core java training in chennai
Hibernate Training in Chennai
Mobile Testing Training in Chennai
SAS Training in Chennai
Wonderful Blog.... Thanks for sharing with us...
ReplyDeleteHadoop Training in Chennai
Big data training in chennai
Best Hadoop Training in Chennai
Big Data Course in Chennai
Big data training in Guindy
Hadoop Training in Tambaram
Python Training in Chennai
SEO training in chennai
JAVA Training in Chennai
Selenium Training in Chennai
ReplyDeleteThis blog gives more useful information, I enjoyed to read this blog.
DevOps Training in Bangalore
Best DevOps Training in Bangalore
AWS Training in Bangalore
Data Science Courses in Bangalore
DevOps Course in Bangalore
DevOps Training Bangalore
rpa training in bangalore
Python Training in Bangalore
PHP Training in bangalore
spoken english classes in bangalore
Thanks for your nice post, i am interested to learn online freelancing, but firstly i have to learn computer , could you suggest me please which computer training center best.
ReplyDeleteDot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery
Great Article Image Processing Projects
ReplyDeleteDeep Learning Projects for Final Year
JavaScript Training in Chennai
JavaScript Training in Chennai
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training