Proxy project pattern, why use?

4

This example proxy pattern can be found on Wikipedia.

reference: link

What I want to know is this:

  • Why should I use this pattern? When should I use it? Why is it good?
  • What would be a wrong way to do this pattern?
  • Are there any flaws in this pattern?
  • Anything else to add?
  • Example:

    import java.util.*;
    
    interface Imagem {
        public void mostrarImagem();
    }
    
    // no Sistema A
    class ImagemReal implements Imagem {
    private String nomeDoArquivo;
        public ImagemReal(String nomeDoArquivo) {
        this.nomeDoArquivo = nomeDoArquivo;
        carregarImagemDoDisco();
    }
    
    private void carregarImagemDoDisco() {
        System.out.println("Carregando " + nomeDoArquivo);
    }
    
    public void mostrarImagem() {
            System.out.println("Mostrando " + nomeDoArquivo);
        }
    }
    
    // no Sistema B
    class ImagemProxy implements Imagem {
        private String nomeDoArquivo;
        private Imagem imagem;
    
    public ImagemProxy(String nomeDoArquivo) {
        this.nomeDoArquivo = nomeDoArquivo;
    }
    public void mostrarImagem() {
        if(imagem == null)
          imagem = new ImagemReal(nomeDoArquivo);
        imagem.mostrarImagem();
    }
    }
    
    class ExemploProxy {
        public static void main(String[] args) {
            Imagem imagem1 = new ImagemProxy("ResOi_10MB_Foto1");
            Imagem imagem2 = new ImagemProxy("ResOi_10MB_Foto2");
    
            imagem1.mostrarImagem(); // é necessário o carregamento
            imagem1.mostrarImagem(); // não é necessário o carregamento
            imagem2.mostrarImagem(); // é necessário o carregamento
            imagem2.mostrarImagem(); // não é necessário o carregamento
            imagem2.mostrarImagem(); // não é necessário o carregamento
            imagem1.mostrarImagem(); // não é necessário o carregamento
        }
    }
    
        
    asked by anonymous 08.06.2015 / 16:26

    1 answer

    3

    @Felipe Jorge,

    Proxy, translated directly means proxy or representative. Most important is to understand the purpose of creating an Object Proxy is to create a representative of another Object.

      

    Why should I use this pattern? When should I use it? Because he is   good?

    You should apply good design practices for a variety of reasons, you should not use Design Patterns everywhere, just because you think you are cool, remember the purpose of a Design technique. In our case, we are talking about the Proxy, a representative, so when you need to use a Representative of an Object or the modeling of an object, you create a Proxy. An example? The first thing that sets me in mind is the proxy created to make the local call of a remote object in Java. Using RMI, and here I will not issue what it is, but it is the implementation of Java for RPC. The remote object that is on a different virtul machine, elsewhere on the network, can execute business logic. And you can make this call locally from your Java Virtual Machine , but for this you need a Proxy for that remote object, so you make the call on the local object , and underneath the wipes - if you allow me - whoever executes the truth method is the Remote Object, but the call you make on your Proxy

    See more about RMI here and here

      

    What would be a wrong way to do this pattern?

    Well, still using the example above, you would not need Proxy for a Local Object, at least not normally. But there may be several flaws when implementing such a Pattern.

      

    Are there any flaws in this pattern?

    Perhaps the biggest failures are to not know when to implement, or to stop using it when necessary. An example is that it can be very useful for serializing an object that you do not want to serialize. Then you create a proxy for your object and serialize this proxy. So your object does not need to implement Serializable . What I mean is that there are no talks but trade offs. These trade offs happen because as you choose one solution instead of another, you create some other kind of requirement. If you do not serialize your class, and use a proxy for this, you have a good understanding of what you are doing and create another class, and so, but maintenance, as I said, an exchange.

    (In this article more about serialization proxy pattern):

    link

      

    Anything else to add?

    Emphasize that it is important to know the purpose of designing a class or system using a standard, or a technique, for example, you know the purpose of DAO, I do not want to stimulate this topic here - DAO - just show how most programmers follow a copy and paste routine without understanding what the goals are.

        
    08.06.2015 / 18:11