Design Pattern Introduction and Practice

zhaozj2021-02-16  52

Design Pattern Practice 1. Program from a simple multi-column sorted example, explained the purpose, analysis and practice of Design Pattern (design mode) by shallow to deeply. The examples in the article uses Compositor Pattern and Proxy Pattern (Or Decorator Pattern). (Also, Simple Visitor, Functor) At the same time, the examples in the article also provide solutions for a class of issues (conditional combination issues). 2. The introduction of the problem of the problem of Design Pattern (design mode) is to separate the constant parts and changes in the common problem. The constant portion constitutes the Design Pattern (design mode). This is some like this and the framework. The following is a sorted example, indicating how the constant portion in the problem is extracted. Suppose a Java class RECORD has fields such as Field1, Field2, Field3.

Java code:

public

Class Record

{

public

INT field1;

Public long far2;

public

Double filed3;

}

We also have an array of RECORD objects (] Records. We need to sort this array in different conditions. First, sort order sequence from small to large in size from the size of Field1. The sort function is as follows:

Java code:

Void Sort

(Record

[

] Records

)

{

for

(

INT i = ....

)

{

for

(

INT j = ....

)

{

IF

(Records)

[i

].

FIELD1> Records

[J

].

Field1

)

// Swap Records [i] and records [J]

}

}

}

Second, sort order is sorted from small to large according to the size of Field2.

Java code:

Void Sort

(Record

[

] Records

)

{

for

(

INT i = ....

)

{

for

(

INT j = ....

)

{

IF

(Records)

[i

].

FIELD2> Records

[J

].

Field2

)

// Swap Records [i] and records [J]

}

}

}

Again, sort order sequencing from small to large in size from the size of the field. This requirement is too much, we wrote too much repetition code. We can see that the change in the problem is only the conditional part (the IF condition judgment statement of the black body). We can introduce a Comparator interface and extract the part of this change.

Java code:

public

Interface

Comparator

(

)

{

public

Boolean Greaterthan

(RECORD A, RECORD B

);

}

The Sort function can be written like this (the judgment condition is used as a parameter):

Void Sort

(Record

[

] Records,

Comparator Compare

)

{

for

(

INT i = ....

)

{

for

(

INT j = ....

)

{

IF

COMPARE.

Greaterthen

(Records)

[i

], RECORDS

[J

]

)

)

// Swap Records [i] and records [J]

}

}

}

Thus, corresponding to the first requirement - sorting the RECORDS array in size in the size of Field1. We can make a CompareByfield1 class that implements the Comparator interface.

Java code:

public

Class comparebyfield1

IMPLEMENTS

Comparator

{

public

Boolean Greaterthan

(RECORD A, RECORD B

)

{

IF

(a.

Filed1> b.

Filed1

)

{

Return Ture;

}

Return

False;

}

}

The call to the Sort function is: Sort (Records, New CompareByfield1 ()); this, corresponding to the first requirement - sorting the RECORDS array in size in the size of Field2. We can do a CompareByfield2 class that implements the Comparer interface.

Java code:

public

Class comparebyfield2

IMPLEMENTS

Comparator

{

public

Boolean Greaterthan

(RECORD A, RECORD B

)

{

IF

(a.

Filed2> b.

Filed2

)

{

Return Ture;

}

Return

False;

}

}

The call of the Sort function is: sort (Records, New CompareByfield2 ()); according to C STL called, the Sort is called algorithm, refords is called a container (collection), and Comparator is called function object (Function Object) . JDK's Java.util.Collections class sort method and java.util.comParetor interface is designed in this way. Let's take a look at how to apply Sort and Comparator to solve multiple column sorting problems. 3. Multi-column Sort Problem 3.1 Sorting Conditions We know that the SQL statement can achieve a powerful sorting function, can be sorted in accordance with the arrangement of different fields, and can be sorted in descending sequencing. For example, the following statement. ORDER by Field1 ASC, Field2 ASC, Field3 DESC. This sort condition is sorted by the ascending order of Field1, the ascending order of Field2, sorting the descending order of Field3. Note that the top field has a high priority. For example, two records A and B are met: (1) a.field1> B.field1, (2) a.field2 B. If (1) a.field1> B.field1 changes in the above conditions to a.field1 == B.field1. At this time, the conditions (2) will work. At this time, A

According to this analysis, we introduce two classes, ReverseComparator classes, and CompositeComparator classes. The CompositeComparator class is used to solve the combination of fields. The ReverseComparator class is used to solve the ascending and descending of the fields. 3.3 ReverseComparator class code Java code:

Import Java.

Util.

Comparator;

public

Class ReverseComparator

IMPLEMENTS

Comparator

{

/ ** the Original Comparator * /

Private

Comparator OriginalComparator =

NULL;

/ ** Constructor Takes a Comparator As Parameter * /

Public ReverseComparator

(

Comparator Comparator

)

{

ORIGINALCOMPARATOR = Comparator;

}

/ ** REVERSE The Result of the Original Comparator * /

public

int ComPare

(

Object O1,

Object O2

)

{

Return - OriginalComparator.

Compare

(O1, O2

);

}

}

3.4 CMPARATOR Class code

