第一条:考虑使用静态方法替代构造器

The traditional way for a class to allow a client to obtain an instance is to provide a public consructor.There is another technique that should be a part of every programmer's toolkit.A class can provide a public static factory method,which is simply a static method that returns an instance of the class.Here's a simple example from Boolean(the boxed primitive class for boolean).This method translates a boolean primitive value into a Boolean object reference:

    public static Boolean valueOf(boolean b) {

        return b ? Boolean.TRUE : Boolean.False;

    }

Note that a static factory method is not the same as the Factory Method pattern from Design Patterns[Gramma95].The static factory method described in this item has no direct equivalent in Design Patterns.

对于一个类来说,允许获取一个对象实例的传统的方式就是提供一个公有的构造器。还有一个另外的方法应该出现在每一个程序员的工具包中。一个类可以提供一个简单的公有静态工厂方法,返回一个类的实例。Boolean类(基本类型boolean的包装类)中有个简单的例子,下面这个方法将一个boolean值转换成了Boolean对象的引用:

    public static Boolean valueOf(boolean b) {

        return b ? Boolean.TRUE : Boolean.False;

    }

注意这里的静态工厂方法并不是设计模式中的工厂模式。本条中描述的静态工厂方法在设计模式中并没有直接的等价物。

A class can provide its clients with static factory methods instead of,or in addition to,public constructors.Providing a static factory method instead of a public constructor has both advantages and disadvantages.

类除了给客户端提供公有构造方法外还可以提供静态工厂方法代替。提供静态工厂方法替代公有构造器有优点也有缺点。

One advantage of static factory method is that,unlike constructors,they have names.If the parameters to a constructor do not,in and of themselves,describe the object being returned,a static factory with a well-chosen name is easier to use the resulting client code easier to read.For example,the constructor BigInteger(int,int,Random),which returns a BigInteger that is probably prime,would have been better expressed as a static factory method named BigInteger.probablePrime.(This method was added in Java 4.)

静态工厂方法的第一个优点是,不像构造器那样,他们有名字。如果构造器的参数没有描述返回值对象,那么一个具有好的名字的静态工厂方法将会使代码更加易读。例如,构造方法BigInteger(int,int,Random),返回一个可能为质数的BigInteger,相比较而言,静态工厂方法BigInteger.probablePrime(Java 4中增加的方法)可能表达的更好一点。

A class can have only a single constructor with a given signature.Programmers have been known to get around this restriction by providing two constructors whose parameter lists differ only in the order of their parameter types.this is really bad idea.The user of such an API will never be able to remember which constuctor is which and will end up calling the wrong one by mistake.People reading code that uses these constructors will not know what the code does without referring to the class documentation.

一个类对于给定的签名只能提供一个构造器。程序员知道可以提供两个构造器来绕开这个限制,通过改变参数类型的顺序,这真的是个坏主意。API文档的使用者将不可能记住哪个构造器做什么的,有可能会发生错误的调用。读代码的人如果不看文档将不会知道构造器的作用。

Because they have names,static factory methods don't share the restriction discussed int the previous paragraph.In cases where a class seems to require multiple constructors with the same signature,replace the constructors with static factory methods and carefully chosen names to highlight their differences.

因为有名字,所以静态工厂方法不存在上段所讨论的限制。如果一个类中需要多个相同签名的构造函数,那么使用静态工厂方法替代他们,并认真的选择方法名来强调他们的不同。

A second advantage of static factory methods is that,unlike constructors,they are not required to create a new object each time they're invoked.This allows immutable classes to use preconstructed instances,or to cache instances as they're constructed,and dispense them repeatedly to avoid creating unnecessary duplicate objects.The Boolean.valueOf(boolean) method illustrates this technique: it never creates an object. This technique is similar to the Flyweight pattern. It can greatly improve performance if equivalent objects are requested often,especially if they are expensive to create.

