Abstract Factory mode-and-factory method mode differences

Source: Internet
Author: User
Tags logitech keyboard

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. So the abstract factory implementation class (a specific subclass of it) objects can produce the mouse and keyboard, but Factory A may produce the Logitech keyboard and mouse, factory B is from Microsoft.

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. But with the abstract factory model, you only need to change the factory, you can replace the mouse and keyboard at the same time. If you want dozens of products, it is most convenient to replace all of them with the abstract factory model (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.
---------------------------------
The "Abstract Factory" mode depends on the "Factory method" mode. Therefore, the abstract factory emphasizes the previous verb "abstraction", that is, the "action or design" that you extract from the factory method in the factory method mode is the "abstract engineering" mode.
---------------------------------
The factory method is for a product level structure.
Abstract Factory is designed for the hierarchical structure of multiple products.
---------------------------------
Factory method mode: an abstract product class that can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
Each factory class can only create one instance of a specific product class.
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.

Difference: 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.
---------------------------------
1. If a back garden only grows vegetables, you can use a simple factory.
2. If there are a wide variety of vegetables in the back garden, you must use the factory method to abstract the common things.
3. if we want to expand the scale of the back garden, such as one in the north and one in the south, the factory method will not be implemented, so we should use abstract factories to put all kinds of plants, it also forms a back garden.
Therefore, I personally think that a simple factory is a factory that produces only one type of products and faces specific classes. The factory method can produce different products and abstract public methods, then create a variety of products. abstract Factory separates several products into common items and abstracts mutually dependent objects. as long as these interfaces are implemented, different products can be obtained.
Example:
1. Simple Factory:

Using system;
Public interface ICAR
{
Void run ();
}

Public class bmwcar: ICAR
{
Public void run ()
{
Console. writeline ("bmwcar Run ");
}
}

Public class benzcar: ICAR
{
Public void run ()
{
Console. writeline ("benzcar Run ");
}
}

Public class driver
{
Public static ICAR drivercar (string cartype)
{
Switch (cartype)
{
Case "bmwcar": return New bmwcar ();
Case "benzcar": return New benzcar ();
Default: Throw new exception ();
}
}
}

Public class client
{
Public static void main ()
{
ICAR mycar = driver. drivercar ("benzcar ");
Mycar. Run ();
Console. Read ();
}
}

Experience: The advantage is that different vehicle running methods can be implemented only by implementing the common interface, but the disadvantage is to determine which type of vehicle, resulting in the need to modify the driver class.
2. Factory method:

Using system;
Public interface ICAR
{
Void run ();
}

Public class bmwcar: ICAR
{
Public void run ()
{
Console. writeline ("bmwcar Run ");
}
}

Public class benzcar: ICAR
{
Public void run ()
{
Console. writeline ("benzcar Run ");
}
}
Public abstract class driver
{
Public abstract ICAR drivercar ();
}

Public class bmwdriver: Driver
{
Public override ICAR drivercar ()
{
Return new bmwcar ();
}

}
Public class benzdriver: Driver
{
Public override ICAR drivercar ()
{
Return new benzcar ();
}
}

Class Client
{
Public static void main ()
{
Driver mydriver = new benzdriver ();
ICAR mycar = mydriver. drivercar ();
Mycar. Run ();
Console. Read ();
}
}

 

Experience: The advantage is that it complies with the open-closed principle (OCP) and does not show any disadvantages as a whole.

3. Abstract Factory:

Using system;

Public interface ibusinesscar
{
Void run ();
}

Public interface isportcar
{
Void run ();
}

Public class bmwbusinesscar: ibusinesscar
{
Public void run ()
{
Console. writeline ("bmwcar Run ");
}

}

Public class benzbusinesscar: ibusinesscar
{
Public void run ()
{
Console. writeline ("benzbusinesscar Run ");
}
}

Public class bmwsportcar: isportcar
{
Public void run ()
{
Console. writeline ("bmwsportcar Run ");
}
}

Public class benzsportcar: isportcar
{
Public void run ()
{

Console. writeline ("benzsportcar Run ");
}
}

Public interface idriver
{
Ibusinesscar businesscardriver ();
Isportcar sportcardriver ();
}

Public class bmwdriver: idriver
{
Public ibusinesscar businesscardriver ()
{
Return new bmwbusinesscar ();
}
Public isportcar sportcardriver ()
{
Return new bmwsportcar ();
}
}

Public class benzdriver: idriver
{
Public ibusinesscar businesscardriver ()
{
Return new benzbusinesscar ();
}

Public isportcar sportcardriver ()
{
Return new benzsportcar ();
}
}

Class Client
{
Public static void main ()
{
Idriver mydriver = new benzdriver ();
Isportcar mycar = mydriver. sportcardriver ();
Mycar. Run ();
Console. Read ();
}

}

From: http://blog.csdn.net/wangwenhui11/archive/2009/03/04/3955125.aspx

Experience: abstract methods seem to have reached the perfect realm. abstract The public methods of drivers and drivers of BMW, and create different classes for different drivers. Then, you can add drivers of different cars as needed. the only thing they have in common is driving.

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.