Description and abstraction - dynamic aggregation, association and dependent application example

zhaozj2021-02-16  72

Description and abstraction - dynamic aggregation, association and dependent application example

First, the problem description

In the data structure and chart, we often come into contact with the data structure of "Figure". "Figure" has an extremely important role in many programs. Therefore, a picture is described in the computer and it is displayed as a problem that we often encounter. Here, we will implement the description of the map with object-oriented ideas. Note that the figure we describe is, and the non-map can be used as a special case of a map), and we allow two points to exist multiple lines. For example, we should be able to describe this figure below (Figure 1):

(Figure 1) (Dark color is the starting point, pastel color is endpoint) (Figure 2)

Second, the problem analysis

As can be seen from the above problem description, we mainly solve two problems. One is a description, one is a display. Obviously, for one figure, it should be no location information, we describe such a picture with a BasicGraph class, which describes the pure map of the data structure. And we use another PositionGraph class to add a location information, which can do not have to pay attention to the data structure of the map, if organized, it only needs to know the location information of point and line. Therefore, these two classes have the inheritance relationship shown in Figure 2.

(1) Description of Dynamic aggregation method

For one figure, it dynamically aggregates points and lines. We use the BasicPoint class to describe the point and describe the line with the BasicLine class. These two classes are located at the same level with BasicGraph, and they have no location information.

Now we introduce three classes to a picture: BasicPoint, Basicline and Basicgraph. These three classes have the following relationships:

1. There are two endpoints in one line, one as the starting point, one for the start point.

2. A point may be connected to multiple sides. The lines connected to a certain end point are divided into two categories: one is the line with the starting point of this point; the other is the line of the end of this point.

3. A chart consists of a set of points with a set of wires.

Thus, we can get the relationship diagram of these three classes as shown in Figure 3, Figure 4 and Figure 5:

(Figure 4) (Figure 5)

(image 3)

Here, Figure 3 illustrates a diagram to accumulate from a set of points and a set of wires. The picture does not know the relationship between point and line, the relationship between point and line is spread between points and lines, and more detailed relationships between points and lines are shown in Figures 4 and 5. As we can see from the figure, you know all the points and all the lines; click to know all lines of its starting point, also know all lines of it; line knows its starting point and end.

(B) describing the location information with inheritance method

Let a picture show it in the screen, we need to know the topology of the map, but also need to know the location information of point and line. Obviously the three classes above are enough to describe the topology of the figure. Next, we must join the location of the location in the existing class. Thus, three classes containing location information: PositionPoint, PositionLine and PositionGraph. These three classes are inherited on the top of BasicPoint, Basicline, and Basicgraph.

For points, we use the setPosition method to add a point information, give some positioning;

(3) Formation with association methods to display location information on the drawing board We have the purpose of displaying the diagram on multiple drawings, so we use simplified MVC mode. In this simplified MVC mode, we only have M (MODEL) and V (View) without considering C (Controller). We here is a PositionGraph class, while V is a class that implements a Visuable interface. The Visuable interface defines a method repaint so that every PositionGraph change, you can call the Repaint function, re-draw.

Obviously, PositionGraph can also dynamically aggregate a plurality of classes that implement the Visuable interface to display the diagram in multiple places. The class that implements the Visuable interface will access the PositionGraph's data structure through the associated method, and display it. We give a Visuable interface here to DrawingPanel. Their relationship is shown in Figure 6, here, a DrawingPanel can only draw a positiongraph:

(Figure 6) (Figure 7)

(4) Use a dependency method to turn a positionless map into a location map

Typically, we only care about the topology of the figure, and it doesn't matter it. And we finally hope that you can display it directly in the program. Therefore, we need a helper class that can be automatically positioned and the line location according to a BasicGraph, a DrawingPanel, providing height and width information), and the automatic positioning point and the line of the line, then automatically generates a positiongraph. This is the role of class POSGRAPHELPER. See Figure 7 on the relationship between several classes just mentioned.

It is difficult to position points and lines of a positionless figure, we just give a simple implementation. If there is a better algorithm, we only need to update the POSGRAPHELPER class.

Third, the realization of dynamic aggregation

Dynamic aggregation can be realized in java2, and a Collections framework is given in Java2, and multiple classes provide us to achieve dynamic aggregation. Basically they are divided into four categories: hash table, variable length array, balance tree and linked list. The VECTOR class is a variable length array; TreeMap and HashMap belong to a hash table. TreeMap is more than HashMap, you can sort the keywords.

