这段代码使用了代理模式、单例模式,还是两者都使用了?

huangapple go评论115阅读模式
英文:

Does this code use the Proxy or Singleton pattern, or both of them?

问题

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();
    }
}

I think it's the Singleton pattern because it states 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 example, when SomeObject is null, an instance is created as required.

However, it also looks like the Proxy pattern is used because SomeObject is the proxy.

Is this code really using both patterns? Or is the Proxy pattern used only?

英文:
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();
    }
}

I think it's the Singleton pattern because it states 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 example, when SomeObject is null an instance is created as required.

However, it also looks like the Proxy pattern is used, because SomeObject is the proxy.

Is this code really using both patterns? Or is the Proxy pattern used only?

答案1

得分: 1

单例模式 的目的是确保单例类的实例只存在一个。

给定的代码是否确保了这一点?不是,因为没有任何阻止你从两个类 SomeObjectImplAnotherObject(只需调用它们的默认构造函数)创建任意多个实例。

提示: 参考 Wikipedia 上的单例模式页面 获取不同语言的实现示例... 一旦你了解了如何创建单例,那么最好永远不要在真实代码中使用它们,因为这是创建 难以测试的软件 ⚡ 并且拥有将所有内容粘合在一起的 全局变量 ⚡ 的最佳方式。


代理模式 的目的是控制对对象的访问或在访问对象时提供一些附加功能。

给定的代码是否看起来像这样?是的

两个类 SomeObjectImplAnotherObject 实现了接口 SomeObject。在代理模式的术语中,该接口代表了提供某些操作的 Subject,即 process 方法。然后,SomeObjectImpl 类是实际的实现;代理模式称之为 RealSubject。而 AnotherObject 类只是简单地委托给实际实现 SomeObjectImpl,也就是一个 代理

鉴于该代码,代理 的目的(或附加功能)是缓存 SomeObjectImpl 的一个实例,以便于后续的 process 调用。当对象的创建成本很高时,这是有意义的。例如,当 process 方法的实现需要设置一些外部资源或需要进行“大”的预分配内存时,...

英文:

The Singleton Pattern's purpose is to ensure that only one instance of the singleton class ever exists.

Does the given code ensure that? No, because nothing prevents you from creating as many instances as you like from the two classes SomeObjectImpl and AnotherObject (by just invoking their default constructor).

Hint: See the Wikipedia page on Singleton Pattern for implementation examples in different languages... and once you've seen how to create singletons – then forget about using them in real code, ever – as it's the best way for creating untestable software ⚡ and having global variable(s) ⚡ gluing everything together.


The Proxy Pattern's purpose is to control the access to an object or to provide some additional functionality upon accessing an object.

Does the given code look something like that? Yes.

The two classes SomeObjectImpl and AnotherObject implement the interface SomeObject. That interface (in terms of the Proxy Pattern) represents the Subject providing some action, i.e. the process method. The SomeObjectImpl class is then the acutal implementation; the RealSubject (as named by the Proxy Pattern). While AnotherObject class simply delegates to the actual implemenation SomeObjectImpl, i.e. it's a Proxy.

Given that code, the Proxy's purpose (or additional functionality) is to cache an instance of SomeObjectImpl for subsequent process calls. This makes sense when the object creation is a costly operation. For example, when the process method implementation requires the setup of some external resources or requires "big" upfront memory allocations, ...

huangapple
  • 本文由 发表于 2020年5月29日 19:44:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/62085210.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定