Abstract Factory mode is an improvement to the factory method pattern: In the factory method mode, if new products are added, then it is necessary to go into the factory to add new methods to produce new products, which is not very friendly to code development. The abstract factory model is different, after the addition of new products, only need to write a corresponding factory.
Package mode.abstract_factory;/* Abstract Fruit Factory */public interface Abstractfruitfactory {public Fruit getfruit ();}
Package mode.abstract_factory;/* Abstract Fruit Factory */public interface Abstractfruitfactory {public Fruit getfruit ();}
Package mode.abstract_factory;/* Apple */public class Apple implements Fruit {@Overridepublic void Eat () {System.out.println ("Eat Apples");}}
Package mode.abstract_factory;/** Apple Factory */public class Applefactory implements Abstractfruitfactory {@Overridepublic Fruit Getfruit () {return new Apple ();}}
With this Apple factory, we can produce our apples, but now we have introduced an orange product.
Package mode.abstract_factory;/** Orange */public class Orange implements Fruit {@Overridepublic void eat () { System.out.println ("Eat Oranges");}}
To produce oranges, we build an orange factory.
Package mode.abstract_factory;/** Orange Factory */public class Orangefactory implements Abstractfruitfactory {@Overridepublic Fruit Getfruit () {return new Orange ();}}
With this orange factory we can produce oranges, and here we do not modify the original factory, but add new factories. This makes it much better to create new independent classes with a schema than to modify other classes in the source code.
Abstract Factory mode