SlideShare a Scribd company logo
Abstract Factory:
Define aninterface orabstractclass forcreatingfamiliesof related(ordependent)objectsbutwithout
specifyingtheirconcrete sub-classes.ThatmeansAbstractFactoryletsa class returnsa factoryof
classes.Thisfactoryisalso calledas factoryof factories
Abstract factory pattern, where you find a factory (or builder) for a specific type of object, and
then this factory gives you a concrete instance of that object.
ImplementationApproach1:
1) Define the Abstractclasswhichwill extendorimplementbythe concrete class.
2) Implementthe concrete class.
3) Define the AbstractFactorywhichwill extendorimplementbythe concrete Factory class.The Return
type of the Abstractfactory isabstract classtype.
4) Implementthe concrete factory.
5) Create the Factory Producer. ReturnType of this is Abstract Factory type andthe Inputparameter
.Basedon the inputparametercreatesthe concrete factoryandreturnit.
6) In clientcode, Call the FactoryProducerWiththe inputparameter.Thiswill returnthe corresponding
concrete factoryand assignitto the AbstractFactory type.Soif you dothe actionsthenthe
correspondingconcrete classwillgetcalled.
ImplementationApproach2:
1) Define the Abstractclasswhichwill extendorimplementbythe concrete class.
2) Implementthe concrete class.
3) Define the Abstract Factorywhichwill extendorimplementbythe concrete Factory class.The Return
type of the Abstractfactory isabstract classtype.
4) Implementthe concrete factory.
5) Create the Factory Producer. ReturnType of this is Abstract class type and the Inputparameteris
abstract factory type.Based on the inputparametercreate the concrete factoryand returnit.
6) In clientcode, call the FactoryProducerWiththe inputparameterascorresponding factory.
Difference betweenfirstandsecondapproach.
In Approach 1, inside the factory producer,the if - else conditionrequiredtocheckthe inputparameter
and basedonthisthe concrete factoryiscreating.
In Approach 2, callingthe factoryproducertime itself we are passingthe concrete factory.
Benefits of Abstract Factory Pattern:
AbstractFactory patternprovidesapproachtocode forinterface ratherthanimplementation.
AbstractFactory patternis “factoryof factories”andcan be easily extendedtoaccommodate more
products,forexample we can add anothersub-classLaptopanda factory LaptopFactory.
AbstractFactory patternis robustand avoidconditional logicof Factorypattern.
Abstract Factory Pattern Examples in JDK.
 javax.xml.parsers.DocumentBuilderFactory#newInstance()
 javax.xml.transform.TransformerFactory#newInstance()
 javax.xml.xpath.XPathFactory#newInstance().
