Design Pattern _ abstract factory pattern
Foreshadowing
To understand the greatest advantages of the abstract mode, you must first understand the product level and product family.
In the above class diagram, the hatchback and the sedan are called two different hierarchical structures, while the 2.0 and 2.4 displacement vehicles are called two different product families. To be more specific, the 2.0-displacement hatchback and the 2.4-displacement hatchback belong to the same level structure. The 2.0-displacement and 2.4-displacement hatchback belong to another level structure; while 2.0-displacement hatchways and 2.0-displacement sedan cars belong to the same product family, 2.4-displacement hatchways and 2.4-displacement sedan cars belong to another product family.
ExampleNext, I wrote the article about the creation of a girl
public interface Human { public void getColor(); public void talk(); public void getSex();}
Public abstract class AbstractYellowHuman implements Human {public void getColor () {System. out. println (yellow race);} public void talk () {System. out. println (Chinese );}}
Public class MaleYellow extends actyellowhuman {public void getSex () {System. out. println (yellow male );}}
Public class FemaleYellow extends actyellowhuman {public void getSex () {System. out. println (yellow female );}}
White and black are both the same... No code is posted here.
public interface HumanFactory { public Human createWhiteHuman(); public Human createBlackHuman(); public Human createYellowHuman();}
public class FemaleFactory implements HumanFactory { public Human createBlackHuman() { return null; } public Human createWhiteHuman() { return null; } public Human createYellowHuman() { return new FemaleYellow(); }}
public class MaleFactory implements HumanFactory { public Human createBlackHuman() { return null; } public Human createWhiteHuman() { return null; } public Human createYellowHuman() { return new MaleYellow(); }}
Public class Client {public static void main (String [] args) {HumanFactory factory = new MaleFactory (); MaleYellow maleYellow = (MaleYellow) factory. createYellowHuman (); maleYellow. talk () ;}}/* Human Interface * abstract class white people AbstractWhiteHuman implement two methods in the Human interface: talk () and getColor (), in this way, there are three kinds of people. * white men MaleWhiteHuman inherit wannactwhitehuman get white men * white women FemaleWhiteHuman inherit wannactwhitehuman get white women * As for the factory you can DIY **/
AdvantagesBut it can meet multiple levels of structure, such as buying milk and coffee, plus a large or small cup.
Disadvantages are very difficult to expand
Applicable scenarios
Abstract Factory mode can be used when the object to be created is a series of interrelated or interdependent product families. A more clear point is that in an inheritance system, if there are multiple levels (that is, there are multiple abstract classes ), the abstract factory mode can be used if there are certain associations or constraints between implementation classes in each hierarchy. If there is no association or constraint between implementation classes in each hierarchy, it is more appropriate to create products using multiple independent factories.