Design Patterns (four) - proxy mode (Proxy)

Participants proxy mode are: a constraint, an agent who is a broker, a caller

Implementing a proxy mode is very simple; or that house, to open the door to this operation, I replaced a remote to unlock the door, then I can through this server remotely unlock a remote connection, so my family did not take the keys, I can remotely unlocked, and does not require a key, or even need to know the existence of the lock, I realized what the code

    ///  <Summary> 
    /// abstract interface for restraining
     ///  </ Summary> 
    public  interface IAccess 
    { 
        void Access (); 
    } 
    ///  <Summary> 
    /// user class
     ///  </ Summary> 
    public  class Room: IAccess 
    { 
        ///  <Summary> 
        /// into the house operation
         ///  </ Summary> 
        public  void Access () 
        { 
            Console.WriteLine ( " into the house " ); 
        } 
    } 
///  <Summary>
/// 远程开门类
/// </summary>
public class RemoteDoor : IAccess
{
private Room room = new Room();
/// <summary>
/// 
/// </summary>
public void Access()
{
room.Access();
}
}

 

So I can call

RemoteDoor class Access () method to indirectly call Access Room class () method, and this looks very sad to realize Ha, why do not I call the class method Room direct it, have one around, this is not sick it ?
At first I thought so come forward, but think carefully, if we do not want the caller to know R OOM this class or that class I do not want the caller can access this class directly, and that the benefits of the proxy mode on the highlights out; 
benefits: 1, hide the location of the object, the object introduced in a certain indirect;
   2, can provide a copy-on-write (copy-on-write) optimized way, never used, ready to find a time to carefully look at to say.
   3, AOP can do some similar operations, such as counting like main line of business needs non-

feeling after reading the proxy mode and proxy mode decorator pattern like ah, but there are differences between the two,
the difference between: 1, without having to proxy mode examples of real business objects, you only need to instantiate an object broker, for the caller, the real business object is unknowable; and decorator real business need to instantiate an object, and then by the decorator constructor this object passing arguments to functions, recursively constructed at runtime, when the business object is clear;
   2, point of interest is not the same, the proxy model is to control access to the concerned real business object decorator pattern is a concern Add dynamic method on the object.

Guess you like

Origin www.cnblogs.com/yuchenghao/p/11980521.html