Example Program for Approach 1:
abstract class Animal {
public abstract String makeSound();
}
class Cat extends Animal {
public String makeSound() {
return "Mee";
}
}
class Dog extends Animal {
public String makeSound() {
return "Bark";
}
}
class Snake extends Animal {
public String makeSound() {
return "Hisss";
}
}
class Tyrannosaurus extends Animal {
@Override
public String makeSound() {
return "Roar";
}
}
abstract class SpeciesFactory {
public abstract Animal getAnimal(String type);
}
class MammalFactory extends SpeciesFactory {
@Override
public Animal getAnimal(String type) {
if ("dog".equals(type)) {
return new Dog();
} else {
return new Cat();
}
}
}
class ReptileFactory extends SpeciesFactory {
@Override
public Animal getAnimal(String type) {
if ("snake".equals(type)) {
return new Snake();
} else {
return new Tyrannosaurus();
}
}
}
class AbstractFactory {
public SpeciesFactory getSpeciesFactory(String type) {
if ("mammal".equals(type)) {
return new MammalFactory();
} else {
return new ReptileFactory();
}
}
}
public class AbstractFactoryDemo {
public static void main(String[] args) {
AbstractFactory abstractFactory = new AbstractFactory();
SpeciesFactory speciesFactory1 =
abstractFactory.getSpeciesFactory("reptile");
Animal a1 = speciesFactory1.getAnimal("tyrannosaurus");
System.out.println("a1 sound: " + a1.makeSound());
Animal a2 = speciesFactory1.getAnimal("snake");
System.out.println("a2 sound: " + a2.makeSound());
SpeciesFactory speciesFactory2 =
abstractFactory.getSpeciesFactory("mammal");
Animal a3 = speciesFactory2.getAnimal("dog");
System.out.println("a3 sound: " + a3.makeSound());
Animal a4 = speciesFactory2.getAnimal("cat");
System.out.println("a4 sound: " + a4.makeSound());
}
}
Example Program for Approach 2:
abstract class Computer {
public abstract String getRAM();
public abstract String getHDD();
public abstract String getCPU();
@Override
public String toString() {
return "RAM= " + this.getRAM() + ", HDD=" + this.getHDD() + ",CPU=" +
this.getCPU();
}
}
class PC extends Computer {
private String ram;
private String hdd;
private String cpu;
public PC(String ram, String hdd, String cpu) {
this.ram = ram;
this.hdd = hdd;
this.cpu = cpu;
}
@Override
public String getRAM() {
return this.ram;
}
@Override
public String getHDD() {
return this.hdd;
}
@Override
public String getCPU() {
return this.cpu;
}
}
class Server extends Computer {
private String ram;
private String hdd;
private String cpu;
public Server(String ram, String hdd, String cpu) {
this.ram = ram;
this.hdd = hdd;
this.cpu = cpu;
}
@Override
public String getRAM() {
return this.ram;
}
@Override
public String getHDD() {
return this.hdd;
}
@Override
public String getCPU() {
return this.cpu;
}
}
interface ComputerAbstractFactory {
public Computer createComputer();
}
class PCFactory implements ComputerAbstractFactory {
private String ram;
private String hdd;
private String cpu;
public PCFactory(String ram, String hdd, String cpu) {
this.ram = ram;
this.hdd = hdd;
this.cpu = cpu;
}
@Override
public Computer createComputer() {
return new PC(ram, hdd, cpu);
}
}
class ServerFactory implements ComputerAbstractFactory {
private String ram;
private String hdd;
private String cpu;
public ServerFactory(String ram, String hdd, String cpu) {
this.ram = ram;
this.hdd = hdd;
this.cpu = cpu;
}
@Override
public Computer createComputer() {
return new Server(ram, hdd, cpu);
}
}
class ComputerFactory {
public static Computer getComputer(ComputerAbstractFactory factory) {
return factory.createComputer();
}
}
public class TestDesignPatterns {
public static void main(String[] args) {
Computer pc = ComputerFactory.getComputer(new PCFactory("2 GB", "500
GB", "2.4 GHz"));
Computer server = ComputerFactory.getComputer(new ServerFactory("16 GB",
"1 TB", "2.9 GHz"));
System.out.println("AbstractFactory PC Config::" + pc);
System.out.println("AbstractFactory Server Config::" + server);
}
}
Abstract factory VS Builder Pattern:
Builder provides you more control over the object creation process .
Abstract factory VS Factory Pattern
With the Factory pattern, you produce implementations (Apple, Banana, Cherry, etc.) of a
particular interface -- say, IFruit.
With the Abstract Factory pattern, you produce implementations of a particular Factory interface
-- e.g., IFruitFactory. Each of those knows how to create different kinds of fruit.
Abstract factory VS Factory Method Pattern
Factory Method pattern uses inheritance and relies on a subclass to handle the desired object
instantiation.
Abstract Factory pattern, a class delegates the responsibility of object instantiation to another
object via composition...
The methods of an Abstract Factory are implemented as Factory Methods. Both the Abstract
Factory Pattern and the Factory Method Pattern decouples the client system from the actual
implementation classes through the abstract types and factories.
The Abstract Factory Pattern consists of an AbstractFactory, ConcreteFactory, AbstractProduct,
ConcreteProduct and Client.
How to implement
The Abstract Factory Pattern can be implemented using the Factory Method Pattern, Prototype
Pattern or the Singleton Pattern. The ConcreteFactory object can be implemented as a Singleton
as only one instance of the ConcreteFactory object is needed.
Factory Method pattern is a simplified version of Abstract Factory pattern. Factory Method
pattern is responsible of creating products that belong to one family, while Abstract Factory
pattern deals with multiple families of products.
Factory Method uses interfaces and abstract classes to decouple the client from the generator
class and the resulting products. Abstract Factory has a generator that is a container for several
factory methods, along with interfaces decoupling the client from the generator and the
products.
When to Use the Factory Method Pattern
Use the Factory Method pattern when there is a need to decouple a client from a particular
product that it uses. Use the Factory Method to relieve a client of responsibility for creating and
configuring instances of a product.
When to Use the Abstract Factory Pattern
Use the Abstract Factory pattern when clients must be decoupled from product classes.
Especially useful for program configuration and modification. The Abstract Factory pattern
can also enforce constraints about which classes must be used with others. It may be a lot of
work to make new concrete factories.

More Related Content

What's hot

Recommending Method Invocation Context Changes
Recommending Method Invocation Context ChangesRecommending Method Invocation Context Changes
Recommending Method Invocation Context Changes
Beat Fluri
 
Smarter Testing with Spock
Smarter Testing with SpockSmarter Testing with Spock
Smarter Testing with Spock
Dmitry Voloshko
 
Advanced junit and mockito
Advanced junit and mockitoAdvanced junit and mockito
Advanced junit and mockito
Mathieu Carbou
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
Alok Kumar
 

What's hot (20)

How to fake_properly
How to fake_properlyHow to fake_properly
How to fake_properly
 
Unit testing concurrent code
Unit testing concurrent codeUnit testing concurrent code
Unit testing concurrent code
 
Grid gain paper
Grid gain paperGrid gain paper
Grid gain paper
 
Code generation for alternative languages
Code generation for alternative languagesCode generation for alternative languages
Code generation for alternative languages
 
Unit/Integration Testing using Spock
Unit/Integration Testing using SpockUnit/Integration Testing using Spock
Unit/Integration Testing using Spock
 
JUnit Pioneer
JUnit PioneerJUnit Pioneer
JUnit Pioneer
 
Recommending Method Invocation Context Changes
Recommending Method Invocation Context ChangesRecommending Method Invocation Context Changes
Recommending Method Invocation Context Changes
 
Smarter Testing With Spock
Smarter Testing With SpockSmarter Testing With Spock
Smarter Testing With Spock
 
Smarter Testing with Spock
Smarter Testing with SpockSmarter Testing with Spock
Smarter Testing with Spock
 
Advanced junit and mockito
Advanced junit and mockitoAdvanced junit and mockito
Advanced junit and mockito
 
Mockito with a hint of PowerMock
Mockito with a hint of PowerMockMockito with a hint of PowerMock
Mockito with a hint of PowerMock
 
Java 8: the good, the bad and the ugly (Oracle Code Brussels 2017)
Java 8: the good, the bad and the ugly (Oracle Code Brussels 2017)Java 8: the good, the bad and the ugly (Oracle Code Brussels 2017)
Java 8: the good, the bad and the ugly (Oracle Code Brussels 2017)
 
Spock framework
Spock frameworkSpock framework
Spock framework
 
Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)
 
Java 8: the good, the bad and the ugly (JBCNConf 2017)
Java 8: the good, the bad and the ugly (JBCNConf 2017)Java 8: the good, the bad and the ugly (JBCNConf 2017)
Java 8: the good, the bad and the ugly (JBCNConf 2017)
 
Spock Framework
Spock FrameworkSpock Framework
Spock Framework
 
Spock: Test Well and Prosper
Spock: Test Well and ProsperSpock: Test Well and Prosper
Spock: Test Well and Prosper
 
Important java programs(collection+file)
Important java programs(collection+file)Important java programs(collection+file)
Important java programs(collection+file)
 
Java programs
Java programsJava programs
Java programs
 
Spock Testing Framework - The Next Generation
Spock Testing Framework - The Next GenerationSpock Testing Framework - The Next Generation
Spock Testing Framework - The Next Generation
 

Similar to Abstract factory

27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples
Quang Suma
 
1 MVC – Ajax and Modal Views AJAX stands for Asynch.docx
1  MVC – Ajax and Modal Views AJAX stands for Asynch.docx1  MVC – Ajax and Modal Views AJAX stands for Asynch.docx
1 MVC – Ajax and Modal Views AJAX stands for Asynch.docx
honey725342
 
Padroes Projeto
Padroes ProjetoPadroes Projeto
Padroes Projeto
lcbj
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
Vince Vo
 

Similar to Abstract factory (20)

Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder Desing pattern prototype-Factory Method, Prototype and Builder
Desing pattern prototype-Factory Method, Prototype and Builder
 
Factory method pattern (Virtual Constructor)
Factory method pattern (Virtual Constructor)Factory method pattern (Virtual Constructor)
Factory method pattern (Virtual Constructor)
 
Java design patterns
Java design patternsJava design patterns
Java design patterns
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples27418524 design-patterns-dot-net-with-examples
27418524 design-patterns-dot-net-with-examples
 
Creational pattern 2
Creational pattern 2Creational pattern 2
Creational pattern 2
 
Java Static Factory Methods
Java Static Factory MethodsJava Static Factory Methods
Java Static Factory Methods
 
Design patterns(red)
Design patterns(red)Design patterns(red)
Design patterns(red)
 
Design patterns
Design patternsDesign patterns
Design patterns
 
1 MVC – Ajax and Modal Views AJAX stands for Asynch.docx
1  MVC – Ajax and Modal Views AJAX stands for Asynch.docx1  MVC – Ajax and Modal Views AJAX stands for Asynch.docx
1 MVC – Ajax and Modal Views AJAX stands for Asynch.docx
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Padroes Projeto
Padroes ProjetoPadroes Projeto
Padroes Projeto
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
 
Тарас Олексин - Sculpt! Your! Tests!
Тарас Олексин  - Sculpt! Your! Tests!Тарас Олексин  - Sculpt! Your! Tests!
Тарас Олексин - Sculpt! Your! Tests!
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 

Recently uploaded (20)

UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 

Abstract factory

