Intento
Fornisce una intefaccia per creare famiglie di oggetti dipendenti o imparentati senza specificare le classi concrete.
Also Known As
Kit
Descrizione
L' Abstract Factory pattern fornisce una classe astratta che determina quale classe concreta è appropriata per creare un set di prodotti concreti che implementano una interfaccia standard.
Il client interagisce solo con le interfacce dei prodotti e l'abstract factory class
Il client non conosce le classi concrete costruttrici.
L'Abstract Factory è simile al Factory Method pattern eccetto per il fatto che crea famiglie di oggetti imparentati tra loro.
Benefici- Isola le classi concrete.
- Rende semplice scambiare le famiglie di prodotti.
- Promuove la consistenza tra prodotti.
Quando usarlo- Il sistema deve essere indipendente da come i suoi prodotti sono creati, composti o rappresentati.
- Il sistema deve essere configurato con una di molte famiglie di prodotti. Per esempio Microsoft Windows o Apple McIntosh? widget grafici.
- Le famiglie di prodotti sono pensate per essere utilizzate insieme, e tu devi rafforzare questa costrizione. Questo è il punto chiave del pattern, altrimenti tu potresti utilizzare Factory Method
- Vuoi fornire una libreria di prodotti, e rivelare soltanto le loro interfacce, mantenendo nascoste le relative implementazioni.
Esempio
public class Client {
public static void main(String[] args) {
FurnitureFactory factory = new PineFurnitureFactory();
Chair chair = factory.createChair();
Table table = factory.createTable();
factory = new SteelFurnitureFactory();
chair = factory.createChair();
table = factory.createTable();
}
}
|
/**
* @stereotype Abstract Product
*/
public interface Chair {
}
|
/**
* @stereotype Abstract Product
*/
public interface Table {
}
|
/**
* @stereotype Abstract Factory
*/
public interface FurnitureFactory {
public Chair createChair();
public Table createTable();
}
|
/**
* @stereotype Concrete Product
*/
public class PineChair implements Chair {
public String toString() { return "I am a pine chair."; }
}
|
/**
* @stereotype Concrete Product
*/
public class PineTable implements Table {
public String toString() { return "I am a pine table."; }
}
|
/**
* @stereotype Concrete Factory
*/
public class PineFurnitureFactory implements FurnitureFactory {
public Chair createChair() { return new PineChair(); }
public Table createTable() { return new PineTable(); }
}
|
/**
* @stereotype Concrete Product
*/
public class SteelChair implements Chair {
public String toString() { return "I am a steel chair."; }
}
|
/**
* @stereotype Concrete Product
*/
public class SteelTable implements Table {
public String toString() { return "I am a steel table."; }
}
|
/**
* @stereotype Concrete Factory
*/
public class SteelFurnitureFactory implements FurnitureFactory {
public Chair createChair() { return new SteelChair(); }
public Table createTable() { return new SteelTable(); }
}
|
att:AbstractFactory
AbstractFactory is mentioned on: CreationalPatterns