代理模式 原型模式
来源:互联网 发布:火狐浏览器mac版 编辑:程序博客网 时间:2024/06/08 13:54
代理模式:为其他对象提供一种代理,以控制对这个对象的访问。
class Program { static void Main(string[] args) { Proxy proxy = new Proxy(); proxy.Request(); Console.Read(); } } abstract class Subject { public abstract void Request(); } class RealSubject : Subject { public override void Request() { Console.WriteLine("真实的请求"); } } class Proxy : Subject { RealSubject realSubject; public override void Request() { if (realSubject == null) { realSubject = new RealSubject(); } realSubject.Request(); } }
原型模式:用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。
另外,注意浅拷贝和深拷贝
class Program { static void Main(string[] args) { ConcretePrototype1 p1 = new ConcretePrototype1("I"); ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone(); Console.WriteLine("Cloned: {0}", c1.Id); ConcretePrototype2 p2 = new ConcretePrototype2("II"); ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone(); Console.WriteLine("Cloned: {0}", c2.Id); // Wait for user Console.Read(); } } abstract class Prototype { private string id; // Constructor public Prototype(string id) { this.id = id; } // Property public string Id { get { return id; } } public abstract Prototype Clone(); } class ConcretePrototype1 : Prototype { // Constructor public ConcretePrototype1(string id) : base(id) { } public override Prototype Clone() { // Shallow copy return (Prototype)this.MemberwiseClone(); } } class ConcretePrototype2 : Prototype { // Constructor public ConcretePrototype2(string id) : base(id) { } public override Prototype Clone() { // Shallow copy return (Prototype)this.MemberwiseClone(); } }
0 0
- 代理模式 原型模式
- 慕课网----大话PHP设计模式 五(原型模式,装饰器模式,迭代器模式,代理模式)
- 原型模式
- 原型模式
- 原型模式
- 原型模式
- 原型模式
- 原型模式
- 原型模式
- 原型模式
- 原型模式
- 原型 模式
- 原型模式
- 原型模式
- 原型模式
- 原型模式
- 原型模式
- 原型模式
- 第七周 CSS3 长度单位
- 设置代理服务器
- MFC 自定义消息实现与响应
- spring学习笔记(六)
- robocode学习系列(一)--基础内容
- 代理模式 原型模式
- 取数字
- 汇编程序:查表法找ASCII
- Java8:接口新特性
- 压力传感器电路
- POJ 3984 迷宫问题(dfs)
- Java基础--运算符
- 经典手眼标定算法之Tsai-Lenz的OpenCV实现
- 使用逻辑分析进行UART通信协议分析