Introduction to designpattern_prototype

Source: Internet
Author: User

UML:

Sample:

  • Prototype()
    • Declares an interface for cloning itself
    • Define the interface for copying objects
  • Concreteprototype()
    • Implements an operation for cloning itself
    • Copy your own operations
  • Client()
      • Creates a new object by asking a prototype to clone itself
      • Create a new object to copy by accessing prototype

    Prototype:

     

     1     Abstract    Class  Prototype
    2 {
    3 Private String _ Id;
    4 Public Prototype ( String ID)
    5 {
    6 This . _ Id = ID;
    7 }
    8 Public String ID
    9 {
    10 Get { Return _ Id ;}
    11 }
    12 Public Abstract Prototype clone ();
    13 }

     

    Concreteprototype1:

     

      1     Class  Concreteprototype1: Prototype
    2 {
    3 Public Concreteprototype1 ( String ID)
    4 : Base (ID)
    5 {}
    6
    7 Public Override Prototype clone ()
    8 {
    9 Return (Prototype) This . Memberwiseclone ();
    10 }
    11 }

     

    Concreteprototype2:

     1     Class  Concreteprototype2
    2 {
    3 Public Concreteprototype2 ( String ID)
    4 : Base (ID)
    5 {}
    6 Public Override Prototype clone ()
    7 {
    8 Return (Prototype) This . Memberwiseclone ();
    9 }
    10 }

     

    Program:

      1     # Region  Prototype  
    2 Concreteprototype1 p1 = New Concreteprototype1 ( " I " );
    3 Concreteprototype1 C1 = (Concreteprototype1) p1.clone ();
    4 Console. writeline ( " Cloned: {0} " , C1.id );
    5 Concreteprototype2 p2 = New Concreteprototype2 ( " II " );
    6 Concreteprototype2 C2 = (Concreteprototype2) p2.clone ();
    7 Console. writeline ( " Cloned: {0} " , C2.id );
    8 Console. readkey ();
    9 # Endregion

     

    The prototype is to request a prototype to clone the object itself.

    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.