Proxy project default, why use?

Asked

Viewed 1,666 times

4

This proxy pattern example can be found on Wikipedia.

reference: http://en.wikipedia.org/wiki/Proxy_pattern

What I want to know is this:

  1. Why should I use this pattern? When should I use it? Why is it good?
  2. How would be a wrong way to make this pattern?
  3. There are flaws in this pattern?
  4. Something 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
    }
}

1 answer

3


@Felipe Jorge,

Proxy, translated directly means attorney or representative. The most important thing is to understand the purpose to create a Proxy Object is to create a representative of another Object.

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

You should apply good design practices for various reasons, you should not use Design Patterns everywhere, just because you think it’s 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 an Object Representative or the modeling of an object, you create a Proxy. An example? The first thing that comes to mind is the proxy created to make the local call of a remote object, in Java. Using RMI, and here I will not explain what it is, but it is the implementation of Java for RPC. The remote object that is in a different virtual machine, elsewhere in the network, can perform business logic. And you can make this call locally, from your Java Virtual Machine, but for that you need a Proxy to that remote object, so you make the call on the local object, and behind the scenes - if you allow me - who executes the true method is the Remote Object, but the call you make in yours Proxy

See more about RMI here and here

How would be a wrong way to make this pattern?

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

There are flaws in this pattern?

Perhaps the biggest flaws are not knowing when to implement, or stop using it when necessary. An example is that it can be very useful for serialization of 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 by that is that there are no flaws, but exchanges - Trade offs - These trade offs happen, because as you choose one solution over another, you create some other kind of requirement. If you don’t serialize your class, and you use a proxy for that, you have to really understand what you’re doing and create one more class, and so on, but maintenance, like I said, an exchange.

(More about serialization proxy Pattern):

http://java.dzone.com/articles/serialization-proxy-pattern

Something else to add?

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.