简体   繁体   English

代理模式具体类隐藏实现细节

[英]Proxy pattern concrete class hiding implementation details

I have been reading about java proxy pattern and invocation handler and everywhere it is seen that the concrete class construction is available to the client. 我一直在阅读有关Java代理模式和调用处理程序的信息,到处都可以看到具体的类构造可用于客户端。

For example, 例如,

//TwitterService service = (TwitterService) SecurityProxy.newInstance(new TwitterStub());
TwitterService service = new TwitterStub();
System.out.println(service.getTimelinePosts("abc"));

User can create an instance of TwitterStub directly and access the methods. 用户可以直接创建TwitterStub的实例并访问方法。 I was wondering if there was a way of not exposing the or avoiding the construction of the concrete class via the client ? 我想知道是否存在不通过客户端公开或避免构建具体类的方法?

Is there a better way ? 有没有更好的办法 ?

As the GoF puts it, the intent of the proxy pattern is "to provide a surrogate or placeholder for another object to control access to it". 正如GoF所说,代理模式的目的是“为另一个对象提供代理或占位符,以控制对其的访问”。

So, in your specific case what happens is that you're creating a particular proxy instance directly. 因此,在您的特定情况下,发生的事情是直接创建特定的代理实例。 And that's fine as long as you (as a client) know that you want a particular type of proxy object. 只要您(作为客户端)知道您想要特定类型的代理对象,就可以了。

If what you want is a level of indirection, you can use an Abstract Factory Pattern that return different types of proxy objects. 如果想要的是间接级别,则可以使用抽象工厂模式来返回不同类型的代理对象。 But so far, the reason of proxy objects is to act on behalf of other objects. 但是到目前为止,代理对象的原因是代表其他对象进行操作。

As a side note, proxies are useful when you have objects that are expensive to create, but you don't want to necessarily cripple main application function due to such expense. 附带说明一下,当您创建的对象很昂贵时,代理很有用,但是由于此类开销,您不想削弱主要的应用程序功能。 For example, suppose you have a document with 1000 images. 例如,假设您有一个包含1000张图像的文档。 You can use proxies for them, and only load them when stricly needed (ie when they are in visible view), and you can still load the full document really fast without the overhead of reading 1000 images at once. 您可以为它们使用代理,并且仅在非常需要时(即,当它们在可见视图中时)加载它们,并且仍然可以非常快速地加载完整文档,而无需一次读取1000张图像的开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果在代理模式中我们有接口而不是实际的具体主题在Proxy类中它等同于Decorator模式 - If In Proxy Pattern we have interface instead of actual concrete Subject in Proxy class is it equivalent to Decorator Pattern 接口>抽象类>具体类模式 - Interface > Abstract Class > Concrete Class pattern Java动态代理-如何参考具体的类 - Java Dynamic Proxy - How reference concrete class Spring注入具体类而不是代理 - Spring is injecting concrete class instead of proxy 在具体实现类中使用泛型类型自动完成 - Autocomplete with generic types in concrete class of implementation Spring JDK动态代理和CGLIB - 实现细节 - Spring JDK Dynamic Proxy and CGLIB - implementation details 抽象如何帮助隐藏 Java 中的实现细节? - How does abstraction help in hiding the implementation details in Java? 是与代理设计模式相关的代理类 - is Proxy class related to proxy design pattern 抽象类持有成员字段还是使用具体实现的Getter()? - Abstract Class Hold Member Field or Use Getter() of Concrete Implementation? 如何指定抽象类的具体实现的哪个版本在Spring中自动装配? - How to specify which version of a concrete implementation for an abstract class to autowire in Spring?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM