Vb. NET Chinese Tutorials (one) prototype style

Source: Internet
Author: User
Tags contains integer
Tutorials | Chinese theme: Prototype style
Subtitle: Multi-form, interface (Interface)


????????? Content?????????
V 1. Style
V 2. object's prototype (objects prototype)
V 3. Implement prototype style with VB
V 4. Design and assembly of application-----components of prototype style


1. Style
The masterpiece of "Design patterns:elements of reusable object-oriented Software" by Erich Gamma and others contains 23 important designing styles. As the name suggests,"style" is everybody can "have kind of study kind, dots", and again and again, repeatedly in different occasions, repeated use (reuse) it to solve common problems.
Style must be used often, and the more skillful the better, can be changed with the external environment (context), to really solve the problem (problem). Like Sun Tzu's Art of War, Taijiquan contains many styles (or moves) that must be understood and practiced in order to reach the point of perfection. The main reason is: Single style (moves) often only solve a small problem, and the big problem may require a variety of styles to use the mix. How do you combine small styles into large styles to solve big problems? This often requires a complete set of rules (rule), commonly known as "style language" (Pattern language). This article refers to the prototype style in the Gamma book, explains how to implement it with VB interface, also makes you better use of the concept of polymorphism. Here's a look at how you can use the prototype style.


Figure 1, prototype-style UML diagram


2. Object's prototype (objects prototype)
In People's daily life, the following statements are common:

"I'm going to have a cat like Garfield."
"I'm going to marry a wife of beauty like Xishi."
......

Among them, Garfield and Xi Shi are all prototype (or translated as examples). When you say these two words, the listener immediately through the prototype object (that is, Garfield or Xi Shi) to understand what you want to describe the new object. On the software side, the user can use prototype to tell the computer:

"The object I want is like this prototype object."

The computer then uses the prototype object to create an identical new object to the user.
Recall that we are familiar with VB, C #, Java, or C + + language, all by "category" to describe the characteristics of the object, and then the computer according to the description of the class to create new objects. This is known as class-based programming; and the former is called prototype-based programming.
Along with, Software 0 component (Sofrware IC) concept of popular,prototype-based programming concept also increasingly important. Even like VB language, can also support prototype-based programming.


3. Implement prototype style with VB
Figure 1 above is the prototype style listed in the Gamma book. Figure 2 Below is a practical example.


Figure 2, prototype of drawing objects

The object Designer derives the circle and rectangle two categories from shape, and each birth of 1 prototype objects, and is stored in shapelist serial or array. The designer must define the Clone () function for each type to give birth to the new object and form the polymorphism. The object assembler can then simply call the Clone () function to get the new object without having to have a category concept. In the future, designers can derive a large number of subcategories from the shape category and put objects into shapelist for use by assemblers.
Let's see how VB can be used to implement the UML pattern shown in Figure 2:

' Ex01.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'-------------------------------------------------------------------------
Class Shape
Protected LX, ly as Integer
Public Sub SetXY (ByVal x As Integer, ByVal y As Integer)
LX = x
ly = y
End Sub
Public Overridable Sub Draw ()
End Sub
Public Overridable Function Clone () as Shape
End Function
End Class

Class Circle
Inherits Shape

