Abstract Factory Pattern:
1. The simple factory pattern is not practical in practice and has obvious shortcomings.
2. The factory method pattern solves the drawback that a simple factory cannot implement the "open-closed principle", but the code will be redundant.
3.抽象工厂模式:Provide an interface for creating families of related or dependent objects without specifying their concrete classes。
Abstract product: mobile phone and charger
//Product category (mobile phone) public interface Mobile { String getName(); } //Product class (charger) public interface Charger { String getName(); }
public class AppleMobile implements Mobile { private String name; public AppleMobile() { name = "iPhone"; System.out.println("Apple phone production is complete!"); } @Override public String getName() { return name != null ? name : null; } }
public class HuaweiMobile implements Mobile { private String name; public HuaweiMobile() { name = "Huawei phone"; System.out.println("Huawei mobile phone production completed!"); } @Override public String getName() { return name != null ? name : null; } }
Cell Phones and Chargers: Two Specific Brands
public class AppleCharger implements Charger { private String name; public AppleCharger() { name = "iPhone Charger"; System.out.println("Apple mobile phone charger production completed!"); } @Override public String getName() { return name != null ? name : null; } } //+++++++++++++++++++++++++++++++ public class HuaweiCharger implements Charger { private String name; public HuaweiCharger() { name = "Huawei Charger"; System.out.println("Huawei mobile phone charger production completed!"); } @Override public String getName() { return name != null ? name : null; } }
Product samples are already available, and now the factory is required to produce them.
The factory needs to be abstracted:
public interface MobileFactory { // make mobile phone Mobile getMobile(); // make the charger Charger getCharger(); }
Create a concrete factory
public class AppleFactory implements MobileFactory { // produce iPhone @Override public Mobile getMobile() { return new AppleMobile(); } // Produce iPhone chargers @Override public Charger getCharger() { return new AppleCharger(); } }
public class HuaweiFactory implements MobileFactory { // Produce Huawei phones @Override public Mobile getMobile() { return new HuaweiMobile(); } // Produce Huawei mobile phone chargers @Override public Charger getCharger() { return new HuaweiCharger(); } }
Test using factory class:
public class Test { public static void main(String[] args) { AppleFactory appleFactory = new AppleFactory(); AppleMobile appleMobile = (AppleMobile) appleFactory.getMobile(); AppleCharger appleCharger = (AppleCharger) appleFactory.getCharger(); HuaweiFactory huaweiFactory = new HuaweiFactory(); HuaweiMobile huaweiMobile = (HuaweiMobile) huaweiFactory.getMobile(); HuaweiCharger huaweiCharger = (HuaweiCharger) huaweiFactory.getCharger(); } }
Summarize:
1. Abstract the product
2. Abstract the factory
3. Concrete products implement abstract product interfaces
4. Concrete factories implement abstract factories
5. Create the desired class as needed