Design Pattern Learning Notes (V)-abstract Factory abstract Factory model

Source: Internet
Author: User
Tags abstract

GOF The book "Design Pattern" describes the abstract factory pattern:

Provides an interface for creating a set of related or interdependent objects, and does not need to specify their specific classes.

The general meaning is that when we create these objects, we do not need to specify their specific classes whose objects are instantiated by the factory object. The following is an example of the book "Design Patterns Explained", where the display and printing of computer systems depends on the current running system. Low-end machines use low-resolution display and print drivers, and high-end machines use high-resolution display and print drivers. The structure of the chart is as follows:

The code is as follows:

Abstract class resfactory{
Abstract public displaydriver getdisplaydrvr ();
Abstract public printdriver getprintdrvr ();
}
Class Lowresfact extends resfactory{
Public Displaydriver Getdisplaydrvr () {
Returnnew Lrdd ();
}
Public Printdriver Getprintdrvr () {
Returnnew LRPD ();
}
}
Class Highresfact extends resfactory{
Public Displaydriver Getdisplaydrvr () {
Returnnew Hrdd ();
}
Public Printdriver Getprintdrvr () {
Returnnew HRPD ();
}
}
Abstract class displaydriver{
}
Abstract class printdriver{
}
Class Hrdd extends displaydriver{
Public Hrdd () {
SYSTEM.OUT.PRINTLN ("Use display driver for high-end machines")
}
}
Class Lrdd extends displaydriver{
Public Lrdd () {
SYSTEM.OUT.PRINTLN ("Display driver with low-end machine");
}
}
Class HRPD extends printdriver{
Public HRPD () {
System.out.println ("Print driver with high-end machine");
}
}
Class LRPD extends printdriver{
Public LRPD () {
System.out.println ("Print driver with low-end machine");
}
}
public class Apcontrol {
public static Resfactory Getresfactory (Resfactory factory) {
return factory;
}
public static void Main (string[] args) {
Resfactory highresfact=apcontrol.getresfactory (New Highresfact ());
Highresfact.getdisplaydrvr ();
Highresfact.getprintdrvr ();
Resfactory lowresfact=apcontrol.getresfactory (New Lowresfact ());
Lowresfact.getdisplaydrvr ();
Lowresfact.getprintdrvr ();
}
}

Output results:

Using a display driver for high-end machines

Print drivers with high-end machines

Use display drivers for low-end machines

Print drivers with low-end machines

In this example, Apcontrol uses objects derived from two different service classes (Displaydriver and Printdriver). This design is very simple, hides the implementation details, and the system is more maintainable. Apcontrol does not know the specific implementation of the service object that it owns because it is the responsibility of the factory to create the object. Apcontrol also does not know which particular factory he is using, because it only knows that he has a Resfactory object. It may be a highresfact or it may be a lowresfact, but it doesn't know exactly which one it is.

Summary: When you must reconcile the creation of a set of objects, you can apply the abstract factory pattern. It provides a way to extract the rules on how to instantiate objects from the client objects that use those objects. First, find the instantiated rule, define an abstract class with interfaces, where the interface provides a method for each object that needs to be instantiated. Then, from this class, you implement specific classes for each group. Finally, the client object decides to use a specific factory to create the desired object. It mainly applies to the following situations:

1 A system is independent of its product creation, composition and presentation.

2 The system can be configured so that the system can use one of several product families.

3 when it is necessary to emphasize the design of a series of related product objects for joint use.

4 when you want to provide a product class library and just want to show their interface instead of implementing it.

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.