  • 1. Abstract Factory: Define aninterface orabstractclass forcreatingfamiliesof related(ordependent)objectsbutwithout specifyingtheirconcrete sub-classes.ThatmeansAbstractFactoryletsa class returnsa factoryof classes.Thisfactoryisalso calledas factoryof factories Abstract factory pattern, where you find a factory (or builder) for a specific type of object, and then this factory gives you a concrete instance of that object. ImplementationApproach1: 1) Define the Abstractclasswhichwill extendorimplementbythe concrete class. 2) Implementthe concrete class. 3) Define the AbstractFactorywhichwill extendorimplementbythe concrete Factory class.The Return type of the Abstractfactory isabstract classtype. 4) Implementthe concrete factory. 5) Create the Factory Producer. ReturnType of this is Abstract Factory type andthe Inputparameter .Basedon the inputparametercreatesthe concrete factoryandreturnit. 6) In clientcode, Call the FactoryProducerWiththe inputparameter.Thiswill returnthe corresponding concrete factoryand assignitto the AbstractFactory type.Soif you dothe actionsthenthe correspondingconcrete classwillgetcalled. ImplementationApproach2: 1) Define the Abstractclasswhichwill extendorimplementbythe concrete class. 2) Implementthe concrete class. 3) Define the Abstract Factorywhichwill extendorimplementbythe concrete Factory class.The Return type of the Abstractfactory isabstract classtype. 4) Implementthe concrete factory. 5) Create the Factory Producer. ReturnType of this is Abstract class type and the Inputparameteris abstract factory type.Based on the inputparametercreate the concrete factoryand returnit. 6) In clientcode, call the FactoryProducerWiththe inputparameterascorresponding factory.
  • 2. Difference betweenfirstandsecondapproach. In Approach 1, inside the factory producer,the if - else conditionrequiredtocheckthe inputparameter and basedonthisthe concrete factoryiscreating. In Approach 2, callingthe factoryproducertime itself we are passingthe concrete factory. Benefits of Abstract Factory Pattern: AbstractFactory patternprovidesapproachtocode forinterface ratherthanimplementation. AbstractFactory patternis “factoryof factories”andcan be easily extendedtoaccommodate more products,forexample we can add anothersub-classLaptopanda factory LaptopFactory. AbstractFactory patternis robustand avoidconditional logicof Factorypattern. Abstract Factory Pattern Examples in JDK.  javax.xml.parsers.DocumentBuilderFactory#newInstance()  javax.xml.transform.TransformerFactory#newInstance()  javax.xml.xpath.XPathFactory#newInstance(). Example Program for Approach 1: abstract class Animal { public abstract String makeSound(); } class Cat extends Animal { public String makeSound() { return "Mee"; } } class Dog extends Animal { public String makeSound() { return "Bark"; } } class Snake extends Animal { public String makeSound() { return "Hisss"; } } class Tyrannosaurus extends Animal {
  • 3. @Override public String makeSound() { return "Roar"; } } abstract class SpeciesFactory { public abstract Animal getAnimal(String type); } class MammalFactory extends SpeciesFactory { @Override public Animal getAnimal(String type) { if ("dog".equals(type)) { return new Dog(); } else { return new Cat(); } } } class ReptileFactory extends SpeciesFactory { @Override public Animal getAnimal(String type) { if ("snake".equals(type)) { return new Snake(); } else { return new Tyrannosaurus(); } } } class AbstractFactory { public SpeciesFactory getSpeciesFactory(String type) { if ("mammal".equals(type)) { return new MammalFactory(); } else { return new ReptileFactory(); } } } public class AbstractFactoryDemo { public static void main(String[] args) { AbstractFactory abstractFactory = new AbstractFactory(); SpeciesFactory speciesFactory1 = abstractFactory.getSpeciesFactory("reptile"); Animal a1 = speciesFactory1.getAnimal("tyrannosaurus"); System.out.println("a1 sound: " + a1.makeSound()); Animal a2 = speciesFactory1.getAnimal("snake"); System.out.println("a2 sound: " + a2.makeSound());
  • 4. SpeciesFactory speciesFactory2 = abstractFactory.getSpeciesFactory("mammal"); Animal a3 = speciesFactory2.getAnimal("dog"); System.out.println("a3 sound: " + a3.makeSound()); Animal a4 = speciesFactory2.getAnimal("cat"); System.out.println("a4 sound: " + a4.makeSound()); } } Example Program for Approach 2: abstract class Computer { public abstract String getRAM(); public abstract String getHDD(); public abstract String getCPU(); @Override public String toString() { return "RAM= " + this.getRAM() + ", HDD=" + this.getHDD() + ",CPU=" + this.getCPU(); } } class PC extends Computer { private String ram; private String hdd; private String cpu; public PC(String ram, String hdd, String cpu) { this.ram = ram; this.hdd = hdd; this.cpu = cpu; } @Override public String getRAM() { return this.ram; } @Override public String getHDD() { return this.hdd; } @Override public String getCPU() { return this.cpu; } }
  • 5. class Server extends Computer { private String ram; private String hdd; private String cpu; public Server(String ram, String hdd, String cpu) { this.ram = ram; this.hdd = hdd; this.cpu = cpu; } @Override public String getRAM() { return this.ram; } @Override public String getHDD() { return this.hdd; } @Override public String getCPU() { return this.cpu; } } interface ComputerAbstractFactory { public Computer createComputer(); } class PCFactory implements ComputerAbstractFactory { private String ram; private String hdd; private String cpu; public PCFactory(String ram, String hdd, String cpu) { this.ram = ram; this.hdd = hdd; this.cpu = cpu; } @Override public Computer createComputer() { return new PC(ram, hdd, cpu); } } class ServerFactory implements ComputerAbstractFactory { private String ram; private String hdd; private String cpu; public ServerFactory(String ram, String hdd, String cpu) { this.ram = ram; this.hdd = hdd; this.cpu = cpu; }
  • 6. @Override public Computer createComputer() { return new Server(ram, hdd, cpu); } } class ComputerFactory { public static Computer getComputer(ComputerAbstractFactory factory) { return factory.createComputer(); } } public class TestDesignPatterns { public static void main(String[] args) { Computer pc = ComputerFactory.getComputer(new PCFactory("2 GB", "500 GB", "2.4 GHz")); Computer server = ComputerFactory.getComputer(new ServerFactory("16 GB", "1 TB", "2.9 GHz")); System.out.println("AbstractFactory PC Config::" + pc); System.out.println("AbstractFactory Server Config::" + server); } } Abstract factory VS Builder Pattern: Builder provides you more control over the object creation process . Abstract factory VS Factory Pattern With the Factory pattern, you produce implementations (Apple, Banana, Cherry, etc.) of a particular interface -- say, IFruit. With the Abstract Factory pattern, you produce implementations of a particular Factory interface -- e.g., IFruitFactory. Each of those knows how to create different kinds of fruit.
  • 7. Abstract factory VS Factory Method Pattern Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation. Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition... The methods of an Abstract Factory are implemented as Factory Methods. Both the Abstract Factory Pattern and the Factory Method Pattern decouples the client system from the actual implementation classes through the abstract types and factories. The Abstract Factory Pattern consists of an AbstractFactory, ConcreteFactory, AbstractProduct, ConcreteProduct and Client. How to implement The Abstract Factory Pattern can be implemented using the Factory Method Pattern, Prototype Pattern or the Singleton Pattern. The ConcreteFactory object can be implemented as a Singleton as only one instance of the ConcreteFactory object is needed. Factory Method pattern is a simplified version of Abstract Factory pattern. Factory Method pattern is responsible of creating products that belong to one family, while Abstract Factory pattern deals with multiple families of products. Factory Method uses interfaces and abstract classes to decouple the client from the generator class and the resulting products. Abstract Factory has a generator that is a container for several factory methods, along with interfaces decoupling the client from the generator and the products. When to Use the Factory Method Pattern Use the Factory Method pattern when there is a need to decouple a client from a particular product that it uses. Use the Factory Method to relieve a client of responsibility for creating and configuring instances of a product. When to Use the Abstract Factory Pattern Use the Abstract Factory pattern when clients must be decoupled from product classes. Especially useful for program configuration and modification. The Abstract Factory pattern can also enforce constraints about which classes must be used with others. It may be a lot of work to make new concrete factories.