静态工厂方法的第二个优点是,不像构造器那样,他们不需要在每次被调用的时候都创建一个新的对象。这允许了不可变类去使用之前构造的实例,或者缓存创建过的实例,然后反复的发放它们来避免创建多余的对象。Boolean.valueOf(boolean)方法表明了这个技巧,他从不会创建一个对象。这个技巧和设计模式中的享元模式相似。如果一个对象经常被请求的话,这种方式可以提高性能,特别是创建对象的代价很高时。

The ability of static factory methods to return the same object from repeated invocations allows classes to maintain strict control over what instances exist at any time.Classes that do this are said to be instance-controlled classes.There are several reasons to wirte instance-controlled classes.Instance contro allowsa a class to guarantee that it is a singleton or noninstantiable.Also,it allows an immutable value class to make the guarantee that no two equal instances exist: a.equals(b) if and only if a == b.This is the basis of the Flyweight pattern.Enum types provide this guarantee.

静态工厂方法从重复调用返回同一对象的能力允许类在任何时候保持对存在的实例的严格控制。可以做到这点的类被称为实例控制类。这里有几个要写实例控制类的原因。实例控制允许一个类去保证它的单例或者不可实例化。同样,它还允许不可变值类去保证没有两个相同实例的存在:当且仅当a==b时,a.equals(b)。这是享元模式的基础。枚举类型保证了这一点。

A third advantage of static factory methods is that,unlike constructors,they can return an object of any subtype of their return type.This gives you great flexibility in choosing the class of the returned object.

静态工厂方法的第三个好处是,他们可以返回一个返回值类型的任意一个子类型。这让你在选择返回对象时具有非常大的灵活性。

One application of this flexibility is that an API can return objects without making their classes public.Hiding implementation classes in this fashion leads to a very compact API.This technique lends itself to interface-based frameworks,where interfaces provide natural return types for static factory methods.

这种灵活性的一个应用就是API可以在不公开实现类信息的情况下返回对象。隐藏实现类的方式会使API非常的紧凑。这种技术衍生了很多基于接口的框架,接口为静态工厂方法提供原始返回类型。

Prior to Java 8,interfaces couldn't have static methods.By convention,static factory methods for an interface named Type were put in a noninstantiable companion class names Types.For example,The Java collections Framework has forty-five utility implementations of its interfaces,providing unmodifiable collections,synchronized collections,and the like.Nearly all of these implmentations are exported via static factory in one noninstantiable class(java.util.Collections).The classes of the returned objects are all nonpublic.

Java 8之前,接口不可以有静态方法。因此按照惯例,接口Type的静态工厂方法被放在一个名为Types的不可实例化的伴生类中。例如,Java Collections Framework 的集合接口有45个工具实现,分别提供了不了修改的集合、同步集合,等等。几乎所有这些实现都通过静态工厂方法在一个不可实例化的类(java.util.Collections)中导出。所有返回对象的类都是非公有的。

The collections Framework API is much smaller than it would have been had it exported forty-five separate public classes,one for each convenience implementation.It is not just the bulk of the API that is reduced but the conceptual weight: the number and  difficulty of the concepts that programmers must master in order to use the API.The programmer knows that the returned objects has precisely the API specified by its interface,so there is no need to read additional class documentation for the implementation class.Furthermore,using such a static factory method requires the client to refer to the returned object by interface rather than implementation class, which is generally good practice.