In the figure described in Basicgraph, we give each point a unique ID, and each line has a unique ID. We may often need to access the points and lines in the picture through the ID, so use the hash table to save the points and lines in the picture are the best choice. Regular ID we give points may be: 1, 2, 3┉, or P1, P2, P3┉. Therefore, the point is preferably arranged in the order of ID. Therefore, in BasicGraph, we use the following statements to dynamically aggregate points and lines:

TreeMap Points;

Hashmap lines;

One line contains information of two endpoints, so BasicLine only needs to include the following information:

BasicPoint StartPoint, Endpoint;

And a point will record all the lines adjacent to it, these lines are divided into two categories, and one class is starting with this point, and one class is at the end of this point. These lines are gathered using the following aspects in BasicPoint:

Vector linesasstartpoint; vector LinesasendPoint; now the problem is to automatically maintain this information when you add or delete points and lines in BasicGraph. The sequence diagram of the addition line is as follows:

Here, other classes must operate to the BasicGraph operation to implement the modification of the map, first, if we want to add a line, we must give the ID (SID) and the ID (EID) of the start-up, but also give the addition line. ID (ID). When addLine in these parameters, BasicGraph removes the starting point (SP) and end points (EP) from the hash table via SID and EID to newly generate an instance of a BasicLine. When a new BasicLine instance is newly generated, it will call the starting point SP's AddLineAasstartPoint method, and register the line itself as a neighboring line of the starting point SP; at the same time, it will call the addLineasendPoint method of the end point EP, and register the line itself as an adjacency of the end point EP line. Finally, BasicGraph will call its own addline method to register the newly generated BasicLine instance as its own line. The specific program is seen from the source code.

Fourth, the realization of the association

In a DrawingPanel class, a location with a location is associated with an instance of the PositionGraph class. DrawingPanely initializes this instance of PositionGraph when calling the setposgraph method of the DrawingPanel class. DrawingPanely first draws the first picture by accessing the PositionGraph class, DrawingPanely first draws the line to draw the entire map.

A figure can gather multiple views that when there is only one view and it is an instance of a DrawingPanel class, we can also see from the PositionGraph class to DrawingPanel. Such a associated meaning is that when PositionGraph knows how its own topology is changed, it can call the PositionGraph's Repaint method for redraw.

Five, dependence

In POSGRAPHELPER, we offer several ways to generate a PositionGraph from a BasicGraph class, or automatically generate a full picture, etc. For example, getPositionGraph can turn BasicGraph to a positionGRAPH according to the size of DrawingPanel. The source code is as follows:

Public Static PositionGraph getPositionGraph (DrawingPanel Panel, Basicgraph Bgraph) {

Positiongraph pgraph = new positiongraph ();

Reshape (Panel, BGRAPH, PGRAPH);

Return PGRAPH;

}

The Reshape method can realize the repositioning of the point and the line, and finally generate the positionGraph, return to the caller. The reshape method is still very imperfect and needs further modification.

Six, used class list

At present, we have explained most of the class that is used. Now list all the classes, as shown in the following table:

Class name BasicPoint implements basic point, basic point is only a bit line relationship, no location information BasicLine implements basic line, basic line only bit line relationship, no position information Basicgraph implement topology map, no location information PositionPoint BasicPoint subclasses, add location information PositionLine Basicline subclass, add location information PositionGraph BasicGraph subclass, implement a class of location Visuable interface, define a class DrawingPanel drawing board that can be sang (repaint), implementing a Visuable interface, associating a positiongraph object, implement drawing The functional POSGRAPHELPER assistant class is used to generate some special positiongraph, or convert from BasicGraph to the Frame1 test class. It is used to test the above other classes to put the front nine classes in the package DynaggRegate. Different from other unrelated classes.

Seven, instance

We generate two diagrams, one is a complete picture in the unidistory map, one is a complete picture in the figure (not including the line to its own line). Both pictures are passed by:

1. POSGRAPHELPER.PRODUCEFULLGRAPH (5)

2. POSGRAPHELPER.PRODUCEFULLGRAPH2 (5)

Generated. The results they produce are as follows:

Complete picture of the full picture of the map

Eight, partial source code (only a small part)

1. BasicPoint source code

Class BasicPoint {

protected string point_id; // unique ID

Protected Vector LineSasstartPoint; // Save all lines from this point as the starting point

Protected Vector LinesasendPoint; // Save all lines with this point

Public BasicPoint (String ID) {

Point_id = ID;

LINESASSTARTPOINT = New Vector ();

LinesasendPoint = new vector ();

}

Public void addlineasstartPoint (Basicline line) {// Register line to the starting point for this point

LINESASSSTARTPOINT.ADD (line);

}

Public void addlineasendpoint (Basicline Line) {// Register the line to the end of this point

LinesasendPoint.Add (line);

}

}

