zoukankan      html  css  js  c++  java
  • 创建型设计模式-原型模式(单例) MemberwiseClone()

    原型模式(Prototype Pattern):是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式;

    1: 专门用来快速生产对象!
    2: 把单例实例对象在内存中拷贝一份
    3: 避免单列对象污染

    实例

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    
    namespace SingletonPattern
    {
        /// <summary>
        ///原型模式
        /// </summary>
        public class SingletonFifth
        {
            public int Id { get; set; }
    
            public string Name { get; set; }
    
            /// <summary>
            /// 1.饿汉式写法 
            /// </summary>
            private static SingletonFifth singleton = new SingletonFifth(); //静态,只执行一次
             
            /// <summary>
            /// 2.私有化构造函数
            /// </summary>
            private SingletonFifth()
            {
                Console.WriteLine($"{typeof(SingletonFifth).Name}被构造。。。");
            }
    
            /// <summary>
            /// 懒汉式学习 (MemberwiseClone()基于内存克隆)
            /// </summary>
            /// <returns></returns>
            public static SingletonFifth CreateInstance()
            {  
                return (SingletonFifth)singleton.MemberwiseClone();  //基于内存克隆
            }
    
            public static void Test()
            {
                Console.WriteLine("this is Test...");
            }
        }
    }
    
    
    年轻人,现在没钱算什么,以后没钱的日子还多着呢(-_-
  • 相关阅读:
    github加速
    aardio类的例子
    aardio调用dll
    荔枝派nano例子
    我的书单
    架构设计之熔断设计
    【leetcode】两数之和
    K-近邻算法(KNN)
    CLion之C++框架篇-优化开源框架,引入curl,实现get方式获取资源(四)
    CLion之C++框架篇-优化框架,引入boost(三)
  • 原文地址:https://www.cnblogs.com/lgq168/p/13868030.html
Copyright © 2011-2022 走看看