Plant model of Java design pattern

zhaozj2021-02-16  54

Java Design Patterns Part of the factory pattern: Feng Rui article taken from: CCID March 7, 2003 in object-oriented programming, it is a factory pattern is often used to model. The class implemented according to the factory mode can generate an instance of a class in a set of classes in a set of categories according to the supplied data, which typically has a public abstract parent class and implements the same method, but these methods are for different data. Different operations.

The principle of factory model

In order to understand the plant mode, if you work, let's take a look at the picture 1:

Figure 1 Working principle of factory model

In Figure 1, X is the base class, XY and XZ inherited the X class. The factory class can determine the instance of the subclass according to the data passed to it. A GetClass method is defined on the right, which requires parameter a and returns an instance of a X class. For programmers, it is not important to return XY or XZ, because they have the same way, but the internal implementation of these methods is different.

A simple example

Under what circumstances will it be used to plant mode? Let us look at a simple example. In some online questionnaires, you often need to fill in the name. Some people fill in the last name in front, name is behind (for example, the Chinese fill in the habit of the name); and some people use the "name, surname" or "surname, name" format (most cultures in the Western culture Fill in the name). Now let us assume whether it contains "," and spaces in the name of the name, it can be judged to be the surname in front. Let's first define a base class name:

Class name {

protected string last; // surname

protected string first; //

Public string getFirst () {

Return first;

}

Public string getLast () {

Return Last;

}

}

In the base class, we save your name in two different variables and provide a getFirst () and getLast () method. Since subclasses need to use variables to be stored, we set them to protected. Now we can achieve two categories to distinguish the two situations mentioned above. In the WitHoutComma class, we assume that there is no space in the read string, the first character is a last name, the rest of the characters are named; the first space is called before, and then the last name:

Class withoutcomma extends namer {

Public withoutcomma (string s) {

INT i = s.lastIndexof ("");

IF (i> 0) {

// Space left is named

First = s.substring (0, i) .trim ();

// Space right is the last name

Last = S.SUBSTRING (i 1) .trim ();

}

Else {

// There is no space, the first character is a last name, and the next character is named.

Last = s.substring (0, 1);

First = s.substring (1) .trim ();

}

}

}

The code is as follows for the case where the name is included.