Java code:

Import Java.

Util.

Comparator;

Import Java.

Util.

Iterator;

Import Java.

Util.

List;

Import Java.

Util.

LINKEDLIST;

public

Class CompositeComparator

IMPLEMENTS

Comparator

{

/ ** in the condition list, Comprators' Priority Decrease from head to tail * /

Private

List comparators =

New

LinkedList

(

);

/ ** Get The Comprators, You CAN MANIPULATE IT AS NEED. * /

public

List getcomparators

(

)

{

Return Comprators;

}

/ ** Add a batCh of comprators to the condition list * /

public

Void Addcomparators

(

Comparator

[

] Comparatorarray

)

{

IF

(comparatorarray ==

NULL

)

{

Return;

}

for

(

INT i =

0; i

Length; i

)

{

Comprators.

Add

(Comparatorarray)

[i

]

);

}

}

/ ** Compare by the priority * /

public

int ComPare

(

Object O1,

Object O2

)

{

for

(

Iterator itrator = comprators.

Iterator

(

Iterator.

Hasnext

(

);

)

{

Comparator Comparator =

(

Comparator

Iterator.

NEXT

(

);

int result = comparator.compare

(O1, O2

);

IF

(Result! =

0

)

{

Return Result;

}

}

Return

0;

}

}

3.5 Combination Application Application This section describes the usage of two classes above. For the previous sorting problem, we only need 3 Comparator classes: (1) Field1comaprator; (2) Field2comaprator; (3) Field3comaprator. The following is an example, how to combine these Comparator to achieve different sort conditions. (1) ORDER BY FIELD1, FIELD2

Java code:

Compoicomparator mycomparator =

New Compoicomparator

(

);

Mycomparator.

Addcomparators

(

New

Comparator

[

]

{

New Field1comaprator

(

),

New Field2comaprator

(

)

}

);

// Records is a list of record

COLLECTIONS.

Sort

(Records, Mycomparator

);

(2) Order by Field1 DESC, FIELD2

Java code:

Compoicomparator mycomparator =

New Compoicomparator

(

);

Mycomparator.

Addcomparators

(

New

Comparator

[

]

{

New ReverseComparator

(

New Field1comaprator

(

)

),

New Field2comaprator

(

)

}

);

// Records is a list of record

COLLECTIONS.

Sort

(Records, Mycomparator

);

The ReverseComparator class and the CompositeComparator class provided here have used Decorator Pattern. The CompositeComparator class is also Composite Pattern. 4. The arrangement of filtration conditions (Thank you shinwell refers to the following code) Filtering condition issues are also a category of conditional combination. For example, the Java.IO.File class provided by JDK provides a file filtering method Listfile (filefilter), users can customize different FileFilters, implement different filtering conditions, such as file time within a certain range; file compact name, file name Some mode; is a directory, or a file, and so on. Similarly, we can apply the above solution to achieve a flexible filtering condition combination - with a CompositeFilter class with any combination of filtering conditions, use a ReverseFilter class as the exclusion condition. 4.1 Code of CompositeFilter class

Java code:

Import Java.

IO.

Filefilter;

Import Java.

IO.

File;

Import Java.

Util.

Iterator;

Import Java.

Util.

List;

Import Java.

Util.

LINKEDLIST;

public

Class CompositeFilter

IMPLEMENTS

FILEFILTER {

/ ** in the filter list, every situation shouth be met. * /

Private

List filters =

New

LinkedList

(

);

/ ** Get the filters, you can manipulate it as need. * /

public

List getfilters

(

)

{

Return Filters;

}

/ ** Add a batCh of filters to the condition list * /

public

Void Addfilters

(

FILEFILTER

[

] Filterarray

)

{

IF

(Filterarray ==

NULL

)

{

Return;

}

for

(

INT i =

0; i

Length; i

)

{

Filters.

Add

(Filterarray)

[i

]

);

}

}

/ ** Must Meet All The Filter Condition * /

public

Boolean Accept

(

File pathname

)

{

for

(

Iterator itrator = filters.

Iterator

(

Iterator.

Hasnext

(

);

)

{

FILEFILTER FILTER =

(

FILEFILTER

Iterator.

NEXT

(

);

Boolean Result = filter.

ACCEPT

(Pathname

);

// if any condition can not be met, returnaf.

IF

(Result ==

False

)

{

Return

False;

}

}

// All conditions are Met, Return True.

Return

True;

}

}

4.2 ReverseFilter class code

Java code:

Import Java.

IO.

Filefilter;

Import Java.

IO.

File;

public

Class ReverseFilter

IMPLEMENTS

FILEFILTER

{

/ ** The Original Filter * /

Private

FILEFILTER OriginalFilter =

NULL;

/ ** Constructor takes a filter as parameter * /

Public ReverseFilter

(

FILEFILTER FILTER

)

{

Originalfilter = filter;

}

/ ** Must Meet All The Filter Condition * /

public

Boolean Accept

(

File pathname

)

{

Return! OriginalFilter.

ACCEPT

(Pathname

);

}

}

5. Summary This article tells the analysis and practice of Design Pattern, and elaborates the solution of a class of conditions for conditions.

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

New Post(0)