[Special] Design Pattern - Structural Patterns - Proxy Pattern

50 篇文章 0 订阅
37 篇文章 0 订阅

2007

Section 7, Chapter 4


Proxy Pattern


Concept

Proxies generally provide a stub or placeholder for an actual implementation object. It allows the creation, security, or accessibility to be handled outside the current code domain, or latently handled on another address space.


Use

We wish to hide and encapsulate the security protocols, initialization, and instance creation of the Subject object. In addition, we need a way to share desired functionality either remotely or between otherwise incompatible domains.


Design

Four different types:

  • Remote Proxy
  • Virtual Proxy
  • Protection Proxy
  • Smart Reference/Smart Pointer



Illustration

The proxy object will handle how and when we create our Subject object implementation. It will also mask any initialization and the pointer into the domain where the Subject object exists.




// Subject
public interface ISubject
{
	string GetRequest();
}

// RealSubject
class Subject : MarshalByRefObject, ISubject
{
	public string GetRequest(){ return "Request Found"; }
}

// Remote Proxy Object
class SubjectProxy : ISubject
{
	private ISubject _subject;
	public SubjectProxy(
		string domainName,
		System.Security.Policy.Evidence evidence,
		System.AppDomainSetup appSetup)
	{
		AppDomain domain = System.AppDomain.CreateDomain(domainName, evidence, appSetup);
		//creates a {System.Runtime.Remoting.Proxies.__TransparentProxy}
		//proxy stub to Examples.Proxy.Subject
		_subject = (ISubject)domain.CreateInstanceAndUnwrap("Examples", "Examples.Proxy.Subject");
	}

	public string GetRequest()
	{
		return _subject.GetRequest();
	}
}


// Specify the domain details
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = path;
setup.ConfigurationFile = "(some file)";

// Set up the Evidence
Evidence baseEvidence = AppDomain.CurrentDomain.Evidence;
Evidence evidence = new Evidence(baseEvidence);
evidence.AddAssembly("(some assembly)");
evidence.AddHost("(some host)");

SubjectProxy proxy = new SubjectProxy("SubjectDomain", evidence, setup);
return proxy.GetRequest();




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值