views:

469

answers:

3

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?

A: 

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
But I have to constantly change Program class for adding further types. So how can you explain OCP in this case?
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
A: 

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

Jimmy Chandra
Oh god! This is the most fuzzy discussion.
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
+1  A: 

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
http://stackoverflow.com/questions/1001767/design-patterns-factory-vs-abstract-factoryAre you sure?
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
Thanks @Tom! That was more than helpful.