2. Basicline source code

Class Basicline {

Protected BasicPoint StartPoint, Endpoint;

Protected string line_id;

Protected int PowerValue = 0;

Public BasicLine (BasicPoint Start, BasicPoint End, String ID) {// Line always consists of two points

StartPoint = start;

Endpoint = end;

Start.addlineasstartPoint (this); // Putting the line with the starting point

End.addlineasendpoint (this); // Put the line with the end point linkage_id = id;

}

Public void resetstartPoint (BasicPoint Start) {// Sometimes you may need to change the starting point

StartPoint.removelineasstartPoint (this);

Start.addlineasstartPoint (this);

StartPoint = start;

}

Public void resendPoint (BasicPoint End) {// may sometimes need the end point of the change

Endpoint.removelineasendpoint (this);

End.addlineasendpoint (this);

Endpoint = end;

}

}

3. Basicgraph source code

Public class basicgraph {

Protected Treemap Points;

Protected hashmap line;

Public Basicgraph () {// Initialization

Points = new treem ();

LINES = new hashmap ();

}

Public Vector getPoints () {// Get all points of the map

Return New Vector (Points.Values ​​());

}

Public Vector getLines () {// Get the outline of the map

Return New Vector (Lines.Values ​​());

}

Public BasicPoint AddPoint (String ID) {// Plus Point

Return AddPoint (New BasicPoint (ID));

}

Public BasicPoint AddPoint (BasicPoint Point) {// Plus Point

IF (Point! = null) Points.Put (Point.GetId (), POINT);

Return Point;

}

Public Basicline AddLine (String StartID, String endID, String ID) {// 加 图 图 线

Object start = points.get (startID), end = points.get (endID);

IF (start == null) start = addpoint (startID);

IF (end == null) end = addpoint (endID);

Return AddLine (NEW BASICLINE ((BasicPoint) START, (BasicPoint) End, ID);

}

Public Basicline AddLine (Basicline Line) {// Plus Line

IF (line! = null) lines.put (line.getid (), line;

Return line;

}

}

4. PositionPoint source code

Public Class PositionPoint Extends BasicPoint {Private Point Position;

Public positionPoint (string id) {

THIS (ID, New Point (0, 0));

}

Public positionPoint (String ID, POINT POS) {

SUPER (ID);

Position = POS;

}

Void SetPosition (POINT POS) {// Setting point location

Position = POS;

}

}

5. PositionLine source code

Public class positionLine Extends Basicline {

Private point gradualpoint = null; // The position of the line is determined by the starting point and endpoint, but it can also have a gradual point.

Public positionLine (positionPoint StartPoint, PositionPoint Endpoint, String ID) {

Super (StartPoint, Endpoint, ID);

}

Public void setgradualpoint (Point Point) {

GradualPoint = POINT;

}

Public Point getGradualPoint () {

Return gradualpoint;

}

}

6. PositionGraph source code

Public class positiongraph extends Basicgraph {

Private Vector Viewers = NULL; / / You can register multiple views

Public positiongraph () {

Super ();

Viewers = new vector ();

}

Public BasicPoint AddPoint (String ID) {// Reserved BasicPoint to generate a PositionPoint point

PositionPoint PP = (PositionPoint) AddPoint (New PositionPoint (ID));

Return PP;

}

Public BasicPoint AddPoint (BasicPoint Point) {// Reserved, Automatic Heavy Picture

PositionPoint PP = (positionPoint) Super.addpoint (Point);

Repaint ();

Return PP;

}

// Reserve Basicline to survive the PositionLine line

Public Basicline AddLine (String StartID, String endID, String ID) {

Object start = points.get (startID), end = points.get (endID);

IF (start == null || end == null) Return NULL;

PositionLine PL =

(PositionLine) Addline (positionPointLine) Start, (PositionPoint) end, ID);

Return PL;

}

Public Basicline AddLine (Basicline Line) {// Reserved, Automatic Heavy Picture

PositionLine PL = (positionLine) Super.addline (line); repaint ();

Return PL;

}

Public Void AddViewer (Visuable Viewer) {// Add view

Viewers.Add (Viewer);

Viewer.repaint ();

}

Public void repaint () {// All views Heavy

For (int i = 0; i

(ViSuable) Viewers.ementatat (i)). Repaint ();

}

}

}

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

New Post(0)