First, the factory method mode
The flaw of the simple factory model is that the creation of classes is too dependent on the factory, and the code of the factory class must be modified once the program is extended
It's against the knapsack principle.
and the factory method model has been improved to solve this problem. .
Public interface Sender {public void Send ();} Public interface Producer {public Sender produce ();} public class Mailfactory implements Producer{public Sender produce () {return new Mail ();}} public class Mail implements sender{public void Send () {System.out.println ("Mail is sending");}} Test class public class Factorypatten {public static void main (string[] args) {mailfactory mailfactory = new Mailfactory (); Sender sender = mailfactory.produce (); Sender.send ();}}
The advantage of the factory method pattern is that once the function is extended, only one additional class is required to implement the sender interface, and then a factory class is implemented to implement the producer interface.
For the original plant to retain, so as to ensure that no breach of the closure principle, and thus improve the expansion.
Second, abstract Factory mode
Factory method pattern and abstract factory model are not clear
Factory method Mode:
An abstract product class that can derive a lot of specific product classes
An abstract factory class that can derive many specific factory classes
Each specific factory class can only create an instance of a specific product
Abstract Factory mode:
Multiple abstract product classes, each abstract product class can derive multiple specific product classes.
An abstract factory class that can derive a number of specific factory classes.
Each specific factory class can create multiple instances of a specific product class, which is the creation of multiple products under a product line.
Package Day03;interface producecomputer{}//Abstract Computer Products Interface producehouse{}//Abstract home, the following only abstract computer Products example Interface factory{}// Abstract Factory//a Factory Mouse class Mousea implements Producecomputer{mousea () {System.out.println ("Mousea has created");} public void DoSomething () {System.out.println ("Mousea is Running");}} B Factory Mouse class Mouseb implements Producecomputer{mouseb () {System.out.println ("Mouseb has created");} public void DoSomething () {System.out.println ("Mouseb is Running");}} A factory's keyboard class Keyboarda implements Producecomputer{keyboarda () {System.out.println ("Keyboarda has created");} public void DoSomething () {System.out.println ("Keyboarda is Running");}} B Factory Keyboard class Keyboardb implements Producecomputer{keyboardb () {System.out.println ("Keyboardb has created");} public void DoSomething () {System.out.println ("Keyboardb is Running");}} A factory, the production of a factory product class Factorya implements Factory{public Mousea Producemousea () {return new Mousea ();} Public Keyboarda Producekeyboarda () {return new Keyboarda ();}} b factory, production of B factory Product class Factoryb IMPlements factory{public mouseb Producemouseb () {return new Mouseb (); Public Keyboardb Producekeyboardb () {return new Keyboardb ();}} Test class public class Abstractfactory {public static void main (string[] args) {Mousea ma = new Factorya (). Producemousea (); MA.D Osomething (); Keyboarda ka = new Factorya (). Producekeyboarda (); ka.dosomething (); Mouseb MB = new Factoryb (). PRODUCEMOUSEB (); mb.dosomething (); Keyboardb kb = new Factoryb (). Producekeyboardb (); Kb.dosomething ();}}
The following is excerpted from it:
Difference:
The factory method pattern has only one abstract product class, and the abstract factory pattern has multiple.
Factory-mode-specific factory classes can only create instances of a specific product class, while abstract Factory mode may create multiple.
Factory method creates "one" product, his focus is "
How to create"That is, if you develop, your large amount of code is likely
These details are initialized around the construction of this product. Because of this, there are many features that can be reused between similar products,
So it will be with the template method.
Abstract factories need to create some column products, focusing on "
Create which"On the product, that is to say, if you develop, your main task
is to divide different product lines and try to keep each product line interface consistent so that it can inherit from the same abstract factory.
For Java, most of the abstract factory patterns you can see are this:
---its inside is a bunch of factory methods, each factory method returns some type of object.
For example, the factory can produce mouse and keyboard. Then the object of the abstract factory's implementation class (one of its specific subclasses) can produce the mouse
and keyboards, but maybe factory A produces Logitech keyboards and mice, Factory B is Microsoft's.
So A and B are factories, corresponding to the abstract factory;
Each factory produces the mouse and the keyboard is the product, corresponds to the factory method;
Using the factory method mode, you replace the factory method that generates the keyboard, you can switch the keyboard from Logitech to Microsoft. But with an abstract factory model,
, you can replace the mouse and keyboard set at the same time as you change your factory. If you want to have dozens of products, of course, with an abstract factory model
One-time replacement is the most convenient (this factory will use the corresponding factory method)
So the abstract factory is like a factory, and the factory method is like a product line of a factory.
Factory method mode and abstract Factory mode