Structural patterns (proxy、bridge、decorator)学习笔记(一)
一、proxy pattern是实际应用中,一般都用于比较复杂的一些对象当中,尤其是创建对象需要消耗比较多的资源的时候,但是在个人看来用得比较多的,应该还是在于,proxy对象可以很好的隐藏真实对象中一些无须实现和向外界透漏的一些方法和属性,而只需要把要调用的方法通过proxy出来,这样就可以减少系统不必要的开销。使用proxy pattern无异于把真实对象的访问权限给限制起来了,不同直接实例化真实对象,而是间接通过proxy来调用。为什么要这样设计呢?有时候,真实对象中的一些成员函数或者属性字段的访问权限是public,只要实例化了真实对象,就能直接调用了,但是有时却又不想让某些程序对其进行调用,以妨破坏和删改了数据。proxy就是一个很好的方式。例子如下:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace proxy_pattern
{
interface ISubject
{
void Request();
}
class Subject : ISubject
{
public void Request()
{
Console.WriteLine("i am the realguys");
}
}
class proxySubject : ISubject
{
ISubject subject;
public void Request()
{
if (subject == null)
{
Console.WriteLine("the realsubject is not yet active");
subject = new Subject();
}
Console.WriteLine(" i am the proxy guys,now staring invoking realsubject request method");
subject.Request();
}
}
class Program
{
static void Main(string[] args)
{
ISubject i = new proxySubject();
//这个是第一次激活对象
i.Request();
//这个对象已经不为空了,再次调用。
i.Request();
Console.ReadLine();
}
}
}
二、bridge pattern是一个很好的设计模式,在它的设计下很好的把抽象化和实现化给解耦了,使得他们能各自独立的变化。震宇老师在其博客的例子是对bridge pattern的一个解释。在此借用一下他的例子,以作自己学习自用。
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Bridge_Pattern
{
class Abctraction
{
protected Implementation implemente;
public Implementation Implemente
{
set { implemente = value; }
}
public virtual void operation()
{
implemente.operation();
}
}
class RedefinedAbctraction:Abctraction
{
public override void operation()
{
implemente.operation();
}
}
interface Implementation
{
void operation();
}
class ConcreteImplementeA:Implementation
{
public void operation()
{
Console.WriteLine("hello ,i am the ConcreteimplementA");
}
}
class ConcreteImplementB : Implementation
{
public void operation()
{
Console.WriteLine("hello , i am the ConcreteImplementB");
}
}
class Program
{
static void Main(string[] args)
{
Abctraction ab = new RedefinedAbctraction();
ab.Implemente = new ConcreteImplementeA();
ab.operation();
ab.Implemente = new ConcreteImplementB();
ab.operation();
Console.ReadLine();
}
}
}
三、decorator pattern主要使用于动态的给对象添加字段属性和方法等。
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace decorator_pattern
{
interface IComponent
{
string operation();
}
class Component : IComponent
{
public string operation()
{
return "i am programing";
}
}
class DecoratorA : IComponent
{
IComponent component;
public DecoratorA(IComponent c)
{
component = c;
}
public string operation()
{
string s = component.operation();
s += " and listening to the music !";
return s;
}
}
class DecorationB:IComponent
{
IComponent component;
public string addNewPro = " and missing my wife !";
public DecorationB(IComponent c)
{
component = c;
}
public string operation()
{
string s = component.operation();
s += " and typing the keyboard!";
return s;
}
public string addMethond()
{
return "add a methond to my component !";
}
}
class Client
{
public static void Display(string s,IComponent component)
{
Console.WriteLine(s+component.operation());
}
}
class Program
{
static void Main(string[] args)
{
IComponent component =new Component();
Client.Display("basic component",component);
Client.Display("A decorator",new DecoratorA(component));
Client.Display("B decorator",new DecorationB(component));
Client.Display("A-B decorator",new DecoratorA(new DecorationB(component)));
Client.Display("B-A decorator",new DecorationB(new DecoratorA(component)) );
DecorationB b = new DecorationB(component);
Console.WriteLine(b.addNewPro+b.addMethond());
Console.ReadLine();
}
}
}
转载于:https://www.cnblogs.com/yongshi123/articles/1366604.html
Structural patterns (proxy、bridge、decorator)学习笔记(一)相关推荐
- Angular input decorator学习笔记
https://angular.io/api/core/Input Input decorator只能用在Angular class字段里, 用于指定元数据,将class字段指定成input prop ...
- 设计模式学习笔记——桥接(Bridge)模式
设计模式学习笔记--桥接(Bridge)模式 @(设计模式)[设计模式, 桥接模式, bridge] 设计模式学习笔记桥接Bridge模式 基本介绍 类的功能层次结构 类的实现层次结构 使用桥接模式的 ...
- css view a if属性,uni-app学习笔记(2)view属性控制css样式
uni-app学习笔记(2)view属性控制css样式 uniapp通过标签属性来改变样式 当鼠标按下去的时候,他会变成这个样式 hover-class="box-active" ...
- [学习笔记]设计模式之Decorator
写在前面 为方便读者,本文已添加至索引: 设计模式 学习笔记索引 Decorator(装饰)模式,可以动态地给一个对象添加一些额外的职能.为了更好地理解这个模式,我们将时间线拉回Bridge模式笔记的 ...
- Proxy 补充学习笔记
Proxy 补充学习笔记 1.基本使用 let obj = new Proxy({},{get:function(target,key,receiver){console.log('获取值');},s ...
- Sharepoint学习笔记---Sandbox Solution-- Full Trust Proxy--开发实例之(2、在Webpart中访问Full Trust Proxy)...
上一篇Sharepoint学习笔记---Sandbox Solution-- Full Trust Proxy--开发实例之(1.创建一个能访问DataBase的Full Trust Proxy), ...
- 设计模式学习笔记——代理(Proxy)模式
设计模式学习笔记--代理(Proxy)模式 @(设计模式)[设计模式, 代理模式, proxy] 设计模式学习笔记代理Proxy模式 基本介绍 代理案例 类图 实现代码 Printable接口 Pri ...
- 设计模式学习笔记——装饰(Decorator)模式
设计模式学习笔记--装饰(Decorator)模式 @(设计模式)[设计模式, 装饰模式, decorator] 设计模式学习笔记装饰Decorator模式 基本介绍 装饰案例 类图 实现代码 Dis ...
- Python 学习笔记9(装饰器,decorator)
Python 学习笔记9(装饰器,decorator) 31 装饰器 装饰器可以对一个函数.方法或者类进行加工,是一种高级的python语法. 装饰函数 接收一个可调用对象作为输入参数,并返回一个新的 ...
最新文章
- 深入java_深入Java Final
- VirtualBox虚拟机中启用usb3.0却无法显示u盘的解决方法
- 抢领英饭碗?Facebook测试简历功能
- java comparator_Java 中如何指定集合元素的排序策略
- Qt+VS2013编译报错:'cl' 不是内部或外部命令,也不是可运行的程序
- linux中matlab,linux中Matlab编译m文件
- 《深入理解计算机系统》第七章 链接
- 汉字转换成拼音的代码(asp版)
- 6个原则、50条秘技提高HTML5应用及网站性能
- SQL Server 2008 R2 中英文 开发版/企业版/标准版 链接地址
- 理解Docker(8):Docker 存储之卷(Volume)
- Asp.net Mvc 获取json数据 简单案例
- 数据中心机房建设方案
- idea插件开发入门
- uva 473 - Raucous Rockers(dp)
- 债券属性「久期」的本质是什么?
- Sunday算法特征码搜索C++(支持通配符)
- 10月24日程序员HTML5,【通知】10月24日程序员节放假1天
- 太突然!著名音乐人陈道明去世,死因曝光,刺痛全网……
- 银行账户管理系统(一)