Vb. NET Chinese course (Whole-part) relationship

Source: Internet
Author: User
Tags contains expression inheritance integer join reference
Tutorials | Chinese topic: whole-part relations


?????????? Content??????????
V 1. Object Whole-part Relationship
V 2. Combination/partial relationship
V 3. Inclusive/content Relationships
V 4. Set/Member Relationships





1. Object Whole-part Relationship

Class inheritance (class inheritance) and object combination (objects composition) are two key tools for software reuse (reuse). Class inheritance is the relationship between the parent and subcategory, for example,,"students" can be divided into "college students", "middle school students" and "primary School" three categories, whose inheritance relationship icon is as follows:

Figure 1, inheriting in UML expression categories

Object combinations are designed to create "composite objects" (composite object), for example, hospitals containing physicians and nurses, and their combined icons are as follows:


Figure 2, a UML expression object combination

Inheritance and combination of two magic weapons can be used jointly to organize a large software system. For example, the car is divided into buses, trucks, cars and other subcategories, and the vehicle contains engine, body, tires and other parts, the car system icon as shown in Figure 3 and figure 4:



Figure 3, Automobile category inheritance system


Fig. 4, the object combination relation of automobile

In this section, you will further analyze and describe the method of object composition. Youton (Yourdon) believes that there are three common composition relationships:
1) The combination/part (assembly-parts) relationship.
2) contains/content (container-contents) relationship.
3) Set/Member (collection-members) relationship.





2. Combination/partial relationship

A combination/partial relationship, often called a APO (A part of) relationship, for example, a car is a "combination of", with parts that are part ". The door is part of the house, so the house is "combined", door is "part of the"; besides, the window is also the "part" of the house. The relationship between the house and the doors and windows, icons are as follows:


Fig. 5, the object combination relation of the house

The following is expressed in VB:

' Ex01.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'---------------------------------------------------------------------------------
Class House
Class Door
Public size as Double
Public Sub New (ByVal s as Double)
Size = S
End Sub
End Class

Class Window
Public size as Double
Public Sub New (ByVal s as Double)
Size = S
End Sub
End Class

Private Dr as Door
Private win as Window
Public Sub New ()
Dr = New Door (50)
Win = New Window (100)
End Sub
Public Sub Show ()
MessageBox.Show ("Door:" + str (dr.size) + "Win:" + str (win.size))
End Sub
End Class
'---------------------------------------------------------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form

Public Sub New ()
MyBase.New ()
Form1 = Me
' This is required by the Win Form Designer.
InitializeComponent ()
' Todo:add any initialization on the InitializeComponent () call
End Sub
' Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose ()
Mybase.dispose ()
Components. Dispose ()
End Sub
#Region "Windows Form Designer generated code"
......
#End Region
Protected Sub Form1_Click (ByVal sender as Object, ByVal
e as System.EventArgs)
Dim h as New House ()
H.show ()
End Sub
End Class

The output of this program is as follows:
Door:50 win:100


House object was born immediately after the birth of the door object and Window object. For example, declaring orders--
Dim h as New House ()

At this time,h object was born, its inside of the DR and win object also was born.



This h-pass is called a "composite object", while Dr and win are called "partial objects" (Component object). This relationship has one feature: The life span of a combination object and a part of the object should be consistent.
In the sense of logic (Logical), in this House structure, doors and windows have the affinity of "life and die" with the houses, that is, the longevity is consistent. When the computer entity (physical) expresses, house object does not "really" contain door and window objects, but uses two references to point them. So the picture above can also be imagined as follows:


Both of these physical structures express the "combination/partial" relationship. Please look at another example:

' Ex02.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'----------------------------------------------------
Class person
Private P_name as String
Private P_age as Integer

Public Sub New (ByVal na as String, ByVal A as Integer)
P_name = NA
P_age = A
End Sub
Public Function isequal (ByVal obj as person) as Integer
Dim k as Integer = 0
If me.p_name = Obj.p_name Then
K = 1
End If
IsEqual = k
End Function
Public Sub Show ()
MessageBox.Show (Me.p_name + "," + str (me.p_age))
End Sub
End Class
'---------------------------------------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form

