Created
December 2, 2018 12:26
-
-
Save kazuya-k-ishibashi/ce14da435639a368f8f60185daa8d0b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kishibashi.desigh.pattern.factory; | |
/** | |
* コンテンツの文字列長を表すクラスです。 | |
* | |
* オブジェクト指向の理念に基づき、単一の概念を表す一つのクラスを定義します。 | |
* | |
* 文字列長自体を引数にインスタンスを生成するパターンと、 | |
* 指定された文字列の文字列長を示すインスタンスを生成するパターンがほしいのです。 | |
* | |
*/ | |
public class ContentLength { | |
private final int value; | |
private ContentLength(final int value) { | |
this.value = value; | |
} | |
/** | |
* 指定された文字列長を示すContentLengthオブジェクトを生成します。 | |
* | |
* @param length | |
*/ | |
public static ContentLength valueOf(final int length) { | |
return new ContentLength(length); | |
} | |
/** | |
* 指定された文字列長を示すContentLengthオブジェクトを生成します。 | |
* 引数がString版。 | |
* | |
* @param length | |
*/ | |
public static ContentLength valueOf(final String length) { | |
return new ContentLength(Integer.parseInt(length)); | |
} | |
/** | |
* 指定された文字列の文字列長を示すContentLengthオブジェクトを生成します。 | |
* | |
* @param length | |
*/ | |
public static ContentLength contentOf(final String content) { | |
return new ContentLength(content.length()); | |
} | |
/** | |
* int型で取り出します。 | |
* | |
* @return | |
*/ | |
public int intValue() { | |
return value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
// 文字列長の数値で生成 | |
ContentLength len1 = ContentLength.valueOf(5); | |
// 文字列長の文字列で生成 | |
ContentLength len2 = ContentLength.valueOf("5"); | |
// 文字列から生成 | |
ContentLength len3 = ContentLength.contentOf("Hello"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment