vendredi 29 mai 2020

whether the codes use the Proxy pattern or singleton pattern?

I'm not sure the following code using the Proxy pattern or Singleton pattern?

public interface SomeObject {
    void process();
}

public class SomeObjectImpl implements SomeObject {

    public SomeObjectImpl() {
        ...
    }

    @Override
    public void process() {
        ...
    }
}

public class AnotherObject implements SomeObject {
    private SomeObject object;

    @Override
    public void process() {
        if (object == null) {
            object = new SomeObjectImpl();
        }
        object.process();
    }
}

The reasons for me to use the singleton pattern is: The singleton pattern was used in the codes cause it defines that a class must ensure that only a single instance should be created and a single object can be used by all other classes. In the case, when the parent interface object is null, we create an instance of Same-Object when it required.

As for the Proxy pattern, it seems that the someObject is the proxy. But I'm really confused that both the two patterns are correct or only the proxy pattern is correct? Thanks.

Aucun commentaire:

Enregistrer un commentaire