Public Sub New ()
MyBase.New ()
Form1 = Me
' This is required by the Win Form Designer.
InitializeComponent ()
' Todo:add any initialization on the InitializeComponent () call
End Sub
' Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose ()
Mybase.dispose ()
Components. Dispose ()
End Sub
#Region "Windows Form Designer generated code"
.........
#End Region
Protected Sub Form1_Click (ByVal sender as Object,
ByVal e as System.EventArgs)
Dim A as New person ("Alvin", 35)
Dim B as New person ("David", 25)
A.show ()
B.show ()
MessageBox.Show (str (a.isequal (b)))
MessageBox.Show (Str (b.isequal ("David", 32))
End Sub
End Class

This program outputs:
Alvin, 35
David, 25.
0
1

The constructor of the "group" object is the "partial" object of the birth of the new () program. The contents of the A and B two objects are:






In the IsEqual () program, two objects are compared by their p_name values. For example, the,a.p_name value is "Alvin" and the B.p_name value is "David", so the value of the a.isequal (b) expression is 0 (False).







3. Inclusive/content Relationship

In the house structure of the last section, doors and windows have the affinity of "life and die" with the houses. However, in daily life, similar but not so close to the honey situation is common. For example, a pilot sits in a cockpit and drives a plane, a driver drives a car in a car, a guest rides in a bus, and so on. The driver is not a part of the car, the guest is not a bus component, so there is no "combination/part" relationship between the car and the driver, however, the car does contain the driver, which is called the "inclusive/content" (container-contents) relationship.
Drivers and cars are independent objects, unlike engines that are always included in cars, and drivers are included in cars when driving. Obviously, the life expectancy of the driver and the car is not as long. The "inclusive/content" relationship is a special composite structure whose icon method is the same as the "group/part" relationship. e.g.


This diagram expresses the following:
A "combination/part" relationship between a car and an engine.
The relationship between the car and the driver is "inclusive/content".

The following is expressed in VB:

' Ex03.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'----------------------------------------------------
Class Driver
Private name as String
Public Sub New (ByVal na as String)
name = NA
End Sub
Public Sub Show ()
MessageBox.Show ("Driver:" + name)
End Sub
End Class

Class car
Class Engine
Public model as String
Public Sub New (ByVal mdl as String)
Model = MDL
End Sub
End Class

Private e as Engine
Private Dr as Driver
Public Sub New ()
E = New Engine ("Honda")
End Sub
Public Sub Assignto (ByVal D as Driver)
Dr = d
End Sub
Public Sub Show ()
MessageBox.Show ("Engine:" + E.model)
Dr. Show ()
End Sub
End Class
'----------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form
Public Sub New ()
MyBase.New ()
Form1 = Me
' This is required by the Win Form Designer.
InitializeComponent ()
' Todo:add any initialization on the InitializeComponent () call
End Sub
' Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose ()
Mybase.dispose ()
Components. Dispose ()
End Sub
#Region "Windows Form Designer generated code"
.......
#End Region
Protected Sub Form1_Click (ByVal sender as Object, ByVal e as System.EventArgs)
Dim Civic as New car ()
Dim D1 as New Driver ("Wang")
Dim D2 as New Driver ("Kao")
Civic.assignto (D1)
Civic. Show ()
Civic.assignto (D2)
Civic. Show ()
End Sub
End Class

This program outputs:
Model:honda
Driver:wang
Model:honda
Model "Kao

Car object was born, also the birth of the object of engine e; also immediately designated the driver, the following instructions:

Dim Civic as New car ()
Dim D1 as New Driver ("Wang")
.....
Civic.assignto (D1)
.....

Common situation in daily life: When a car object is born, it is not necessary to designate the driver immediately. For example, a car without a driver at the factory or at idle, and the car often change the driver. In this case, the Civic object and the D1 object should be born first, as follows:



At this time, the driver is not specified immediately and, if necessary, the driver is specified in the Assignto () procedure. For example, assigning a D1 to a civic object brings the reference variable Dr Civic to the D1 object, as follows:



In the above procedure, the D1, D2 and civic objects, who first born and irrelevant, each independent existence. Directive ──civic.assignto (D1) assigns the D1 driver to the Civic object, and another instruction ──civic.assignto (D2) says: D2 as the driver of Civic.
This car category refers to the engine object by reference to the variable E. Now, take the person category in the previous section as an example, if someone (who is the object) is married, there is a spouse, and the other is not married, there is no spouse. At this point, the definition of the person category should be modified to express this "spouse" relationship:

' Ex04.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'----------------------------------------------------
Class person
Private P_name as String
Private P_age as Integer
Private P_spouse as Person

Public Sub New (ByVal na as String, ByVal A as Integer)
P_name = NA
P_age = A
End Sub
Public Sub Spouse (ByVal sp as person)
P_spouse = SP
Sp.p_spouse = Me
End Sub
Public Sub Show ()
MessageBox.Show (Me.p_name + "," + str (me.p_age)
+ ", SP:" + Me.p_spouse.p_name)
End Sub
End Class
'----------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form

Public Sub New ()
MyBase.New ()

Form1 = Me
' This is required by the Win Form Designer.
InitializeComponent ()
' Todo:add any initialization on the InitializeComponent () call
End Sub
' Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose ()
Mybase.dispose ()
Components. Dispose ()
End Sub
#Region "Windows Form Designer generated code"
......
#End Region
Protected Sub Form1_Click (ByVal sender as Object, ByVal
e as System.EventArgs)
Dim x as New person ("David", 32)
Dim y as New person ("Hellen", 24)
X.spouse (y)
X.show ()
Y.show ()
End Sub
End Class

This program outputs:
David, Sp:hellen.
Hellen, Sp:david.

The data member P_spouse point to the spouse, and the spouse is also the object of person. So the form of p_spouse should be person. When a person is born without a spouse and has a spouse at the time of marriage, it is spouce () to establish a spouse relationship. objects x and Y marry each other for each other's spouses. So x.p_spouse points to y, and Y.p_spouse points to X. At this point the contents of,x and Y are as follows:



So this person category expresses the "marriage" relationship.






4. Set/Member relations

A set means a group of "groups" consisting of members (member). For example, there are league members in the school's clubs, and the sales department of the company contains marketing staff. The group is not like a car that actually contains drivers, but a collection of members. This situation is collectively referred to as a "set/member" (collection-members) relationship.
Interestingly: In enterprise activities, people plan the scheme, including a number of small programmes, then the big scheme is composed of small programmes. For example, the itinerary for Northeast Asia tours includes sightseeing tours in Japan, sightseeing tours in South Korea and sightseeing tours in Hong Kong. This row table icon is as follows:



The total stroke is the set of the stroke (or segment stroke), which is the "set/member" relationship.



In addition, the baseball team is made up of managers, coaches, and players: There are several product items in the order, which are set/member relationships. When writing a program, there is no need to explicitly divide the relationship between "inclusive person/content" and "set/member". The reason is: the collection and members can also be independent of each other, there is no "life and life" relationship between the affinity; for example,,"Hong Kong Sightseeing Tour" is independent, it can be included in the total itinerary of Northeast Asia, but also included in the overall itinerary of Southeast Asia tour. " Thus the,"set/member relationship is a special" combination "(composition) structure.
Take the person category above as an example, if the person's object will join the club (club) as a member of the clubs, then the relationship between the clubhouse and person is "set/member" relationship. The club category is defined as follows:

' Ex05.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
Imports System.Collections
'----------------------------------------------------
Class person
Private P_name as String
Private P_age as Integer

Public Sub New (ByVal na as String, ByVal A as Integer)
P_name = NA
P_age = A
End Sub
Public Sub display ()
MessageBox.Show (Me.p_name + "," + str (me.p_age))
End Sub
End Class

Class Club
Private C_name as String
Private PA as ArrayList

Public Sub New (ByVal na as String)
C_name = NA
PA = New ArrayList ()
End Sub
Public Sub join (ByVal p as person)
Pa. ADD (P)
End Sub
Public Sub display ()
MessageBox.Show ("Club:" + Me.c_name + "has member:")
Dim p as Person
For all p in PA
P.display ()
Next
End Sub
End Class
'-------------------------------------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form

Public Sub New ()
MyBase.New ()
Form1 = Me
' This is required by the Win Form Designer.
InitializeComponent ()
' Todo:add any initialization on the InitializeComponent () call
End Sub
' Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose ()
Mybase.dispose ()
Components. Dispose ()
End Sub
#Region "Windows Form Designer generated code"
......
#End Region
Protected Sub Form1_Click (ByVal sender as Object, ByVal e as System.EventArgs)
Dim x as New Club ("Sogo")
Dim A as New person ("Alvin", 32)
Dim B as New person ("Judy", 28)
X.join (a)
X.join (b)
X.display ()
End Sub
End Class


This program outputs:
Club:sogo has member:
Alvin, 32
Judy, 28.

C_name point to the object of Strclass, this object contains the name of the club. The PA points to the ArrayList object, which can contain many member (person) objects. The join () program stores the object of person in the ArrayList object that the PA refers to.
The Club object contains ArrayList objects, this collection object (collections) contains the object of person and expresses the "set/member" relationship. For example, the,x object contains a and B objects.



This figure says: "Sogo" Club has a total of "Alvin" and "Judy" two members, that is, X is "set", and A and B are "members" (member).
Note: This software is to use the existing categories of ──strclass and integer groups to synthesize the application category ──person. Then use the person category and the ArrayList category to combine into more complex application categories ──club. In the future, the club and other categories can be used to build larger application categories, and so on, to create large and reliable software. For example:

' Ex06.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
Imports System.Collections
'----------------------------------------------------
Class person
Private P_name as String
Private P_age as Integer
Public Sub New (ByVal na as String, ByVal A as Integer)
P_name = NA
P_age = A
End Sub
Public Sub display ()
MessageBox.Show (Me.p_name + "," + str (me.p_age))
End Sub
End Class

Class Club
Private C_name as String
Private PA as ArrayList

Public Sub New (ByVal na as String)
C_name = NA
PA = New ArrayList ()
End Sub
Public Sub join (ByVal p as person)
Pa. ADD (P)
End Sub
Public Sub display ()
MessageBox.Show ("Club:" + Me.c_name + "has member:")
Dim p as Person
For all p in PA
P.display ()
Next
End Sub
End Class
'----------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form

Public Sub New ()
MyBase.New ()

Form1 = Me
' This is required by the Win Form Designer.
InitializeComponent ()
' Todo:add any initialization on the InitializeComponent () call
End Sub
' Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose ()
Mybase.dispose ()
Components. Dispose ()
End Sub
#Region "Windows Form Designer generated code"
......
#End Region
Protected Sub Form1_Click (ByVal sender as Object, ByVal e as System.EventArgs)
Dim x (2) as Club
X (0) = New Club ("Sogo")
X (1) = New Club ("Gold")
Dim A as New person ("Alvin", 32)
Dim B as New person ("Judy", 28)
Dim C as New person ("Bob", 38)
X (0). Join (a)
X (0). Join (b)
X (1). Join (b)
X (1). Join (c)
X (0). Display ()
X (1). Display ()
End Sub
End Class

This program outputs:

Club:sogo has member:
Alvin, 32
Judy, 28.
Club:gold has member:
Judy, 28.
Bob, 38.

Combination object X contains "Sogo" and "gold" two clubs, of which the "gold" club has two members-"Alvin" and "Judy", while the "Sogo" Club has two members-"Judy" and "Bob". X (0) on behalf of the "Sogo" Club,s (1) represents the "gold" club, so instruction ──s (0). Join (a) indicates that a joins the "gold" club and becomes its member. N



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.