Structural patterns (proxy、bridge、decorator)学习笔记(一)

      一、proxy pattern是实际应用中,一般都用于比较复杂的一些对象当中,尤其是创建对象需要消耗比较多的资源的时候,但是在个人看来用得比较多的,应该还是在于,proxy对象可以很好的隐藏真实对象中一些无须实现和向外界透漏的一些方法和属性,而只需要把要调用的方法通过proxy出来,这样就可以减少系统不必要的开销。使用proxy pattern无异于把真实对象的访问权限给限制起来了,不同直接实例化真实对象,而是间接通过proxy来调用。为什么要这样设计呢?有时候,真实对象中的一些成员函数或者属性字段的访问权限是public,只要实例化了真实对象,就能直接调用了,但是有时却又不想让某些程序对其进行调用,以妨破坏和删改了数据。proxy就是一个很好的方式。例子如下:

ContractedBlock.gif ExpandedBlockStart.gif Code
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的一个解释。在此借用一下他的例子,以作自己学习自用。

ContractedBlock.gif ExpandedBlockStart.gif Code
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主要使用于动态的给对象添加字段属性和方法等。

ContractedBlock.gif ExpandedBlockStart.gif Code
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();
        }
    }
}
posted on 2009-01-01 15:59  yongshi123 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/yongshi123/articles/1366604.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值