Design mode first

zhaozj2021-02-17  77

Design mode first

Yang Hengxian (yanghx@70345.com)

Introduction:

This article describes a often used design pattern ------ factory model. And how to design a class factory through the UML inside Eclipse.

(About the main design pattern in the design mode, I will introduce the use inside Java (Tool Eclipse), all original.)

Now COM, COM is widely used in factory model in design mode. When you learn Java in-depth learning, it will find that in Java usage plant mode will make the program easier to debug.

Let's take a specific introduction to how to use the factory model to simplify your development.

1. What is factory model?

It is simple to say that the factory model is equivalent to New's New's function. He is archived according to the phenomenon in the actual life. One product must be generated in a factory. It is used in programming, an object must be Produced by an object factory.

Typical UMLs are as follows:

2. Why is the factory model so common?

Because the plant mode is equivalent to the new instance object, we often generate instance objects according to class Class, such as A a = new a () factory model is also used to create instance objects, so you will have multiple minds when New. Can I consider practical factory models, although doing this, may do more work, but will give you more scalability and minimal modifications.

Factory model

3. Basic architecture

The program framework is as follows:

The general plant mode is the use of factory-class static functions to generate an interface object implemented, so the factory class has a static function, and the object being generated has an interface.

Typical class diagram:

4. Use range

When encountering the following case, developers can consider using factory models:

1. Unfared instances that need to create which kind of instance you need to create when encoding.

2. A class uses its subclass to create an object.

3. Developers do not want to create which class of instances and how to create instances are exposed to external programs.

4. The base class can be an abstract class. In this case, the factory class must return a non-abstract class.

5. The base class provides some default methods, and only these methods are renovated in subclasses when these default methods cannot meet special needs.

6. You can decide which subclass of the item that should be returned directly by passing parameters to the factory class.

Below we combine UML tools in Eclipse to display the usage process.

We use the design of database operation as an example:

UML PLUG-IN can now be used by Eclipse. Typical by Eclipseuml, Togetherec.

Database operation function requirements:

1. Adapt to different databases

2. Adapt to different connects to the same database

3. Treatment with the database is related to the database

We use factory class

First, design a function function of database operations

Package com.jp.nissan.dealer.practice.db;

Import java.sql. *;

Import java.util.list;

Public interface ibasedatabase {

/ **

* Do not support the operation of the stored procedure

* /

Connection getConnection () throws Exception;

Void disconnect ();

Void Runsql (String Strsql).

Void Runsql (String strsql, list value) throws SqlexCeption;

ResultSet OpenSQL (String Strsql).

ResultSet OpenSQL (String Strsql, List Values) Throws SqlexCeption;

}

In order to support different databases, we customize a factory class:

Package com.jp.nissan.dealer.practice.db;

Import javax.naming.namingexception; import java.sql.sqlexception;

Public class dbfactory {

Public static ibasedatabase createdb () {

Try {

IBaseDatabase DB = New TestDb2Database ();

Return DB;

}

Catch (Namingexception EX) {

Return NULL;

}

Catch (Sqlexception EX) {

Return NULL;

}

Catch (Exception E) {

Return NULL;

}

}

}

Start design on the UML map

Then let the UML tool generate code, you will find that many framework code has been successfully generated.

Start writing your specific database operation. Use the Java class's inheritance, which reaches low coupling type and reaches high reuse.

At last:

About the database implementation code, you can send a message to me.

Thank you. 2004-2-18

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

New Post(0)