Architecture should solve the problem of cloning a good object

Source: Internet
Author: User

The problem of cloning is a very basic problem encountered in the development of the program, and once involved, it takes a lot of effort from the developer. should be resolved in a unified architecture

The Iclone interface has a pit. But some implementations also rely on it. So the removal or removal is not lost. But it can be implemented on its own to remove this dependency.

The system should provide three functions:

Deepclone<t> (this T obj) where t:class

Shallowclone<t> (this T obj) where t:class

Copyto<t> (object source, T toentity)

Deepclone is a deep clone. Using MemoryStream + BinaryFormatter, this approach relies on: Serializable features, but with faster performance. can be detected.

Shallowclone is a shallow clone. Call with Reflection: MemberwiseClone way, do not understand why MemberwiseClone is protected, not public.

CopyTo is copied by the field value using Reflection mode.

When reflection becomes the only way, it is impossible to choose. It's okay. Net has Emit, can be used in the Base library implementation (development costs, but one investment, re-use).

1. The system to provide deep cloning, shallow cloning two ways to remove the Iclone interface.

There are many problems with the Iclone interface, such as when the base class implements the cloning interface, the subclass does not implement the problem: http://www.cnblogs.com/newsea/archive/2012/09/07/2674425.html

2. Base-Class copy constructors

Such as:

 Public  classCreature {pubicstringType {Get;Set; }  Public  intHeight {Get;Set ; }} Public classPerson: Creature { Public  stringSex {Get;Set; }} Public classFlower: Creature { Public stringColor {Get;Set; }} Bio SW=FindOne ();if(SW. Type = ="people") {    varren =Newperson (SW);}Else {    varHua =NewFlower (sw);}

The function of this base class copy constructor should be provided by the system instead of each class writing one. It is possible to achieve shallow cloning. Using the previous CopyTo method

var ren = new Person ();

ren = sw. CopyTo (ren);

var Hua = new Flower ();

Hua = sw. CopyTo (Hua);

3. Deep cloning, in the final analysis, is serialization.

Only clones that can be serialized are deep clones. Deep cloning is the best way to choose the serialization of performance.

There is no schema mechanism for serialization, which is bullying.

4. Cloning and serialization to resolve a problem to a reference

Class A refers to Class B, and Class B refers to Class A.

can detect, when mutual references, automatically broken off.

Architecture should solve the problem of cloning a good object

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.