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();
            }
        }
    }
  • 相关阅读:
    自己动手写个小框架之三
    自己动手写个小框架之四
    定制CentOS
    简单C语言文法
    Python基础
    中文词频统计
    编译原理
    英文词频统计
    熟悉常用的Linux操作
    大数据概述
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/4630445.html
Copyright © 2011-2022 走看看