My scenario: I am creating an application that will allow me to create a mapping of redirect url’s that will point from the request url (the url in the old ecommerce solution) to the target url. There will be multiple implementations as we have multiple ecommerce solutions that were moving to our new one.
I am working in Laravel and have created a controller action that currently returns in JSON the redirect url’s for ONE of our current ecommerce solutions. The implementation of this method is going to differ greatly from one solution to the next.
I could be “dirty” and create an abstract class like storeAbstract and each store extends it and I just look at the store_type sent in to the controller and use the correct class.
This is how I have always done it but I am looking to expand my horizons and have deigned to find a design pattern that fits this situation. If anyone has any suggestions on which design pattern I should use and how I would implement it in laravel it would be greatly appreciated.
Example of how i would do it now:
Abstract class AbstractStore {
public function getRedirectUrls() {}
}
class StorefrontStore extends AbstractStore {
public function getRedirectUrls(){
// code to get the redirect urls
}
}
class <ecommstore>Store extends AbstractStore {
public function getRedirectUrls(){
// code to get the redirect urls
}
}
This just seems very basic and there has to be a better way.
This is a classic use case for an abstract factory.
You’d have a single interface and then separate concrete factories for each individual implementation. You’d then use factory methods to control the initialization of each set of factories.
2
You can use the strategy pattern to switch essential functionality. This seems to be more about behavior than about creating different kinds of subtypes of one supertype – so the behavioral strategy pattern seems more appropriate to me than the creational Abstract Factory pattern, though they can be combined: Strategy pattern for switching behavior, Abstract Factory pattern for creating the strategies… though you should consider whether such complexity is actually needed.
Using ‘Strategy’ You would have an abstract URLMappingStrategy
and concrete strategies assigned to the various e-commerce solutions under a common IURLMappingStrategy
-interface. The router then requires the interface and is composed with a concrete implementation (for the respective e-commerce platform), and dispatches calls to a mapURL
-method to the concrete strategy it was constructed with (stored by reference in a field of the Router)
1