Design patterns, principles

1. Object clone (copy prototype model)

 

 

 

  • scenes to be used:
    • You do not want to hold the referenced object type is modified (Our Common Object a = new Object (); Object b; b = a; copy of this form of the code is a reference to that object's address in memory, a and b object is still pointing to the same object)
    • Guarantee reference type parameter is not modified by other methods
    • Values ​​clones can be used as an argument
    • An object database needs to be created after a costly operation, we can cache the object and returns a clone of it directly at the next call, when you need to update the database to reduce calls to the database
  • Use advantages:
    • Create duplicate objects, while ensuring performance (performance improvement)
    • Constraint constructor escape;
  • Cloning Methods:
    • Shallow copy: Achieved interfaces ICloneable interface call MemberwiseClone () method Clone () method.
    • Deep copy: the object is serialized as data, the data again deserialize new object
  • Case:
    • Nop.Core.Domain.Common.Address
    • Nop.Web.Models.Polls.PollModel
    • Nop.Web.Models.News.HomepageNewsItemsModel
  • related articles:

Guess you like

Origin www.cnblogs.com/gougou1981/p/12186202.html