忍者ブログ

いけいけ理系NEO

情報処理試験など、理系の試験対策、関連知識、日記などです

Javaを学ぶ その3

題材は、「Effective Java 2nd Edition」です。

Item 1: Consider static factory methods instead of constructors

The normal way for a class to allow a client to obtain an instance of itself is to provide
a public constructor. 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.


日本語でのポイント

constructorのかわりに、static factory methodを利用する。

クライアントがあるクラスのインスタンスを取得する、一般的な方法は、public なconstructorを利用することである。
しかし、プログラマが知るべきテクニックは、他にもある。
クラスは、public static factory methodを提供することができる。これは、クラスのインスタンスを返すものである。

解説

static な methodは、スピードとかメモリ等のメリットがあるような気がします。
たしかに、知っておくべきテクニックかと思います。



PR