惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Jina AI
Jina AI
月光博客
月光博客
F
Fortinet All Blogs
Stack Overflow Blog
Stack Overflow Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
小众软件
小众软件
博客园 - 三生石上(FineUI控件)
博客园 - 司徒正美
P
Proofpoint News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
J
Java Code Geeks
L
LangChain Blog
博客园 - 聂微东
G
Google Developers Blog
博客园 - Franky

博客园 - Tristan(GuoZhijian)

Core Design Patterns(16) Chain of Responsibility 职责链模式 Core Design Patterns(15) Template Method 模版方法模式 Core Design Patterns(14) State 状态模式 Core Design Patterns(13) Strategy 策略模式 Core Design Patterns(12) Builder 建造者模式 Core Design Patterns(11) Abstract Factory 抽象工厂模式 Core Design Patterns(10) Singleton 单例模式 Core Design Patterns(9) Factory Method 工厂方法模式 Core Design Patterns(7) Facade 外观模式 Core Design Patterns(6) Adapter 适配器模式 Core Design Patterns(5) Flyweight 享元模式 Core Design Patterns(4) Composite 组合模式 Core Design Patterns(3) Bridge 桥接模式 Core Design Patterns(2) Proxy 代理模式 Core Design Patterns(1) Decorator 装饰模式 老调重弹:插件式框架开发的一个简单应用 Behavior模型应用:可拖动的div容器 Microsoft Asp.Net Ajax框架入门(13) PageRequestManager Microsoft Asp.Net Ajax框架入门(12) 了解异步通信层
Core Design Patterns(8) Prototype 原型模式
Tristan(GuoZhijian) · 2008-03-14 · via 博客园 - Tristan(GuoZhijian)

VS 2008

当我们需要实例化一个类的多个实例,并且实例化的代价比较昂贵的时候,可以使用原型模式。

1. 模式UML图

2. 应用

IPrototype.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPattern.Prototype.BLL {

    
public interface IPrototype<T> {

        T Clone();
        T DeepCopy();
    }

}

UserInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace DesignPattern.Prototype.BLL {

    [Serializable]
    
public class UserInfo : IPrototype<UserInfo> {

        
public int UserId {
            
get;
            
set;
        }

        
public string UserName {
            
get;
            
set;
        }

        
public PetInfo Pet {
            
get;
            
set;
        }


        
IPrototype Members
    }

}

PetInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace DesignPattern.Prototype.BLL {

    [Serializable]
    
public class PetInfo {

        
public string PetName {
            
get;
            
set;
        }

    }

}

Client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DesignPattern.Prototype.BLL;

namespace DesignPattern.Prototype {
    
class Program {
        
static void Main(string[] args) {
            UserInfo u 
= new UserInfo() {
                UserId 
= 1,
                UserName 
= "guozhijian",
                Pet 
= new PetInfo() {
                    PetName 
= "nana"
                }

            }
;
            UserInfo uCopy 
= u.Clone();

            Console.WriteLine(
"{0},{1},{2}", uCopy.UserId.ToString(), uCopy.UserName, uCopy.Pet.PetName);

            UserInfo uDeepCopy 
= u.DeepCopy();
            uDeepCopy.Pet.PetName 
= "sasa";
            Console.WriteLine(
"{0},{1},{2}", uDeepCopy.UserId.ToString(), uDeepCopy.UserName, uDeepCopy.Pet.PetName);

            Console.WriteLine(u.Pet.PetName);


        }

    }

}

Output

posted on 2008-03-14 21:48  Tristan(GuoZhijian)  阅读(595)  评论()    收藏  举报