Class withcomma extends namer {

Public withcomma (string s) {

INT i = s.indexof (",");

IF (i> 0) {

// The left side of the comma is the surname

Last = s.substring (0, i) .trim ();

/ / The name is named on the right.

First = s.substring (i 1) .trim ();

Else {

/ / There is no comma, use the string as the name, the last name is empty

Last = S;

FigSt = ""

}

}

}

Next, the factory class is needed. In the factory class, we only need to generate different classes of instances based on whether there is a comma in the input name.

Class namefactory {

Public Name GetName (String Entry) {

INT i = entry.indexof (","); // Detects if there is ","

IF (i> 0)

Return New withcomma (Entry);

Else

Return new withoutcomma (entry);

}

}

We have passed the examples of Figure II to see how to use factory classes in the program:

Figure 2 Factory mode test procedure

In the program, you first need to initial chemical plants:

NameFactory nfactory = new namefactory ();

Then when the "Name" button is pressed, call the computename () method, and the method is called the factory class GetName () method to get the name of the NAMER, and the last name is displayed in the corresponding text box:

Name NMR;

NMR = nf.getname (jtextfieldname.getText ());

JtextFieldFirstName.Settext (NMR.GetFirst ());

JtextFieldLastName.Settext (NMR.Getlast ());

The source code of the program is as follows:

Public class test extends javax.swing.jframe {

PRIVATE NAMEFACTORY NF;

Public test () {

NF = new namefactory ();

INITComponents ();

}

Private vidinnetic () {

JPanel1 = new javax.swing.jpanel ();

JPanel2 = new javax.swing.jpanel ();

Jlabel1 = new javax.swing.jlabel ();

JtextfieldName = new javax.swing.jtextfield ();

JPanel3 = new javax.swing.jpanel ();

JPanel4 = new javax.swing.jpanel ();

Jlabel2 = new javax.swing.jlabel ();

JtextfieldLastName = new javax.swing.jtextfield ();

Jlabelfirstname = new javax.swing.jlabel ();

JtextFieldFirstName = new javax.swing.jtextfield ();

JPanel5 = new javax.swing.jpanel ();

JPanel6 = new javax.swing.jpanel ();

JButtonCompute = new javax.swing.jbutton ();

JButtonClear = new javax.swing.jbutton ();

JButtoneXit = new javax.swing.jbutton ();

Settitle ("/ U5DE5 / U5382 / U6A21 / U5F0F / U6D4B / U8BD5 / U7A0B / U5E8F"); addwindowlistener (new java.awt.event.windowadapter () {

Public void windowclosing (java.awt.event.windowEvent evt) {

EXITFORM (EVT);

}

});

JPanel1.setLayout (New Java.awt.GridLayout (5, 1));

JPanel2.setLayout (New Org.NetBeans.lib.awTextra.absoluteLayout ());

Jlabel1.Settext ("/ U8F93 / U5165 / U59D3 / U540D");

JPanel2.Add (jlabel1, new org.netbeans.lib.awtextra.absoluteconstraints (50,

20, -1, -1)));

JtextfieldName.AddActionListener (new java.awt.event.ActionListener () {

Public void actionperformed (java.awt.event.ActionEvent evt) {

JtextfieldNameActionPerformed (EVT);

}

});

JPanel2.Add (jtextfieldname, New

Org.NetBeans.lib.awtextra.absoluteconstraints (150, 10, 150, 40);

JPanel1.Add (jPanel2);

JPanel1.Add (jPanel3);

JPanel4.setLayout (New Java.awt.GridLayout (2, 2));

Jlabel2.sethorizontalaLAlignment (javax.swing.swingconstants.center);

Jlabel2.Settext ("/ U59D3:");

JPanel4.Add (jlabel2);

JPanel4.Add (jtextfieldlastname);

Jlabelfirstname.sethorizontalalignment (javax.swing.swingconstants.center);

Jlabelfirstname.Settext ("/ U540D:");

Jlabelfirstname.SetTooltiptext ("null");

JPanel4.Add (jlabelfirstname);

JPanel4.Add (jtextfieldfirstname);

JPanel1.Add (jPanel4);

JPanel1.Add (jPanel5);

JButtonCompute.setText ("/ U83B7 / U5F97 / U59D3 / U540D");

JButtonCompute.AddActionListener (new java.awt.event.ActionListener () {

Public void actionperformed (java.awt.event.ActionEvent evt) {

JButtonComputeActionPerformed (EVT);

}

});

JPanel6.Add (jButtonCompute);

JButtonClear.Settext ("/ U6E05 / U7A7A);

JButtonClear.AddActionListener (new java.awt.event.actionListener () {

Public void actionperformed (java.awt.event.ActionEvent evt) {

JButtoncleActionPerformed (EVT);

});

JPanel6.Add (jButtonClear);

JButtoneXit.Settext ("/ U9000 / U51FA);

JButtonexit.AddActionListener (new java.awt.event.ActionListener () {

Public void actionperformed (java.awt.event.ActionEvent evt) {

JButtoneXitActionPerformed (EVT);

}

});

JPanel6.Add (jbuttonexit);

JPanel1.Add (jPanel6);

GetContentPane (). add (jpanel1, java.awt.borderlayout.center);

Pack ();

}

Private void jbuttoncomputeactionperformed (java.awt.event.ActionEvent evt) {

Name NMR;

NMR = nf.getname (jtextfieldname.getText ());

JtextFieldFirstName.Settext (NMR.GetFirst ());

JtextFieldLastName.Settext (NMR.Getlast ());

}

Private void jbuttoneXitActionPerformed (java.awt.event.ActionEvent evt) {

System.exit (0);

}

Private void jbuttoncleactionperformed (java.awt.event.ActionEvent evt) {

JtextFieldName.SetText ("" ");

JtextFieldFirstName.Settext ("");

JtextFieldLastName.Settext ("");

}

Private void jtextfieldNameActionPerformed (java.awt.event.ActionEvent evt) {

}

Private void exitform (java.awt.event.windowevent evt) {

System.exit (0);

}

Public static void main (string args []) {

New test (). show ();

}

Private javax.swing.jpanel jpanel6;

Private javax.swing.jpanel jpanel5;

Private javax.swing.jbutton jbuttonexit;

Private javax.swing.jpanel jpanel4;

Private javax.swing.jpanel jpanel3;

Private javax.swing.jpanel jpanel2;

Private javax.swing.jpanel jpanel1;

Private javax.swing.jbutton jbuttonclear;

Private javax.swing.jtextfield jtextfieldname;

Private javax.swing.jtextfield jtextfieldlastname;

Private javax.swing.jbutton jbuttoncompute;

Private javax.swing.jtextfield jtextfieldfirstname;

Private javax.swing.jlabel jlabelfirstname;

Private javax.swing.jlabel jlabel2;

Private javax.swing.jlabel jlabel1;

}

Through the above example we can see the most basic development process in the factory model. First, you need to define a base class that the subclass of the class implements the method in the base class through a different method. Then need to define a factory class, and the factory class can generate different subclass examples depending on the conditions. When an example of a subclass is obtained, the developer can call the method in the base class without having to consider the instance of which a subclass is returned.

Factory model scope

When encountering the following, developers can consider using factory modes: • Unfair instances that need to be created when encoding. · A class uses its subclass to create an object. · Developers don't want to create which class of instances and how to create instances are exposed to external programs. In addition to the examples mentioned above, the implementation of the factory model also allows some small changes, for example: · The base class can be an abstract class, in which case the factory class must return a non-abstract class. The base class provides some default methods, and only these methods are renovated in subclasses when these default methods do not meet special needs. · Which subclass of which should be returned directly by passing parameters to the factory class.

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

New Post(0)