Public Overrides Sub Draw ()
MessageBox.Show ("Drawing a Circle at" ("+ str (LX) +", "+ str (ly) +") ")
End Sub
Public Overrides Function Clone () as Shape
Clone = New Circle ()
End Function
End Class

Class Rectangle
Inherits Shape

Public Overrides Sub Draw ()
MessageBox.Show ("Drawing a Rectangle at" ("+ str (LX) +", "+ str (ly) +") ")
End Sub
Public Overrides Function Clone () as Shape
Clone = New Rectangle ()
End Function
End Class
'-------------------------------------------------------------------------------------
Class shapelist
Private Tlist (a) as Shape
Private Counter as Integer

Public Sub New ()
Counter = 0
End Sub
Public Sub AddShape (ByVal sobj as Shape)
Tlist (counter) = Sobj
Counter = counter + 1
End Sub
Public Function Getshape (ByVal i as Integer) as Shape
Getshape = Tlist (i)
End Function
Public Sub Draw ()
Dim I as Integer
For i = 0 to Counter-1
Tlist (i). Draw ()
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 list as New shapelist ()
Dim PS as Shape
PS = New Circle ()
Ps. SetXY (10, 10)
List. AddShape (PS)

PS = New Rectangle ()
Ps. SetXY (50, 50)
List. AddShape (PS)

PS = list. Getshape (0). Clone ()
Ps. SetXY (230, 70)
List. AddShape (PS)

List. Draw ()
End Sub
End Class

This program outputs:
Draw a Circle at (10, 10)
Draw a Rectangle at (50, 50)
Draw a Circle at (230, 70)

The shapelist category belongs to the client, which uses only the shape class information when designing the Shapelist category, and in the client (such as the Form1 category above), in addition to the circle and rectangle category names when the object is born, Can only use the shape category of information, which allows us to continue to expand in the future subcategories, such as square, triangle and so on. In order to achieve this high extensibility, the concept of polymorphism (polymorphism) needs to be used. So the shape category is based on the multiple shapes:

Public Overridable Sub Draw ()
End Sub
Public Overridable Function Clone () as Shape
End Function
Draw () and Clone () are virtual programs to perform multiple (polymorphism) functions. LX and LY are the upper-left corner coordinates of the graph. SetXY () can change LX and ly values. The Shapelist class Draw program is used to draw the individual prototype object graphs in the serial.
The parent category of VB (superclass) has two roles:
1 provides some programs for inheriting subcategories
2) as a common interface between subcategories (Interface)

The above procedure is the way to implement the two. If you implement a distributed (distributed) environment, you should separate and implement the above two roles. The interface mechanism of VB must be used at this time. For example, the above procedure is equivalent to:

' Ex02.bas
Imports System.ComponentModel
Imports System.Drawing
Imports system.winforms
'----------------------------------------------------
Interface IShape
Sub Draw ()
Function Clone () as IShape
Sub SetXY (ByVal x As Integer, ByVal y As Integer)
End Interface

Class Shape
Protected LX, ly as Integer
Protected Sub SetXY (ByVal x As Integer, ByVal y As Integer)
LX = x
ly = y
End Sub
End Class

Class Circle
Implements IShape
Inherits Shape

Public Sub Draw () Implements Ishape.draw
MessageBox.Show ("Drawing a Circle at" ("+ str (LX) +", "+ str (ly) +") ")
End Sub
Public Function Clone () as IShape Implements Ishape.clone
Clone = New Circle ()
End Function
Public Sub SetValue (ByVal x As Integer, ByVal y As Integer) Implements ishape.setxy
Mybase.setxy (x, y)
End Sub
End Class

Class Rectangle
Inherits Shape
Implements IShape

Public Sub Draw () Implements Ishape.draw
MessageBox.Show ("Drawing a Rectangle at" ("+ str (LX) +", "+ str (ly) +") ")
End Sub
Public Function Clone () as IShape Implements Ishape.clone
Clone = New Rectangle ()
End Function
Public Sub SetValue (ByVal x As Integer, ByVal y As Integer) Implements ishape.setxy
Mybase.setxy (x, y)
End Sub
End Class
'------------------------------------------------------------------------------------------
Class shapelist
Private Tlist (a) as IShape
Private Counter as Integer

Public Sub New ()
Counter = 0
End Sub
Public Sub AddShape (ByVal sobj as IShape)
Tlist (counter) = Sobj
Counter = counter + 1
End Sub
Public Function Getshape (ByVal i as Integer) as IShape
Getshape = Tlist (i)
End Function
Public Sub Draw ()
Dim I as Integer
For i = 0 to Counter-1
Tlist (i). Draw ()
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 list as New shapelist ()
Dim Pshape as IShape
Pshape = New Circle ()
Pshape.setxy (10, 10)
List. AddShape (Pshape)

Pshape = New Rectangle ()
Pshape.setxy (50, 50)
List. AddShape (Pshape)

Pshape = list. Getshape (0). Clone ()
PSHAPE.SETXY (230, 70)
List. AddShape (Pshape)

List. Draw ()
End Sub
End Class

This program outputs:
Draw a Circle at (10, 10)
Draw a Rectangle at (50, 50)
Draw a Circle at (230, 70)

Shape categories focus on the behind-the-scenes role, the client and Shapelist category designers only see the IShape interface, which fully play the advantages of the VB interface.


4. Application of prototype style
Design and assembly of-----components

The software industry is gradually moving toward 0 components or components (component), and future computer software personnel will be divided into two groups: Object Designers (Objects designer) and object Assemblers (objects assembler). Take VB programmers, the object Designer is responsible for the design category, in order to create a variety of objects, object assemblers can use existing objects or assemble them into larger objects or systems.
According to the above examples of Garfield and Xishi, it is often natural for people to describe what they want with a familiar example, that is, by example, the object of his mind is more appropriate to people's living habits. Instead of describing the object in his mind in categories. Therefore, the object designer to define the various categories should also be the birth of an object, as an example (prototype), it is best to display on the Windows screen. Object assemblers do not need to have a "category" concept, just use the mouse to pick up the prototype object on the screen, you can get a similar new object. So, it can constitute a good division of labor situation. However, in languages such as VB and Java, objects are created by "category". This can create a conflict problem:

The designer can not be ready to be used by the assembler at any time, so it is hoped that the assembler will be born by category.
The U-assemblers do not have to have category ideas, and are therefore unwilling to be born with categories.

How to solve this problem? The answer is: Use the prototype style.

The work of the object Designer includes:
1. Design (definition) A form category called Form1.
2. Design an Application Architecture (application framework) as the basis for "designer" and "assembler" division of labor.
3. Based on the architecture, it derives subcategories such as circle and rect.
4. The birth object, stored in the serial shaplist, as shown in Figure 3.


Figure 3, the basic environment of the Assembly

The work of the object assembler is:
Click (or make a message) to the prototype object in the serial, by which it calls Clone () to give birth to the new object.
For example, let "assembler" select objects from screen screens. Circle and Rectangle define draw () and clone () to achieve a multiple effect, so that "assemblers" more convenient to use. Clone () must return a new object. The instructions in Form1.click ()-----

Pshape = list. Getshape (0). Clone ()
PSHAPE.SETXY (230, 70)
List. AddShape (Pshape)

First, take out the No. 0 prototype object in the serial, which is the other object that was born, represented by Pshape. When you save this new object in serial, the following illustration shows:




Fig. 4, the assembler according to prototype indicates the computer birth object

At the bottom, the number of prototype objects is shown on the screen.
In the above example, Shapelist is also classified as one of the object Designer's work. In fact, Shapelist is often part of the "Assembly Tool" (Assembly tools). The designer of the Assembly tool, however, may be neither an Object designer nor an object assembler, but is used to assist the assembler to make it work better.
Because of the polymorphism of the clone function and the draw (), adding clone () can produce new objects, so that the assembly tool designer, without having to worry about the proliferation of object classes, or the expiration class name, greatly reduces the complexity of the tool program, which is another important use of the prototype model.



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.