Abstract Factory Template

Regarding the abstract Factory template.

As you know, there are two ways to use ie

  • Either instantiate a specific class directly, or
  • Provide an interface with which we can access a specific class

Can someone say the advantages / disadvantages that if I use option 1, then these may be problems that may arise, or vice versa, if this is possible in real time.

Thanks in advance...

+3
source share
2 answers

If you have a specific class that is not a subclass, and you are sure that it will never exist, then you can just create it, as in your option 1 - there is no need for a factory for this.

/ , - factory ( ).

0

, , ( #):

public class Consumer()
{
    public void DoStuff()
    {
        var f = new Foo()
        f.DoIt("bar");
    }
}

, , .

, , Factory, :

public class Consumer()
{
    private readonly IFooFactory fooFactory;

    public Consumer(IFooFactory fooFactory)
    {
        if (fooFactory == null)
        {
            throw new ArgumentNullException("fooFactory");
        }

        this.fooFactory = fooFactory;
    }

    public void DoStuff()
    {
        var f = this.fooFactory.Create();
        f.DoIt("bar");
    }
}

, , f Consumer.

, .

Factory, . , .

+1

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


All Articles