This is the 9th day of my participation in the August More Text Challenge. For details, see:August is more challenging

  • 📢 welcome to like: 👍 collect ⭐ message 📝 If there is any mistake please correct it, send roses, hand left fragrance!
  • 📢 This article is written by Webmote.
  • 📢 author’s motto: life lies in tossing about, when you do not toss about life, life will start tossing you, let us work together! 💪 💪 💪

🎏 01. Proxy mode

Intent: To proxy objects that are inaccessible to users, allowing you to access objects indirectly.

Figurative say: just like add a proxy to your browser, then, you can go over the Changjiang Great Wall, see the outside world directly.

Friendly reminder: VPN guilty!

Problem area: Jump through the hoops and add something.

  • Encapsulate the tripartite library and add something to it,
  • Add protection control,
  • Filter requests,
  • Encapsulate native classes for resource and link protection
  • The product only provides interfaces, not concrete classes

Solution: We use UML diagrams to describe this.

The Proxy class has an instance member of BehindClass, so it has direct access to all the exposed methods in BehindClass, wrapping these methods to form an interface that can be called by other classes.

The Client is a user. It can instantiate the Proxy class internally and call Request to achieve the same effect as BehindClass Request. Of course, in the Proxy class, you can add additional permission security judgment, reference counting, etc. To control BehindClass method calls.

Effect:

  • Benefits:
  1. Hide direct calls to BehindClass;
  2. Open function calls to BehindClass;
  3. Easy to add permissions, reference counts and other related controls;
  4. You can lazily load heavy resources;
  • Limit:
  1. If the BehindClass class has many interfaces, the Proxy needs to type more similar code.

🎏 02. Dotnet Core source code appreciation

In aspnet Core source code, there is a DynamicClientProxy, using the proxy mode.

internal class DynamicClientProxy : DynamicObject
    {
        private readonly IClientProxy _clientProxy;

        public DynamicClientProxy(IClientProxy clientProxy)
        {
            _clientProxy = clientProxy;
        }

        public override bool TryInvokeMember(InvokeMemberBinder binder, object? []? args,out object? result){ result = _clientProxy.SendCoreAsync(binder.Name, args!) ;return true; }}Copy the code

Proxy mode is super simple, do little, just forward the request.

In this case, the TryInvokeMember method simply calls the SendCoreAsync method of _clientProxy directly to complete the method invocation of the proxy on the real object.

🎏 03. dotnet proxy class implementation

Here is an example. Let’s implement an abstract factory. The interface is defined as follows:

internal class BehindResource{
    public void PerformRWOperations()

    {

    Console.WriteLine("Performing Read Write operation on the Shared Folder"); }}class ResourceProxy 
    {
        private BehindResource folder;
        private Employee employee;
        public ResourceProxy(Employee emp)
        {
            employee = emp;
        }
        public void PerformRWOperations()
        {
            if (employee.Role.ToUpper() == "CEO" || employee.Role.ToUpper() =="MANAGER")
            {
                folder = new BehindResource();
                Console.WriteLine("Shared Folder Proxy makes call to the RealFolder 'PerformRWOperations method'");
                folder.PerformRWOperations();
            }
            else
            {
                Console.WriteLine("Shared Folder proxy says 'You don't have permission to access this folder'"); }}}Copy the code

Only qualified employees can access background resources. Other employees are not allowed to access resources.

Callers, which can be used directly as follows.

Console.WriteLine("Client passing employee with Role Developer to folderproxy");

Employee emp1 = new Employee("Anurag", "Anurag123", "Developer");

ResourceProxy proxy1 = new ResourceProxy(emp1);

proxy1.PerformRWOperations();

Console.WriteLine();

Console.WriteLine("Client passing employee with Role Manager to proxy");

Copy the code

Yes, through the above proxy class, we have added the security access permission for the backend resources. For the client, it cannot directly access the backend resources, it can only access the backend resources through the proxy class provided by us, so that we can easily control the resources.

With similar needs, can follow the agent model to solve.

🎏 04. Summary

If we have business designs that add secure access to existing objects, simplify apis for complex objects, provide interfaces to remote resources, add thread-safe functionality to existing classes without changing the existing class code, and the like, we can consider whether we can take advantage of code patterns in design patterns.

Routine summary, rational view!

Knot is what, knot is I want you to like but can not get the loneliness. 😳 😳 😳

👓 all see this, still care to point a like?

👓 all points like, still care about a collection?

Do you care about a comment when you have collected 👓?