As of Java 8, the restriction that interfaces cannot contain static methods was eliminated, so there is typically little reason to provide a noninstantiable companion class for an interface. Many public static members that would have been at home in such a class should instead be put in the interface itself. Note,however, that it may still be necessary to put the bulk of the implementation code behind these static methods in a separate package-private class. This is because Java 8 requires all static members of an interface to be public.Java 9 allows private static methods,but static fields and static member classes are still required to be public.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
卫星云图全面、及时、动态的反映各类云系的特点及变化过程,成为气象、 水利部门在防洪抗旱决策过程中不可或缺的重要参考依据。多年的云图接收积累 了数量巨大的卫星云图,依靠人工判读方式根本无法满足时效性要求,尽管一些 人工智能的方法能自动完成数据分析,但这类方法只能按照设定的规则执行,不 能主动发现隐含在数据内部的知识。图像挖掘技术作为数据挖掘领域研究的前沿, 提供了从大量图像中获取隐含的、有价值的、可理解知识的理论及方法。本文以 图像挖掘技术理论与方法为指导,设计了面向云图集和云图—雨量混合数据集的 三类挖掘任务,所获得知识将对云图智能化理解和基于卫星云图的降水预测等研 究具有重要价值。论文的研究工作及贡献包括以下方面: (1)在卫星云图预处理方法研究中,提出了新的非线性自适应噪声滤除算法。 该算法与常用的中值滤波技术相比,它能有效的消除椒盐噪声,保护云图中非噪 声点不受影响,确保像素信息能够真实反映云内状态。云图中存在经纬线、地名 等标注对象,它们会影响云图特征参数的提取。针对标注对象的形状特点,提出 一种基于整体变分技术的标注对象剔除算法,通过引入权值改进了整体变分的离 散化过程。结果表明算法有效剔除标注对象的同时保护了邻域信息。 (2)在云图感兴趣区域提取研究中,提出了基于云图直方图的加权聚类算法, 利用该算法实现典型云区的提取。为了更加符合云图数据样本在特征空间的分布 特点,重点研究了对聚类算法的改进策略:1)针对类别个数自适应确定方法的改 进,提出利用遗传算法结合评价指标曲线找出最优类别数,提高算法自动化水平。 2)针对相似性测度的改进,提出基于链式距离的相似性测度,克服了欧式距离测 度对数据分布的敏感性问题。3)针对聚类机制的改进,引入半监督思想,既能克 服单纯聚类的盲目性问题,又能避免分类面临的训练样本问题。以直方图替代云 图像素作为聚类对象,大幅减少了算法处理时间。 (3)在云类智能识别的研究中,本文针对特征提取、特征选择、分类模型三 个问题提出了对应的算法及模型。针对云区的无规则特性,提出了“基圆模型描 述法”用于云区的描述,在此基础上提取云的形态特征参数,克服了以往算法只 能提取颜色、纹理等特征的不足。为避免过拟合问题,本文采用特征曲线分析方 法,从特征候选集中确定分类模型的输入特征集。本文提出将“IPSO—BP网络模 型”作为分类模型。该模型采用改进的粒子群优化算法替代后向学习算法作为BP 神经网络模型的学习算法,在一定程度上克服了收敛速度慢,易陷入局部极小值, 过分依赖初始值的选择等不足。为了在原有分类模型框架下更好的利用多特征信 息,本文提出了基于多特征融合的组合分类模型,将特征子集分别送入子分类模国防科学技术大学研究生院博士学位论文 型后作出本地决策,采用投票表决法将多个本地决策融合后获得最终的结果。结 果表明多特征融合分类模型在分类精度上优于单一分类模型。 (4)在基于云图—雨量混合数据集的关联规则挖掘研究中,本文以云图灰度 和云顶亮温间的关系为基础,设计了四种与降雨关系密切的云状态参数。通过时 空同步处理,云图参数和雨量数据构成统一的混合数据集。为实现数值属性的转 换,本文提出一种基于聚类的数值属性分区方法,它克服了“等深度区间划分法” 对数据倾斜敏感的问题。为了提高对大规模云图—雨量混合数据集的处理效率, 本文提出了基于数据分割的两阶段关联规则挖掘算法,它通过将原始数据库划分 为多个独立的区间,由每个子区间的局部频繁项集产生全局候选项集,并设计了 专门用于支持度计算的数据结构tidlists,这些策略有效的减少了算法对数据库的 扫描次数,大幅提高了算法的效率。结果表明当支持度阈值处于较低水平时,本 文算法的执行效率显著优于Apriori算法的执行效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值