zoukankan      html  css  js  c++  java
  • 6.原型模式(Prototype Pattern)

    using System;
    
    namespace ConsoleApplication5
    {
        class Program
        {
            static void Main(string[] args)
            {
                // 孙悟空 原型
                MonkeyKingPrototype prototypeMonkeyKing = new ConcretePrototype("MonkeyKing");
    
                // 变一个
                MonkeyKingPrototype cloneMonkeyKing = prototypeMonkeyKing.Clone() as ConcretePrototype;
                Console.WriteLine("Cloned1:	" + cloneMonkeyKing.Id);
    
                // 变两个
                MonkeyKingPrototype cloneMonkeyKing2 = prototypeMonkeyKing.Clone() as ConcretePrototype;
                Console.WriteLine("Cloned2:	" + cloneMonkeyKing2.Id);
                Console.ReadLine();
            }
        }
    
        /// <summary>
        /// 孙悟空原型
        /// </summary>
        public  abstract class MonkeyKingPrototype
        {
            public string Id { get; set; }
            public MonkeyKingPrototype(string id)
            {
                this.Id = id;
            }
           
            // 克隆方法,即孙大圣说“变”
            public abstract MonkeyKingPrototype Clone();
        }
     
        /// <summary>
        /// 创建具体原型
        /// </summary>
        public class ConcretePrototype : MonkeyKingPrototype
        {
            public ConcretePrototype(string id)
                : base(id)
            { }
            /// <summary>
            /// 浅拷贝
            /// </summary>
            /// <returns></returns>
            public override MonkeyKingPrototype Clone()
            {
                // 调用MemberwiseClone方法实现的是浅拷贝,另外还有深拷贝
                return (MonkeyKingPrototype)this.MemberwiseClone();
            }
        }
    }
  • 相关阅读:
    jdbc操作元数据
    jdbc完成增删改查
    jdbc原理
    JDBC快速入门
    DOM_调查问卷效果2
    DOM_radio
    DOM_mail效果
    css基础
    《POSIX多线程程序设计》读书笔记
    《(转载)Bullet物理引擎不完全指南(Bullet Physics Engine not complete Guide)》
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/4630445.html
Copyright © 2011-2022 走看看