Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design Pattern - Abstract Factory pattern and Open Closed Principle

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?

like image 958
user366312 Avatar asked Oct 19 '25 03:10

user366312


1 Answers

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();
    }
}
like image 133
Tom Dalling Avatar answered Oct 22 '25 07:10

Tom Dalling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!