4

I am a beginner in design patterns.

I am trying to use Abstract Factory - pattern while maintaining Open-Closed Principle.

Plz look at the following code:

public interface IAbstractFormFactory
    {
        void ShowOSName();        
    }

public class VistaForm : IAbstractFormFactory
    {
        public void ShowOSName()
        {
            Console.WriteLine("Vista");
        }
    }

public class WinXpForm : IAbstractFormFactory
    {
        public void ShowOSName()
        {
            Console.WriteLine("Win XP");
        }
    }

public static class Application
    {
        public static void Run(IAbstractFormFactory factory)
        {
            factory.ShowOSName();
        }
    }

public class Program 
    {
        public static void Main() 
        {
            IAbstractFormFactory form;

            int sys = 0;

            if (sys == 0) 
            {
                form = new WinXpForm();
            } 
            else 
            {
                form = new VistaForm();
            }

            Application.Run(form);

            Console.ReadLine();
        }
    }

can it be an example of Abstract Factory Pattern?

If yes, how can I refactor it incorporating the concept of Open-Closed Principle?

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
user366312
  • 16,949
  • 65
  • 235
  • 452

4 Answers4

6

The example you've given isn't an abstract factory. Abstract factories have factory methods (i.e. methods that create and return objects).

As for the open/closed principle, the abstract factory pattern inherently facilitates this. The code is "closed" in that it doesn't have to be modified if you add a new factory (because you're using dependency injection), and it's "open" in that you can extend the functionality by writing a new abstract factory.

UPDATE: Here is the example code in the question modified to show an abstract factory:

public interface IForm
{
    void ShowFormName();
}

public interface IAbstractFormFactory
{
    IForm MakeForm();        
}

public class VistaForm : IForm
{
    public void ShowFormName()
    {
        Console.WriteLine("Vista Form");
    }
}

public class VistaFormFactory : IAbstractFormFactory
{
    public IForm MakeForm()
    {
        return new VistaForm();
    }
}

public class WinXpForm : IForm
{
    public void ShowFormName()
    {
        Console.WriteLine("WinXP Form");
    }
}

public class WinXpFormFactory : IAbstractFormFactory
{
    public IForm MakeForm()
    {
        return new WinXpForm();
    }
}

public static class Application
{
    public static void Run(IAbstractFormFactory factory)
    {
        IForm form = factory.MakeForm();
        form.ShowFormName();
    }
}

public class Program 
{
    public static void Main() 
    {
        IAbstractFormFactory factory;

        int sys = 0;

        if (sys == 0) 
        {
            factory = new WinXpFormFactory();
        } 
        else 
        {
            factory = new VistaFormFactory();
        }

        Application.Run(factory);

        Console.ReadLine();
    }
}
Tom Dalling
  • 23,305
  • 6
  • 62
  • 80
  • http://stackoverflow.com/questions/1001767/design-patterns-factory-vs-abstract-factory Are you sure? –  Jul 10 '09 at 09:12
  • You are using interfaces/polymorphism/dependency injection correctly, but the factories aren't creating anything, so they aren't really factories. I'll add some code as an example of an abstract factory. – Tom Dalling Jul 10 '09 at 09:38
  • Thanks @Tom! That was more than helpful. –  Jul 10 '09 at 09:59
  • If you need to add another type of Form, even though you create a new implementation of IAbstractFormFactory, you'll have to add a new if-else check at Main method, so this code still breaks OCP – Juan Sep 12 '22 at 09:54
3

Out of the three factory patterns only Simple factory is not according to open-closed principle. Factory method and Abstract factory if implemented properly should be closed for modification and open for extension. The article Factory Design Patterns and Open-Closed Principle (OCP), the ‘O’ in SOLID published in Globinch java forum- gives a more proper explanation for this. The article also tells how to tweak simple factory to obey open closed principle.

James Matt
  • 39
  • 1
0

Open-Closed Principle means "Open" for extension "Closed" for changes. By relying on the interface and injecting this to your Application.Run() method, the Application class meets those principles. You can extend it by providing a new implementation of IAbstractFormFactory without having to make changes.

Dean Povey
  • 9,256
  • 1
  • 41
  • 52
  • But I have to constantly change Program class for adding further types. So how can you explain OCP in this case? –  Jul 10 '09 at 08:52
  • 1
    Well, it seems obvious every time you want to add something you will have to change something in the code. But at the class level, everything remains the same. You just add new classes that implement the interfaces and you won't have to change any of the already made classes. – devoured elysium Apr 12 '10 at 17:36
0

See this for example of an implementation of Abstract Factory (especially see the real world sample code).

Jimmy Chandra
  • 6,472
  • 4
  • 26
  • 38
  • Oh god! This is the most fuzzy discussion. –  Jul 10 '09 at 08:26
  • Fuzzy? Nah, hehehe. Beside the HeadFirst Design Pattern book, this site is very helpful I think in giving quite a good example to understand the patterns. – Jimmy Chandra Jul 10 '09 at 11:10