Showing posts with label Proxy. Show all posts
Showing posts with label Proxy. Show all posts

Saturday, May 08, 2021

Proxy pattern php

Create interface Subject add code:
public function request(): void;
create class RealSubject implements Subject implement missing method repace TODO with
echo "RealSubject: Handling request.<br/>";
create Proxy and have it implements Subject and add code to request function: The most common applications of the Proxy pattern are lazy loading, caching, controlling the access, logging, etc. A Proxy can perform one of these things and then, depending on the result, pass it to the execution to the same method in a linked RealSubject object.
if ($this->checkAccess()) {
    $this->realSubject->request();
    $this->logAccess();
}
The Proxy maintains a reference to the object of the RealSubject class. It can be either lazy-loaded or passed to the Proxy by the client. We then add code
private $realSubject;

public function __construct(RealSubject $realSubject)
{
    $this->realSubject = $realSubject;
}
at the bottom we add:
private function checkAccess(): bool
{
    // Some real checks should go here.
    echo "Proxy: Checking access prior to firing a real request.<br/>";
     return true;
}

private function logAccess(): void
{
    echo "Proxy: Logging the time of request.<br/>";
}
last step is to go to index and add the following includes
include_once ('Subject.php');
include_once ('RealSubject.php');
include_once ('Proxy.php');
we also add the function
function clientCode(Subject $subject)
{
    // ...
    $subject->request();
    // ...
}
and to run our demo we add:
echo "Client: Executing the client code with a real subject:<br/>";
$realSubject = new RealSubject;
clientCode($realSubject);

echo "<br/>";

echo "Client: Executing the same client code with a proxy:<br/>";
$proxy = new Proxy($realSubject);
clientCode($proxy);
**RUN**
Client: Executing the client code with a real subject:
RealSubject: Handling request.

Client: Executing the same client code with a proxy:
Proxy: Checking access prior to firing a real request.
RealSubject: Handling request.
Proxy: Logging the time of request.

The Ray Code is AWESOME!!!
wikipedia

Find Ray on:

facebook
youtube
The Ray Code
Ray Andrade

Friday, May 07, 2021

Proxy pattern c#

Proxy is a structural design pattern that lets you provide a substitute/placeholder for another object. The proxy controls access to the original object, allowing you to perform work before or after the request to the original object. First create the interface Subject. The Subject interface declares common operations for both the RealSubject and the Proxy. As long as the client works with RealSubject using this interface, you'll be able to pass it to a proxy instead of the real subject.
public interface Subject
{
   void Request();
}
Create class RealSubject and remove the public that was added with the code generation. Extend with : Subject to Request the code is:
class RealSubject : Subject
{
    public void Request()
    {
       Console.WriteLine("Handling Request.");
    }
}
Create a class called Proxy and extend it with : Subject remove the public ans implment missing members.
class Proxy : Subject
 {
    private RealSubject _realSubject;
        
    public Proxy(RealSubject realSubject)
    {
       this._realSubject = realSubject;
    }
        
    public void Request()
    {
        if (this.CheckAccess())
        {
            this._realSubject.Request();
            this.LogAccess();
        }
    }
        
    public bool CheckAccess()
    {
        // Some real checks should go here.
        Console.WriteLine("Checking access prior to firing a real request.");
        return true;
    }
        
    public void LogAccess()
    {
        Console.WriteLine("Proxy: Logging the time of request.");
    }
}
Create a class called Proxy and extend it with : Subject The client code is supposed to work with all objects (both subjects and proxies) via the Subject interface in order to support both real subjects and proxies. In real life, however, clients mostly work with their real subjects directly. In this case, to implement the pattern more easily, you can extend your proxy from the real subject's class. We creat the class Client and keep public
public class Client
    {
    public void ClientCode(Subject subject)
    {
        subject.Request();
    }
}
We now goto the Program file and we add the static void Main. The code will be
static void Main(string[] args)
{
    Client client = new Client();
            
    Console.WriteLine("Executing the client code with a real subject:");
    RealSubject realSubject = new RealSubject();
    client.ClientCode(realSubject);

    Console.WriteLine();

    Console.WriteLine("Executing the same client code with a proxy:");
    Proxy proxy = new Proxy(realSubject);
    client.ClientCode(proxy);
}
When we compile and run we get:
Executing the client code with a real subject:
Handling Request.

Executing the same client code with a proxy:
Checking access prior to firing a real request.
Handling Request.
Proxy: Logging the time of request.

The Ray Code is AWESOME!!!
wikipedia

Find Ray on:

facebook
youtube
The Ray Code
Ray Andrade

Wednesday, May 05, 2021

Proxy pattern c++

The Proxy pattern is a structural design pattern that provides an object that acts as a substitute for a real service object used by the client. The Proxy receives client requests, does some work and then passes the request to a service object.

The Subject is an interface that declares the common operations for both RealSubject and the Proxy. As long as the client works with the RealSubject using this interface, you'll be able to pass it a proxy instead of a real subject.

The code for the Subject interface will be:
class Subject {
public:
    virtual void Request() const = 0;
};

Now let's turn our attention to the RealSubject.

The RealSubject contains some core business logic. Usually, the RealSubject(s) are capable of doing some useful work.
class RealSubject : public Subject {
 public:
  void Request() const override {
    std::cout << "The RealSubject is Handling the request.\n";
  }
};
A Proxy can solve these issues without any changes in the RealSubject's code because the Proxy has an interface identical to the RealSubject. The Proxy maintains a reference to an object of the RealSubject class. It can be either lazy-loaded or passed to the Proxy by the client.

The most common applications of the Proxy pattern are lazy loading, caching, controlling the access, logging, etc. A Proxy can perform one of these things and then, depending on the result, pass the execution to the same method in a linked RealSubject object.
#include "RealSubject.h"

class Proxy : public Subject {

private:
    RealSubject *real_subject_;

    bool CheckAccess() const {
        // Some real checks should go here.
        std::cout << "Checking access prior to firing a real request.\n";
        return true;
    }
    void LogAccess() const {
        std::cout << "Logging the time of request.\n";
    }

public:
    Proxy(RealSubject *real_subject) : real_subject_(new RealSubject(*real_subject)) {
    }

    ~Proxy() {
        delete real_subject_;
    }
    void Request() const override {
        if (this->CheckAccess()) {
            this->real_subject_->Request();
            this->LogAccess();
        }
    }
};
Now let's build an example in the main.cpp We need to incude the Proxy.h
#include "Proxy.h"

void ClientCode(const Subject &subject) {
    subject.Request();
}

And when we create the main method we declare a new RealSubject and do some work.
int main() {
    std::cout << "Executing the client code with a real subject:\n";
    RealSubject *real_subject = new RealSubject;
    ClientCode(*real_subject);
    std::cout << "\n";
    std::cout << "Executing the same client code with a proxy:\n";
    Proxy *proxy = new Proxy(real_subject);
    ClientCode(*proxy);

    delete real_subject;
    delete proxy;
    return 0;
}
We now compile and run. We should get:
Executing the client code with a real subject:
The RealSubject is Handling the request.


Executing the same client code with a proxy: Checking access prior to firing a real request. The RealSubject is Handling the request. Logging the time of request.

The Ray Code is AWESOME!!!
wikipedia

Find Ray on:

facebook
youtube
The Ray Code
Ray Andrade

JavaScript Command Design Pattern Explained | UML, Code & SWOT Analysis

🕹️ What Is the Command Design Pattern? The Command Design Pattern is one of the 11 Behavioral Design Patterns described ...