Abstract factory model of java design pattern

xiaoxiao2021-03-06  102

Abstract factory model is a mode in which the extent is much higher than the factory model. In short, the abstract factory class and factory class are the same, but the factory class returns the ordinary examples; the abstract factory class returns an example of a factory class.

Abstract Factory Class The most classic application supports multiple GUI interfaces, such as Java programs, supports Windows, Motif, and Macintosh interfaces (this technology is called interface type, "Look-and-Feel). Developers can obtain a GUI factory class corresponding to a certain interface through an abstract factory, and operate the components (such as buttons, text boxes, etc.) on the interface through the GUI factory developer. Starting with Java 1.2, Java provides interface types that implement abstract plant modes in the system layer. When the developer determines the interface type in the program, an instance of the component on the interface can be obtained by the interface type. For example, in this code below, the program obtains the type of operating system and the interface type of the operating system setting program:

String laf = uimanager.getsystemlookandfeelclassname ();

Try {

UIManager.SetLookAndfeel (LAF);

}

Catch (unsupportedLookandfeeElexception EXC)

{

System.err.Println ("Unsupportedl & f:" LAF);

}

Catch (Exception EXC)

{

System.err.Println ("Error Loading" LAF);

}

example

Let's take a look at a simple example, how is the abstract plant mode is used in the application layer. Suppose you bought a residential, there is a garden on the roof, and you need to design a garden layout. There is a flower stage on the right side of the garden, and a plant is needed, and the center needs a substrate plant. According to the style, the garden can be designed as elegant, used and lazy.

First we need a Garden class as a base class, which can get plants planted in the garden through this class:

// Garden.java

Public Abstract Class Garden

{

Public Abstract Plant GetShade (); // Plant in the table

Public Abstract Plant getcenter (); //

Public Abstract Plant GetBorder (); //

}

Then we need to implement a Plant class, saving the name of the plant in this class.

// Plant.java

Public Class Plant

{

String name;

Public Plant (String PNAME)

{

Name = PNAME;

}

Public string getName ()

{

Return Name;

}

}

In practical applications, the plants corresponding to each type of garden should look up from the database. In order to facilitate convenience, the plant type corresponding to each plant type garden is directly written directly in the program:

// ElegantGarden.java (Elegant)

Public Class ElegantGarden Extends Garden

{

Public Plant GetShade ()

{

Return New Plant ("Tulip");

}

Public Plant getcenter ()

{

Return New Plant ("Banyan Tree");

}

Public Plant GetBorder ()

{

Return New Plant ("Islands");

}

}

// practicalgarden.java (practical)

Public class practicalgarden extends garden {

Public Plant GetShade ()

{

Return New Plant ("Grape");

}

Public Plant getcenter ()

{

Return New Plant ("Pomegranate");

}

Public Plant GetBorder ()

{

Return New Plant ("Loofa");

}

}

// LasyGarden.java (lazy)

Public Class Lasygarden Extends Garden

{

Public Plant GetShade ()

{

Return New Plant ("Month");

}

Public Plant getcenter ()

{

Return New Plant ("tea flower");

}

Public Plant GetBorder ()

{

Return New Plant ("Bamboo");

}

}

Now we have a group of Garden's subclasses, each subclass achieves the same method: getShade (), getcenter () and getBorder (). You can now get an example of a garden of a model by delivering special parameters to abstract factory classes:

// GardenMaker.java

Public Class GardenMaker

{

// Abstract factory class

Private garden gd;

Public Garten getGarden (String gtype)

{

GD = new elegantgarden (); // By default

IF (GTYPE.EQUALS ("practical"))

GD = new phacticalgarden ();

IF (GTYPE.EQUALS ("lazy"))

GD = new lasygarden ();

Return GD;

}

}

After running the program, display the following window:

Two parts are included in the interface: the type of garden is selected on the left. When a user selects one of the types, the program obtains an instance of the actual Garden class by abstract factory class MakeGarden.

Public Void ItemStateChanged (ItemEvent E)

{

Checkbox CK = (Checkbox) E.Getsource ();

Garden = new gardenMaker (). getGarden (ck.getlabel ());

ShadePlant = ""; centerplant = ""; borderplant = "

GardenPlot.Repaint ();

}

When the user presses the "middle", "wall" or "flower platform" button, the program returns the Plant object, and then display the name of the Plant object on the interface via the GardenPanel class:

// ----------------------------------

Public Void ActionPerformed (ActionEvent E)

{

Object obj = E.GETSource ();

IF (Obj == Center)

setcenter ();

IF (Obj == Border)

setBorder ();

IF (Obj == Shade)

Setshade ();

IF (Obj == quit)

System.exit (0);

}

// ----------------------------------

Private void setcenter () {

IF (garden! = null) centerplant = garden.getCenter (). getname ();

GardenPlot.Repaint ();

}

Private void setborder ()

{

IF (garden! = null) borderplant = garden.getBorder (). getname ();

GardenPlot.Repaint ();

}

Private void setshade ()

{

IF (Garden! = null) shadeplant = garden.getshade (). getname ();

GardenPlot.Repaint ();

}

The soft code of other classes in the program is as follows:

// Gardener.java

Import java.awt. *;

Import java.awt.event. *;

Public Class Gardener Extends Frame

Implements anctionsListener, ItemListener

{

Private Checkbox Elegant, Practical, LasyBoy

Private Button Center, Border, Shade, Quit;

Private garden garden = NULL;

Private GardenPanel GardenPlot;

Private string borderplant = "" ", centerplant =", shadeplant = "";

Public gardener ()

{

Super ("Roof Garden Planning");

SetGui ();

}

// ----------------------------------

Private void setgui ()

{

SetBackground (color.lightgray);

SetLayout (New GridLayout (1, 2));

Panel Left = new panel ();

Add (Left);

Panel Right = New Panel ();

Add (Right);

Left.setLayout (New GridLayout (4, 1));

Left.add (New Label ("Garden Type");

CheckboxGroup GRP = New CheckboxGroup ();

Elegant = New Checkbox ("Elegant", GRP, FALSE

Practical = New Checkbox ("Practical", GRP, FALSE

LasyBoy = New Checkbox ("Lazy", GRP, FALSE;

Left.add (elegu);

LEFT.ADD (Practical);

Left.add (layyboy);

Elegant.Additemlistener (this);

LasyBoy.Additemlistener (this);

Practical.Additemlistener (this);

Right.setLayout (New GridLayout (2, 1));

GardenPlot = New GardenPanel ();

GardenPlot.SetBackground (Color.White);

Panel botright = new panel ();

Right.add (GardenPlot);

Right.Add (Botright);

Center = New Button; Border = New Button ("Wallside");

Shade = New Button ("Hua Tai");

Quit = New Button ("Exit");

Botright.add (center);

Center.AddActionListener (this);

Botright.add (border);

Border.addActionListener (this);

Botright.add (shade);

Shade.AddActionListener (this);

Botright.add (quit);

Quit.AddActionListener (this);

SetBounds (200, 200, 400, 300);

SetVisible (TRUE);

}

// ----------------------------------

Public Void ActionPerformed (ActionEvent E)

{

Object obj = E.GETSource ();

IF (Obj == Center)

setcenter ();

IF (Obj == Border)

setBorder ();

IF (Obj == Shade)

Setshade ();

IF (Obj == quit)

System.exit (0);

}

// ----------------------------------

Private void setcenter ()

{

IF (garden! = null) centerplant = garden.getCenter (). getname ();

GardenPlot.Repaint ();

}

Private void setborder ()

{

IF (garden! = null) borderplant = garden.getBorder (). getname ();

GardenPlot.Repaint ();

}

Private void setshade ()

{

IF (Garden! = null) shadeplant = garden.getshade (). getname ();

GardenPlot.Repaint ();

}

// ----------------------------------

Public Void ItemStateChanged (ItemEvent E)

{

Checkbox CK = (Checkbox) E.Getsource ();

Garden = new gardenMaker (). getGarden (ck.getlabel ());

ShadePlant = ""; centerplant = ""; borderplant = "

GardenPlot.Repaint ();

}

// ----------------------------------

Static Public Void Main (String Argv [])

{

New gardener ();

}

// --------------------------------

Class GardenPanel Extends Panel

{

Public void Paint (Graphics G)

{

Dimension SZ = getSize ();

g.setcolor (color.lightgray);

G.fillarc (0, 0, 80, 80, 0, 360);

g.setcolor (color.ble);

g.drawRect (0, 0, Sz.width-1, Sz.Height-1);

g.drawstring (Centerplant, 100, 50);

G. DrawString (Borderplant, 75, 120);

g.drawstring (Shadeplant, 10, 40);

}

}

} // Gardener.java

summary

One main reason for using abstract factory model is due to it can be completely divided into realization and their generation process. Implementation classes are hidden in the factory class, and the caller does not have to know any information to implement the class. Due to this characteristic, developers are also easy to replace in a set of implementations. Although subclasses in abstract plants have inherited the same base class, they can also have different methods from other subclasses. For example, in the ElegantGarden class, we can add the properties of the flower stand and rockery and how to obtain both attributes; and in other garden classes do not need to include both properties. Of course, in the program, it is necessary to judge whether the current instance supports these special methods, and there are two solutions: one is to define all methods including special methods in the base class, only support for special methods. Implement these special methods; another solution is to determine the type of implementation class in an abstract class:

IF (Gard InstanceOf ElegantGarden)

String Rock = gard.getrockery (); // Get a rockery name

转载请注明原文地址:https://www.9cbs.com/read-103648.html

New Post(0)