-2

I am working on one project which redirects user to custom page after login, registration etc. FYI, the custom page can be configured as per customer from admin settings.

I am trying to follow some design-pattern guidelines to achieve this. So far I have designed my classes as below

File: RedirectInterface.php

interface RedirectInterface
{
    public function getUrl($customerId = null);
}

File: LoginRedirect.php

class LoginRedirect implements RedirectInterface
{
    public function getUrl($customerId = null)
    {
        // do some business logic here to get url
        $url = '/account/some-customer';
        return $url;
    }
}

File: RegisterRedirect.php

class RegisterRedirect implements RedirectInterface
{
    public function getUrl($customerId = null)
    {
        // do some business logic here to get url
        $url = '/welcome/some-customer';
        return $url;
    }
}

File: RedirectFactory - Creational design pattern (static factory)

class RedirectFactory
{
    public static function build($type, $customerId)
    {
        if ($type == 'login') {
            return new LoginRedirect($customerId);
        } else if ($type == 'register') {
            return new RegisterRedirect($customerId);
        }
        throw new InvalidArgumentException ('Invalid redirect type.');
    }
}

Usage:

// 1. Usage: Somewhere in customer registration code
$redirectUrl = RedirectFactory::build('register', 102)->getUrl();
header('Location: ' . $redirectUrl);

// 2. Usage: Somewhere in customer login code
$redirectUrl = RedirectFactory::build('login', 102)->getUrl();
header('Location: ' . $redirectUrl);

So my question here is:

  • What is the appropriate design pattern used in this case? like Adapter, Static Factory etc.
  • How would you refactor this sample with more clincial finish?

Thanks

MagePsycho
  • 97
  • 3
  • 4
    Possible duplicate of [Choosing the right Design Pattern](https://softwareengineering.stackexchange.com/questions/227868/choosing-the-right-design-pattern) – gnat Apr 18 '17 at 10:29
  • 1
    @gnat It's not a generic question. I have even given the full example of my case. So this cannot be considered the duplicate in my opinion. – MagePsycho Apr 18 '17 at 10:31
  • 3
    @MagePsycho: The linked duplicate is a generic question, that explains generically why *all* questions of this nature, *including* yours don't make sense. A design pattern is *not* something that you choose before your design. It is something you *discover* in your design, *after* you have designed it. – Jörg W Mittag Apr 18 '17 at 11:04
  • @JörgWMittag where do you think this type of question should to be posted? – MagePsycho Apr 18 '17 at 12:03
  • 1
    What makes you think that *any* pattern is appropriate here? – Ben Aaronson Apr 18 '17 at 13:11
  • @BenAaronson What makes you think that there is no need of any pattern in this kind of task?. To answer yours - You have to create an object (Factory Pattern) and package different redirects into separate classes (S - Separation of Concerns of SOLID) etc. – MagePsycho Apr 18 '17 at 13:27
  • @JörgWMittag: it seems the OP has already shown a design where the strategy pattern can be discovered. – Doc Brown Apr 18 '17 at 14:58
  • 1
    Asking "what design pattern should I use to solve this problem?" is a bit like asking "which power tool should I use to accomplish this task?". Some tasks, perhaps most tasks, just don't call for power tools. Hint: instead of asking "what is the appropriate design pattern", just ask "what is the appropriate *design*" or "what is the appropriate *thing to do*". – Tanner Swett Apr 19 '17 at 12:53
  • @TannerSwett I think static factory becomes obsolete in the case of Dependency Injection. – MagePsycho Apr 19 '17 at 13:36

1 Answers1

1

What is the appropriate design pattern used in this case?

If you mean which design pattern you picked, the answer is, this is the "strategy pattern" (with different "redirect strategies"). If it is appropriate for your case in your context, only you can tell us.

How would you refactor this sample with more clincial finish?

Why should one refactor this? The code snippet looks pretty SOLID to me, since it is "strategy pattern by the book". However, I see only your snippet, not the whole program, and I do not now which kind of evolvability you really need. Thus if this solution it is a good choice in your context, only you can tell.

For example, the current form puts the logic for finding the redirect URL, or the adding of new redirect URLs into the hands of the developer. If that is what is required, then it is ok. If not and you have the requirement to let someone else change that logic by configuration, then you probably need to refactor (or start directly with a different solution). But do not refactor just for the sake of refactoring, otherwise you end up with something like FizzBuzz Enterprise.

Doc Brown
  • 199,015
  • 33
  • 367
  • 565
  • In my case, I will have many types or redirect classes like GeoipRedirect, BrowswerLangRedirect etc. Not sure if the Strategy pattern static factory would be appropriate for this. – MagePsycho Apr 18 '17 at 14:13
  • @MagePsycho: the number of redirection strategies is not so much important than the question how, by whom and how often these strategies need to be extended or changed, – Doc Brown Apr 18 '17 at 14:57