Design pattern--Abstract Factory mode
1, definition: Provide an interface that creates a series of related or interdependent objects without having to develop their specific classes;
2, the following is a schematic diagram of the abstract factory pattern:
3, the implementation code of the Abstract factory pattern:
1), Structure diagram:
2), Code
Entry Class Main.class:
public class Main {
Public static void Main (string[] args) {
Breadmaker Breadmaker;
Pizzamaker Pizzamaker;
System.out.println ("Opening of a noodle bakery");
System.out.println ("Customers want to buy black bread");
Ifactory breadfactory = new Blackbreadfactory ();
Breadmaker = Breadfactory.createbread ();
Breadmaker.getbread ();
SYSTEM.OUT.PRINTLN ("Customer wants black Chocolate pizza");
Pizzamaker = Breadfactory.createpizza ();
Pizzamaker.getpizza ();
}
}
Factory Interface Class Ifactory.class:
Public interface Ifactory {
Breadmaker Createbread ();
Pizzamaker Createpizza ();
}
Three Factory classes:
Blackbreadfactory.class,honeybreadfactory.class,whitebreadfactory.class:
public class Blackbreadfactory implements ifactory{
@Override
Public Breadmaker Createbread () {
TODO auto-generated Method Stub
return new Blackbread ();
}
@Override
Public Pizzamaker Createpizza () {
TODO auto-generated Method Stub
return new Blackpizza ();
}
}
public class Honeybreadfactory implements ifactory{
@Override
Public Breadmaker Createbread () {
TODO auto-generated Method Stub
return new Honeybread ();
}
@Override
Public Pizzamaker Createpizza () {
TODO auto-generated Method Stub
return new Honeypizza ();
}
}
public class Whitebreadfactory implements ifactory{
@Override
Public Breadmaker Createbread () {
TODO auto-generated Method Stub
return new Whiteblead ();
}
@Override
Public Pizzamaker Createpizza () {
TODO auto-generated Method Stub
return new Whitepizza ();
}
}
Two parent classes:
Breadmaker,pizzamaker:
public class Breadmaker {
public void Getbread () { }
}
public class Pizzamaker {
public void Getpizza () { }
}
Related sub-classes:
Blackbread,blackpizza,honeybread,honeypizza,whiteblead,whitepizza:
public class Blackbread extends breadmaker{
@Override
public void Getbread () {
TODO auto-generated Method Stub
Super. Getbread ();
System.out.println ("dark chocolate bread made of a facet");
}
}
public class Blackpizza extends pizzamaker{
@Override
public void Getpizza () {
TODO auto-generated Method Stub
Super. Getpizza ();
System.out.println ("Make a dark chocolate pizza");
}
}
public class Honeybread extends breadmaker{
@Override
public void Getbread () {
TODO auto-generated Method Stub
Super. Getbread ();
System.out.println ("Made of honey bread");
}
}
public class Honeypizza extends pizzamaker{
@Override
public void Getpizza () {
TODO auto-generated Method Stub
Super. Getpizza ();
System.out.println ("Baked honey pizza on a noodle");
}
}
public class Whiteblead extends breadmaker{
@Override
public void Getbread () {
TODO auto-generated Method Stub
Super. Getbread ();
System.out.println ("White chocolate bread made of a facet");
}
}
public class Whitepizza extends pizzamaker{
@Override
public void Getpizza () {
TODO auto-generated Method Stub
Super. Getpizza ();
System.out.println ("White chocolate Pizza" made from a facet);
}
}
3), operation result:
3, Abstract Factory mode application:
1), a series to be independent of its product creation, composition and representation;
2), a series to be configured due to one of multiple product families;
3) When it is necessary to emphasize the design of a series of related product objects for joint use;
4), when you want to provide a product class library, and only display their interface instead of the implementation;
Abstract factory pattern of design patterns