Design Template - Abstract Factory Template and Open Closed Principle

I am starting to develop templates.

I am trying to use an abstract Factory template while maintaining the principle of open closing.

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();
        }
    }

Could this be an example of an Abstract Factory Pattern?

If so, how can I reorganize it to include the concept of an open, closed principle?

+3
source share
4 answers

The example you provided is not an abstract factory. Abstract factories have factory methods (i.e., methods that create and return objects).

open/closed, factory . "" , , factory ( ), "", , factory.

UPDATE: , , 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();
    }
}
+4

factory Simple factory . factory Abstract factory, , . Factory (OCP), "O in SOLID" , Java Globinch, . , factory .

+2

"" "" . Application.Run(), Application . , IAbstractFormFactory .

0

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

0
source

Source: https://habr.com/ru/post/1712277/


All Articles