When to Use the Abstract Factory Pattern

I read about the abstract Factory pattern, but although I understand this, I don't know when to use it. The definition of the template suggests that it is used to abstract the object, but it is not very clear to me, because of why I need to do this, why I should consider the object instance and why (or when) it is important to abstract it.

+4
source share
4 answers

The main advantage of the template is that it allows you to use clean and reusable code by untying the creation of an object from the use of the object.

#, , , , , . Android, SSL/TLS.

, Android SSL, . Factory , - Factory, .

:

// this is pretty gross, but what can you do
public class SocketFactorySupplier implements Supplier<SSLSocketFactory> {
  @Override public SSLSocketFactory get() {
    if (androidVersion >= 2.1 && androidVersion <= 2.6) {
      return createShiftyWorkaround();
    } else {
      return getDefaultSocketFactory();
    }
  }

  // here are ~500 lines of SSL code
}

...

public class NetworkClient {
  private final Supplier<SSLSocketFactory> supplier;
  private Socket socket;

  public NetworkClient(Supplier<SSLSocketFactory> supplier) {
    this.supplier = supplier;
  }

  public void connect() {
    socket = supplier.get().createSocket();
    socket.connect();

    // code that doesn't care about SSL at all and is simpler for it
  }
}

, , , Factory:

  • NetworkClient , , .
  • , , .
  • SSL ,
  • .
+2

, , . Database, factory Database.CreateCommand. , Database.

, factory , DatabaseManager, DatabaseManager.GetDatabase(databaseType), . DatabaseType , .

Database Factory, DatabaseManager Factory `. factory, .

, - :

Dim sqlCommand as ICommand = DatabaseManager.GetDatabase("MsSQL").CreateCommand

Dim oracleCommand as ICommand = DatabaseManager.GetDatabase("Oracle").CreateCommand
+1

, .

, , . .

public class MyShoppingCartUI()
{
    private List<IProduct> _productsInCart = new List<IProduct>();

    public void ClickAddProductButton()
    {
        IProduct product = new ProductOne();
        _productsInCart.Add(product);
    }
}

, , . :

public class MyShoppingCartUI()
{
    private List<IProduct> _productsInCart = new List<IProduct>();

    public void ClickAddProductOneButton()
    {
        IProduct product = new ProductOne();
        _productsInCart.Add(product);
    }

    public void ClickAddProductTwoButton()
    {
        IProduct product = new ProductTwo();
        _productsInCart.Add(product);
    }
}

, , , , . , ?

factory. :

public class ProductFactory()
{
    public IProduct Create(string productName)
    {
        if (productName == "Product1")
            return new ProductOne();
        else if (productName == "Product2")
            return new ProductTwo();            
    }
}

public class MyShoppingCartUI()
{
    private ProductFactory _factory = new ProductFactory();
    private List<IProduct> _productsInCart = new List<IProduct>();

    public void AddItem(string productName)
    {
        IProduct product = _factory.Create(productName);
        _productsInCart.Add(product);
    }
}

, , , , , . , Factory .

, Factory Product, , . 1-2 Factory , , .

+1

Factory - , . , , , , . Abstract Factory , Factory, , . , , , , Factory , . Factory, , .

, , - . Factory, , , . Factory . Factory . , Factory, , 2 - , , . Factory , Factory .

- . , Factory , OS, Windows, Linux, IOS, Android .. Factory Factory .

. Factory OO, . , , , , Unity ( - ).

+1
source

All Articles