Factory method mode And abstract factory mode, mode and Abstract Factory

Source: Internet
Author: User
Tags logitech keyboard

Factory method mode And abstract factory mode, mode and Abstract Factory

I. Factory method model

The defect of the simple factory mode is that the creation of classes is too dependent on the factory. Once the program needs to be extended, the code of the factory class must be modified.

This violates the backpack principle.

The factory method mode has been improved to address 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 mode is that once the function is extended, you only need to add a class to implement the Sender interface, and then create a factory class to implement the producer interface.
The original factory is retained, which ensures that it does not violate the closure principle and further improves scalability.


Ii. Abstract Factory Model

The factory method mode and the abstract factory mode are difficult to clearly distinguish

Factory method mode:

An abstract product class that can derive many specific product classes
An abstract factory class 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 be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
You can create multiple product instances for each specific factory class, that is, multiple products under a product line.

Package day03; interface ProduceComputer {}// abstract computer product interface ProduceHouse {}// abstract home, the following uses only the abstract computer product example interface Factory {} // Abstract Factory // class MouseA implements ProduceComputer {MouseA () {System. out. println ("MouseA has created");} public void doSomething () {System. out. println ("MouseA is running") ;}// class MouseB implements ProduceComputer {MouseB () {System. out. println ("MouseB has created");} public void doSomething () {System. out. println ("MouseB is running") ;}// keyBoardA implements ProduceComputer {keyBoardA () {System. out. println ("keyBoardA has created");} public void doSomething () {System. out. println ("keyBoardA is running") ;}// keyBoardB implements ProduceComputer {keyBoardB () {System. out. println ("keyBoardB has created");} public void doSomething () {System. out. println ("keyBoardB is running") ;}// Factory A, which produces the class FactoryA implements Factory {public MouseA produceMouseA () {return new MouseA ();} public keyBoardA produceKeyBoardA () {return new keyBoardA () ;}// Factory B, which produces the 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. doSomething (); keyBoardA ka = new FactoryA (). produceKeyBoardA (); ka. doSomething (); MouseB mb = new FactoryB (). produceMouseB (); mb. doSomething (); keyBoardB kb = new FactoryB (). produceKeyBoardB (); kb. doSomething ();}}


Here is an excerpt from it:
Differences:

The factory method mode has only one abstract product class, while the abstract factory mode has multiple.
The factory method mode can only create one instance for a specific product type, while the abstract factory mode can create multiple instances.
Factory method to create a "product, his focus is" How to create", That is to say, if you develop a lot of code, it is very likely that
The above details are initialized around the construction of such products. Because of this, similar products have many reusable features,
Therefore, it will be consistent with the template method.

Abstract Factory needs to create some products with emphasis on" Created"Product, that is, if you develop, your main task
Is to divide different product lines, and try to keep the interfaces of each product line consistent, so that they can be inherited from the same abstract factory.
For java, most of the abstract factory patterns you can see are as follows:
--- There are a bunch of factory methods in it, and each factory method returns some type of object.
For example, the factory can produce mouse and keyboard. Then, the object of the abstract factory implementation class (a specific subclass of it) can produce the mouse
And keyboard, but maybe Factory A produces the Logitech keyboard and mouse, and factory B is Microsoft's.

In this way, A and B are factories, which correspond to abstract factories;
The mouse and keyboard produced by each factory are products, corresponding to the factory method;

With the factory method mode, you can replace the factory method that generates the keyboard to switch the keyboard from Logitech to Microsoft. However, the abstract factory model is used.
You only need to change the factory, you can replace the mouse and keyboard at the same time. If you want dozens of products, use abstract factory models.
It is most convenient to replace all of them at a time (this factory will use the appropriate factory method for you)
The abstract factory is like a factory, and the factory method is like a product production line of a factory.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.