Implement Prototype design mode

zhaozj2021-02-16  47

Implement Prototype design mode

Implementing the prototype design pattern

When I build an instance of a class is very complicated, we can use the prototype mode. An instance of a large number of classes is established, it is better to use the original copy of the original instance after appropriate modification. Using the prototype mode, you can reduce the number of subclasses by cloning a prototype. Prototype mode can reduce the number of instances of classes.

In this mode, the object is created by cloning. We sometimes create a lot of subclasses, in addition to creating different objects through a lot of subclasses, we can only need a single subclass, this subclass keeps a reference to each object base class, and created through this subclass Object. Parameters and cloned the object by transmitting the parameters and cloned the object. Each object implements a Clone method, so it can be cloned. We can use the prototype mode to reduce the number of subclasses by cloning prototypes.

Clones can be implemented by implementing the IcloneAble interface. The only way in the iCloneable interface is Clone and returns an instance of a new class.

Icloneable.clone method Signature [VisualBasic] Function Clone () as object [c #] Object Clone ();

We must understand that the clone () method is just a shallow copying, not deep copy (Deep Copy). So it just returns a reference, not an instance of a replication as deep copy (Deep Copy). We can implement deep replication (Deep Copy) by using the iSerializable interface.

Another disadvantage is that each subclass of the prototype must implement Clone () method, sometimes, adding a Clone method is difficult.

In this example, I built the Empdata class and implemented the Icloneable interface and the iSerializable interface. The iCloneable interface needs to implement a Clone method so that the class can be copied. The iSerializable interface in order to achieve deep replication of EMPDATA classes (DEEP COPY). The method used is: Sequence of EMPDATA objects into a file, or the file is also sequenced into an EMPDATA object.

The EmpData class contains two methods: GetEmpData and Changeempdata. These two methods are used to get the EMPDATA object in a string (String), change the EMPDATA class. Each method can be called to verify the difference between Shallow Copy and Deep Copy. When the EMPDATA class changes, this change occurs simultaneously in the clone object of EmpData; in deep copy, this change does not appear when the EmpData object changes. Empdata's cloned object.

The constructor of the EMPDATA class reads the XML file and creates an EMP object.

XML file

VB.NET implementation

Imports system.xml

Imports system.io

Imports system.collections

Imports system.Runtime.Serialization

Imports system.runtime.serialization.formatters.binary

public class CEMPDATA

Implements Icloneable, iSerializablePrivate Arremp as ArrayList

Public Sub New ()

DIM Xmldoc as new xmldocument

Dim node as xmlnode

DIM Objemp as CEMP

Arremp = New ArrayList

XMLDoc.Load ("Empdata.xml")

For each node in xmldoc.documentelement.childNodes

Objemp = New CEMP

Objemp.fname = node.selectsinglenode ("firstname"). InnerText

Objemp.lname = node.selectsinglenode ("Lastname"). InnerText

Arremp.Add (Objem)

NEXT

End Sub

Public Sub New (Byval Info As SerializationInfo, ByVal Context As Street "

DIM INTINDEX AS INTEGER

DIM INTCOUNT AS INTEGER

DIM Objemp as CEMP

Arremp = New ArrayList

INTCOUNT = CINT (Info.getValue ("EMP_COUNT", GetType (String))))

For intindex = 0 to intcount - 1

Objemp = New CEMP (Info, Context, Intindex)

Arremp.Add (Objem)

NEXT

End Sub

Public Function Clone () As Object Implements IcloneAble.clone

Try

Return ME

Catch exception

MsgBox (ex.toswoting)

END TRY

END FUNCTION

Public Function Clone (Byval Deep As Boolean) AS Object

Try

IF Deep Then

Return createdeepcopy ()

Else

Return Clone ()

END IF

Catch exception

MsgBox (ex.toswoting)

END TRY

END FUNCTION

Private function creatededeepcopy () AS CEMPDATA

DIM ObjempCopy As Cempdata

Dim Objstream As Stream

Dim Objbinformatter as new binaryformatter

Try

Objstream = file.open ("EmpData.bin", filemode.create)

Objbinformatter.Serialize (Objstream, ME)

Objstream.close ()

Objstream = file.open ("Empdata.bin", FileMode.Open

Objempcopy = ctype (objbinformatter.deserialize (objstream), CEMPDATA)

Objstream.close ()

CreateDeepcopy = ObjempCopy

Catch exception

MsgBox (ex.toswoting)

END TRY

END FUNCTION

Public Sub GetObjectData (ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectDataDim intIndex As Integer

DIM Objemp as CEMP

Info.addValue ("EMP_COUNT", arremp.count)

For infindex = 0 to arremp.count - 1

Objemp = arremp (intIndex)

Objemp.GetObjectData (Info, Context, Intindex)

NEXT

End Sub

Public function getempdata () AS STRING

DIM INTCOUNT AS INTEGER

DIM STREMPDATA AS STRING

For intcount = 0 TO Arremp.count - 1

Strempdata = strempdata & ctype (arremp (intcount), CEMP) .fname & chr (9) & ctype (arremp (intcount), CEMP) .lname & chr (13)

NEXT

GetEmpdata = strempdata

END FUNCTION

Public Sub Changeempdata ()

DIM Objemp as CEMP

For Each Objemp in Arremp

Objemp.fname = "firstname"

Objemp.lname = "lastname"

NEXT

End Sub

END CLASS

Public Class CEMP

Private mstrfname as string

Private mstrlname as string

Public property fname () AS STRING

Get

FNAME = MSTRFNAME

END GET

Set (byval value as string)

mstrfname = value

End set

End Property

Public property lname () AS STRING

Get

LName = mstrlname

END GET

Set (byval value as string)

MStrlName = Value

End set

End Property

Public Sub New ()

End Sub

Public Sub New (Byval Info As SerializationInfo, Byval Context As StreamingContext, Byval Intentex AS Integer)

MStrFName = CSTR (INFO.GETVALUE ("Emp_FName" & infinDex, gettype (string))

MStrlName = CSTR (INFO.GETVALUE ("EMP_LNAME" & INTINDEX, GETTYPE (STRING)))

End Sub

Public Sub GetObjectData (byval Info As SerializationInfo, Byval Context As Street Info.addValue ("Emp_FName" & intIndex, mstrfname)

Info.addValue ("EMP_LNAME" & INTINDEX, MSTRLNAME)

End Sub

END CLASS

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

New Post(0)