Class.newInstance()是否遵循“抽象工厂”。设计模式? [英] Does Class.newInstance() follow the "Abstract factory" design pattern?

查看:145
本文介绍了Class.newInstance()是否遵循“抽象工厂”。设计模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始阅读Joshua Bloch的Effective Java(第二版)。在阅读第2项(在面对许多构造函数参数时考虑构建器)时,有一个特定的声明,即作者使用 Class.newInstance()方法。具体来说,作者说

I have started reading Joshua Bloch's "Effective Java" (second edition). While reading item 2 (Consider a builder when faced with many constructor parameters), there is a particular statement that the author makes w.r.t the Class.newInstance() method. Specifically, the author says that


Java中的传统抽象工厂实现一直是类对象,使用newInstance方法播放构建方法的一部分。

The traditional Abstract Factory implementation in Java has been the "Class" object, with the "newInstance" method playing the part of the "build" method.

这部分让我感到困惑 - 我对抽象工厂设计模式的理解是它用来代表工厂的工厂。在我看来, Class.newInstance()方法更多地依赖于静态工厂方法编码理念(顺便说一句,这是同一本书中的第1项)

This part has me confused a little bit - my understanding of the Abstract factory design pattern is that it is used to represent a factory of factories. The Class.newInstance() method, in my opinion, borders more on the "static factory method" coding philosophy (which incidentally, is item 1 in the same book)

想法,有人吗?我一直在努力克服一些艰难的采访,如果我的基本面在出现进行此类采访之前是稳固的,我会非常感激。

Thoughts, anyone? I have been preparing hard to crack a few tough interviews and would really appreciate it if my fundamentals were solid before appearing for such interviews.

谢谢。

推荐答案

这是我的看法。

首先,抽象工厂模式并不打算是一家工厂。这种模式的关键方面是存在一个可访问的接口,该接口具有底层(可能无法访问)的工厂实现,通过该实现,您可以获得(可能无法访问)对象实现的可访问接口。我知道,在Gamma的书中我是如何理解这种模式的一些适用性条件的长篇讨厌的文字游戏:

First of all, the Abstract Factory pattern is not intended to be a factory of factories. The key aspect of this pattern is that there is an accessible interface with an underlying (probably inaccessible) factory implementation through which you can get accessible interfaces of (probably inaccessible) object implementations. I know, is a long nasty wordplay of how I understand some of the applicability conditions of this pattern in Gamma's book:



  • 系统应独立于其产品的创建,组合和表示方式

  • 您想要提供产品类库,并且您希望仅显示其接口,而不是他们的实现。

最后你得到的是对象,而不是工厂。

At the end you're getting objects, not factories.

其次,我不会在模式概念和语言关键字之间建立1:1的关系。 Abstract Factory不一定总是转换为Java 抽象类接口结构。只要您以某种方式保证客户端代码独立于底层工厂和对象实现,您仍然可以拥有一个表示抽象工厂的常规,可扩展,可实例化的类。这是 java.lang.Class 的情况,它不是抽象的也不是接口,而是通过<隐藏它所代表的类型的无参数构造函数实现的工作。 code> newInstance()方法。如果您使用它可能更清楚:

Second, I wouldn't make 1:1 relationships between pattern concepts and language keywords. "Abstract Factory" does not necessarily always translate to the Java abstract class or interface constructs. You can still have a regular, extendable, instantiable class that represents an "Abstract Factory" as long as you somehow guarantee that the client code is independent of the underlying factory and object implementations. This is the case of java.lang.Class, which is not abstract nor an interface, but does the job at hiding the parameterless constructor implementation of the type it represents through the newInstance() method. It's probably clearer if you use it like:

Object o = Class.forName(type).newInstance();

Class 播放抽象工厂,和对象播放类型实现的抽象产品。

Class plays the "Abstract Factory", and Object plays the "Abstract Product" to the type implementation.

最后, newInstance()不是静态工厂方法,我认为因为此模式旨在返回实例它实现的类。 newInstance()不返回 Class 的实例,也不返回 Class ES。它返回它所代表的类型的实例。正如布洛赫在他的书中所述,它既不是工厂方法。

Last, newInstance() is not a static factory method, I think because this pattern is intended to return instances of the class it is implemented on. newInstance() does not return instances of Class nor sub-Classes. It returns instances of the type it represents. It neither is a "Factory Method" just as Bloch states in his book.

这篇关于Class.newInstance()是否遵循“抽象工厂”。设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