Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The definition and example usage of .NET prototype pattern

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "the definition and example usage of .NET prototype pattern". In daily operation, I believe that many people have doubts about the definition and example usage of .NET prototype pattern. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "definition and example usage of .NET prototype pattern". Next, please follow the editor to study!

Definition of the prototype pattern:

Specify the type of object to be created with prototype instances, and create new objects by copying these prototypes.

Prototype pattern structure diagram:

The most important feature of the prototype pattern, which is a special pattern in the creative pattern, is to clone an existing object. There are two results of this cloning, one is shallow replication, the other is deep replication.

Creative mode is generally used to create a new object, and then we use this object to complete some object operations. Through the prototype pattern, we can quickly create an object without providing a special new () operation, which is undoubtedly a very effective way to quickly create a new object.

1. Prototype mode: shallow replication

Define an interface that represents all color object interfaces

/ Color API / public interface IColor {IColor Clone (); int Red {get; set;} int Green {get; set;} int Blue {get; set;}}

The specific implementation code of red is given:

Public class RedColor:IColor {public int Red {get; set;} public int Green {get; set;} public int Blue {get; set;} public IColor Clone () {return (IColor) this.MemberwiseClone ();}}

The specific test code is as follows:

Static void Main (string [] args) {IColor color = new RedColor (); color.Red = 255; Console.WriteLine ("color-red" + color.Red); / / 225 IColor color1 = color.Clone (); color1.Red = 224; Console.WriteLine ("color1-red" + color1.Red); / / 224 Console.WriteLine ("color-red" + color.Red); / / 225}

It can be found that when we modify the Red attribute value of the color1 object, it does not affect the parameter of the color property, that is, the modification of the copy of the object will not affect the state of the object itself.

two。 Prototype mode: deep replication

The situation of deep replication consideration will be relatively complicated, because it is possible that when objects have inheritance or reference relationships, we may need to pay attention to them. Generally speaking, deep replication on the one hand can adopt a simple scheme of deep copying objects, and it can also copy objects in the form of serialization. The following implements the prototype pattern in the form of serialization:

Using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication4 {/ color interface / public interface IColor {IColorDemo Clone (); int Red {get;set;} int Green {get;set;} int Blue {get;set;} Factroy f {get;set }} / [Serializable] public class Factroy {public string name {get; set;}} using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication4 {/ [Serializable] public class RedColor:IColor {public int Red {get; set;} public int Green {get; set } public int Blue {get; set;} public Factroy f {get; set;} public IColor Clone () {SerializableHelper s = new SerializableHelper (); string target = s.Serializable (this); return s.Derializable (target);}

Serialize the help class:

/ / serialization and deserialization helper classes / public class SerializableHelper {public string Serializable (object target) {using (MemoryStream stream = new MemoryStream ()) {new BinaryFormatter () .Serialize (stream, target); return Convert.ToBase64String (stream.ToArray ());} public object Derializable (string target) {byte [] targetArray = Convert.FromBase64String (target) Using (MemoryStream stream = new MemoryStream (targetArray)) {return new BinaryFormatter () .Deserialize (stream);} public T Derializable (string target) {return (T) Derializable (target);}}

Test:

Static void Main (string [] args) {IColor color = new RedColor (); color.Red = 255; color.f = new Factroy () {name= "Hubei Factory"}; Console.WriteLine ("color-Factroy:" + color.f.name); / / Hubei Factory IColor color1 = color.Clone (); color1.Red = 234; color1.f.name = "Beijing Factory"; Console.WriteLine ("color1- Factroy:" + color1.f.name) / / Beijing factory Console.WriteLine ("color-Factroy:" + color.f.name); / / Hubei factory Console.Read ();}

The running result of the program is as follows:

Conclusion: new objects are formed by serialization and deserialization. In fact, as long as the prototype pattern is used for object replication in the project, it can be copied deeply in the form of serialization.

At this point, the study of "the definition and instance usage of the .NET prototype pattern" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report