Design Pattern-Proxy介紹
演說者:林政融
日期:2013/08/05
Agenda
 C#的Design Pattern
◦ Creational Patterns
◦ Structural Patterns
◦ Behavioral Patterns
 Design Pattern – Proxy
 Dy...
C#的Design Pattern
Creational Patterns
 Abstract Factory Creates an instance
of several families of classes
 Builder Separates object const...
Structural Patterns
 Adapter Match interfaces of different classes
 Bridge Separates an object’s interface from
its impl...
Behavioral Patterns
 Chain of Resp. A way of passing a request
between a chain of objects
 Command Encapsulate a command...
Behavioral Patterns(continue)
 Observer A way of notifying change to
a number of classes
 State Alter an object's behavi...
Design Pattern – Proxy
Definition
 Provide a surrogate or placeholder for
another object to control access to it.
UML class diagram
Sample Code
using System;
namespace DoFactory.GangOfFour.Proxy.Structural
{
/// <summary>
/// MainApp startup class for St...
Sample Code(Subject)
/// <summary>
/// The 'Subject' abstract class
/// </summary>
abstract class Subject
{
public abstrac...
Sample Code(RealSubject)
/// <summary>
/// The 'RealSubject' class
/// </summary>
class RealSubject : Subject
{
public ove...
Sample Code(Proxy)
/// <summary>
/// The 'Proxy' class
/// </summary>
class Proxy : Subject
{
private RealSubject _realSub...
Sample Code2
using System;
namespace DoFactory.GangOfFour.Proxy.RealWorld
{
/// <summary>
/// MainApp startup class for Re...
Sample Code2(IMath)
/// <summary>
/// The 'Subject interface
/// </summary>
public interface IMath
{
double Add(double x, ...
Sample Code2(Math)
/// <summary>
/// The 'RealSubject' class
/// </summary>
class Math : IMath
{
public double Add(double ...
Sample Code2(MathProxy)
/// <summary>
/// The 'Proxy Object' class
/// </summary>
class MathProxy : IMath
{
private Math _...
Dynamic Proxy
Use of Static Proxy
 Situation: when do add, save the result
to DB
public double Add(double x, double y)
{
var result = _...
Static Proxy
 Advantage: easy to revise the original
method
 Disadvantage: just only revise the
method that use the decl...
Dynamic Proxy(Interface)
public interface IProxyInvocationHandler {
Object Invoke( Object proxy,
MethodInfo method,
Object...
Dynamic Proxy(Impl)
Public Object Invoke(Object proxy,
System.Reflection.MethodInfo method,
Object[] parameters)
{
Object ...
Dynamic Proxy(Impl)
Public Object Invoke(Object proxy,
System.Reflection.MethodInfo method,
Object[] parameters)
{
Object ...
Use of Dynamic Proxy
public interface ITest
{
void TestFunctionOne();
Object TestFunctionTwo( Object a, Object b );
}
publ...
Use of Dynamic
Proxy(continue)
public class TestBed
{
static void Main( string[] args )
{
ITest test = (ITest)SecurityProx...
References
http://www.dofactory.com/Default.aspx
http://www.codeproject.com/Articles/5511/Dynam
ic-Proxy-Creation-Using-C-...
Upcoming SlideShare
Loading in...5
×

Design pattern proxy介紹 20130805

54

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
54
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Design pattern proxy介紹 20130805

  1. 1. Design Pattern-Proxy介紹 演說者:林政融 日期:2013/08/05
  2. 2. Agenda  C#的Design Pattern ◦ Creational Patterns ◦ Structural Patterns ◦ Behavioral Patterns  Design Pattern – Proxy  Dynamic Proxy
  3. 3. C#的Design Pattern
  4. 4. Creational Patterns  Abstract Factory Creates an instance of several families of classes  Builder Separates object construction from its representation  Factory Method Creates an instance of several derived classes  Prototype A fully initialized instance to be copied or cloned  Singleton A class of which only a single instance can exist
  5. 5. Structural Patterns  Adapter Match interfaces of different classes  Bridge Separates an object’s interface from its implementation  Composite A tree structure of simple and composite objects  Decorator Add responsibilities to objects dynamically  Façade A single class that represents an entire subsystem  Flyweight A fine-grained instance used for efficient sharing  Proxy An object representing another object
  6. 6. Behavioral Patterns  Chain of Resp. A way of passing a request between a chain of objects  Command Encapsulate a command request as an object  Interpreter A way to include language elements in a program  Iterator Sequentially access the elements of a collection  Mediator Defines simplified communication between classes  Memento Capture and restore an object's internal state
  7. 7. Behavioral Patterns(continue)  Observer A way of notifying change to a number of classes  State Alter an object's behavior when its state changes  Strategy Encapsulates an algorithm inside a class  Template Method Defer the exact steps of an algorithm to a subclass  Visitor Defines a new operation to a class without change
  8. 8. Design Pattern – Proxy
  9. 9. Definition  Provide a surrogate or placeholder for another object to control access to it.
  10. 10. UML class diagram
  11. 11. Sample Code using System; namespace DoFactory.GangOfFour.Proxy.Structural { /// <summary> /// MainApp startup class for Structural /// Proxy Design Pattern. /// </summary> class MainApp { /// <summary> /// Entry point into console application. /// </summary> static void Main() { // Create proxy and request a service Proxy proxy = new Proxy(); proxy.Request(); // Wait for user Console.ReadKey(); } }
  12. 12. Sample Code(Subject) /// <summary> /// The 'Subject' abstract class /// </summary> abstract class Subject { public abstract void Request(); }
  13. 13. Sample Code(RealSubject) /// <summary> /// The 'RealSubject' class /// </summary> class RealSubject : Subject { public override void Request() { Console.WriteLine("Called RealSubject.Request()"); } }
  14. 14. Sample Code(Proxy) /// <summary> /// The 'Proxy' class /// </summary> class Proxy : Subject { private RealSubject _realSubject; public override void Request() { // Use 'lazy initialization' if (_realSubject == null) { _realSubject = new RealSubject(); } _realSubject.Request(); } } }
  15. 15. Sample Code2 using System; namespace DoFactory.GangOfFour.Proxy.RealWorld { /// <summary> /// MainApp startup class for Real-World /// Proxy Design Pattern. /// </summary> class MainApp { /// <summary> /// Entry point into console application. /// </summary> static void Main() { // Create math proxy MathProxy proxy = new MathProxy(); // Do the math Console.WriteLine("4 + 2 = " + proxy.Add(4, 2)); Console.WriteLine("4 - 2 = " + proxy.Sub(4, 2)); Console.WriteLine("4 * 2 = " + proxy.Mul(4, 2)); Console.WriteLine("4 / 2 = " + proxy.Div(4, 2)); // Wait for user Console.ReadKey(); } }
  16. 16. Sample Code2(IMath) /// <summary> /// The 'Subject interface /// </summary> public interface IMath { double Add(double x, double y); double Sub(double x, double y); double Mul(double x, double y); double Div(double x, double y); }
  17. 17. Sample Code2(Math) /// <summary> /// The 'RealSubject' class /// </summary> class Math : IMath { public double Add(double x, double y) { return x + y; } public double Sub(double x, double y) { return x - y; } public double Mul(double x, double y) { return x * y; } public double Div(double x, double y) { return x / y; } }
  18. 18. Sample Code2(MathProxy) /// <summary> /// The 'Proxy Object' class /// </summary> class MathProxy : IMath { private Math _math = new Math(); public double Add(double x, double y) { return _math.Add(x, y); } public double Sub(double x, double y) { return _math.Sub(x, y); } public double Mul(double x, double y) { return _math.Mul(x, y); } public double Div(double x, double y) { return _math.Div(x, y); } } }
  19. 19. Dynamic Proxy
  20. 20. Use of Static Proxy  Situation: when do add, save the result to DB public double Add(double x, double y) { var result = _math.Add(x, y); // Implement the method to save the result to db save(result); return result; }
  21. 21. Static Proxy  Advantage: easy to revise the original method  Disadvantage: just only revise the method that use the declared proxy
  22. 22. Dynamic Proxy(Interface) public interface IProxyInvocationHandler { Object Invoke( Object proxy, MethodInfo method, Object[] parameters ); }
  23. 23. Dynamic Proxy(Impl) Public Object Invoke(Object proxy, System.Reflection.MethodInfo method, Object[] parameters) { Object retVal = null; // is invoked, otherwise an exception is thrown indicating they // do not have permission if ( SecurityManager.IsMethodInRole( userRole, method.Name ) ) { // The actual method is invoked retVal = method.Invoke( obj, parameters ); } else { throw new IllegalSecurityException( "Invalid permission to invoke " + method.Name ); } return retVal; }
  24. 24. Dynamic Proxy(Impl) Public Object Invoke(Object proxy, System.Reflection.MethodInfo method, Object[] parameters) { Object retVal = null; // is invoked, otherwise an exception is thrown indicating they // do not have permission if ( SecurityManager.IsMethodInRole( userRole, method.Name ) ) { // The actual method is invoked retVal = method.Invoke( obj, parameters ); } else { throw new IllegalSecurityException( "Invalid permission to invoke " + method.Name ); } return retVal; }
  25. 25. Use of Dynamic Proxy public interface ITest { void TestFunctionOne(); Object TestFunctionTwo( Object a, Object b ); } public class TestImpl : ITest { public void TestFunctionOne() { Console.WriteLine( "In TestImpl.TestFunctionOne()" ); } public Object TestFunctionTwo( Object a, Object b ) { Console.WriteLine( "In TestImpl.TestFunctionTwo( Object a, Object b )" ); return null; }
  26. 26. Use of Dynamic Proxy(continue) public class TestBed { static void Main( string[] args ) { ITest test = (ITest)SecurityProxy.NewInstance( new TestImpl() ); test.TestFunctionOne(); test.TestFunctionTwo( new Object(), new Object() ); } }
  27. 27. References http://www.dofactory.com/Default.aspx http://www.codeproject.com/Articles/5511/Dynam ic-Proxy-Creation-Using-C-Emit
  1. ¿Le ha llamado la atención una diapositiva en particular?

    Recortar diapositivas es una manera útil de recopilar información importante para consultarla más tarde.

×