I saw an example of factory method pattern and it had this code in the client.
if (config.OS == "Windows") then
dialog = new WindowsDialog()
else if (config.OS == "Web") then
dialog = new WebDialog()
This violates the OCP principle. If tomorrow you add more types/classes then you'll have to modify the code. I've been searching the net but haven't found a solution.
Why not make the Windows class and the WbClass both have a Dialog method, and instead of newing up the classes here you pass the object into the method?
@Tray2
I read the article. The problem i'm facing is having the customer decide at runtime what they want.
So, if there's an interface which Facebook and lInkedIn use. The customer decides at runtime where to share a specific post how do I do this without using if conditions. The only reason to avoid If/else is to keep OCP going.
Is this making sense or am I confused about the topic?
At some point you’re going to need a conditional depending on what option the user has selected, no matter what design pattern you employ or principles you try to follow.
in php you could also use the the config string to new up the class.
$config = "Windows";
$class = "\App\Dialogs\{$config}Dialog"; // namespace like Laravel [note the forum is removing the double backslash after Dialogs]
$dialog = new $class();
// do some catch for when you forgot to add a particular dialog to dialogs folder
// WindowsDialog
// WebDialog