So me and my group is working on this real estate program (school assignment), we are troubled if we used misused Strategy pattern, and should use Facade instead. I am writing codes for CRUD of database, I used Strategy pattern to call those CRUD methods (add,delete,update) from the jform. We are still starting to learn different design patterns, so we would like to as help if this is a correct implementation.
This is my code for Strategy Pattern
public interface methods {
public void doOperation(int id, String type, int area, String address, String listingStatus);
}
public class methodUse {
private methods method;
public methodUse(methods method) {
this.method = method;
}
public void executeMethod(int id, String type, int area, String address, String listingStatus){
method.doOperation(id, type, area, address, listingStatus);
}
}
public class methodUpdate implements methods{
private SystemAdmin systemAdmin = new SystemAdmin();
@Override
public void doOperation(int id, String type, int area, String address, String listingStatus) {
systemAdmin.updatePropertyListing(id, listingStatus);
}
}
public class methodDelete implements methods{
private SystemAdmin systemAdmin = new SystemAdmin();
@Override
public void doOperation(int id, String type, int area, String address, String listingStatus) {
systemAdmin.deleteListing(id);
}
}
public class methodAdd implements methods{
private SystemAdmin systemAdmin = new SystemAdmin();
@Override
public void doOperation(int id, String type, int area, String address, String listingStatus) {
systemAdmin.addListing(id, type, area, address, listingStatus);
}
}
The classes are called on the jform every time the specific button is clicked, ex. add button executes strategy add.
I would like to ask you all if what we did was the right implementation of Strategy pattern.
Q: When should I use a "Strategy" pattern?
https://refactoring.guru/design-patterns/strategy
Use the Strategy pattern when you want to use different variants of an algorithm within an object and be able to switch from one algorithm to another during runtime.
So in this case, you're not "wrong" to choose a "Strategy" pattern
Q: [Is] what we did was the right implementation of Strategy pattern?
Declare the strategy interface common to all variants of the algorithm.
One by one, extract all algorithms into their own classes. They should all implement the strategy interface.
So yes, you have implemented it correctly.
Q: So what about "Facade"?
As John Bollinger correctly said:
The Strategy and Facade patterns are not alternatives to each other. It is plausible that a program might use both. Nor is there generally a single correct answer to how to design a program or which design patterns might be useful in it.