Upgrade to Pro — share decks privately, control downloads, hide ads and more …

値オブジェクトの悩みポイントとどう付き合っていくか

yachiy
July 30, 2019

 値オブジェクトの悩みポイントとどう付き合っていくか

yachiy

July 30, 2019
Tweet

More Decks by yachiy

Other Decks in Programming

Transcript

  1. public class PersonName { private String firstName; private String lastName;

    public String fullName() { return String.format("%s %s", firstName, lastName); } } ͜ͷ͘Β͍ͳΒಛʹ೰·ͳ͍ ྫͷࢀরݩɿ
  2. public class PersonName { private String firstName; private String lastName;

    public String fullName() { return String.format("%s %s", firstName, lastName); } } ݸผͷৼΔ෣͍͸ͳͦ͞͏ͳͷͰಛʹ೰·ͳ͍
  3. public class PersonName { private String firstName; private String lastName;

    private String middleName; public String fullName() { return String.format("%s %s %s", firstName, middleName, lastName); } }
  4. public class PersonName { private String firstName; private String lastName;

    private String middleName; public String fullName() { return String.format("%s %s %s", firstName, middleName, lastName); } } ໊ࣈɺ໊લͱ͸ໟ৭͕ҧ͏ ˠݸผͷৼΔ෣͍͕͋Γͦ͏ ˠཁૉͷ஋ΦϒδΣΫτԽΛݕ౼
  5. public class MiddleName { private static final int MIN_LENGTH =

    1; private static final int MAX_LENGTH = 100; // null͕ೖΒͳ͍͜ͱ͸ݕূ͞Ε͍ͯΔ૝ఆ → ࢦఆ͕ͳ͍͜ͱ͸ۭจࣈͰදݱ private String value; // ίϯετϥΫλ΍ੜ੒ϝιουͰݕূ͢Δ͔ར༻ଆͰݕূ͔ͯ͠Β౉͔͢͸·ͨผͷ೰ΈϙΠϯτ… ... // ඞཁʹԠͯ͡༻ҙ public boolean isSpecified() { // value͕ۭจࣈͰͷੜ੒͸ڐͯ͠΋nullͰͷੜ੒͸ڐ͞ͳ͍ඞཁ͋Γ return !value.isEmpty(); } @Override public String toString() { return value; } ... }
  6. public class MiddleName { private static final int MIN_LENGTH =

    1; private static final int MAX_LENGTH = 100; // null͕ೖΒͳ͍͜ͱ͸ݕূ͞Ε͍ͯΔ૝ఆ → ࢦఆ͕ͳ͍͜ͱ͸ۭจࣈͰදݱ private String value; // ίϯετϥΫλ΍ੜ੒ϝιουͰݕূ͢Δ͔ར༻ଆͰݕূ͔ͯ͠Β౉͔͢͸·ͨผͷ೰ΈϙΠϯτ… ... // ඞཁʹԠͯ͡༻ҙ public boolean isSpecified() { // value͕ۭจࣈͰͷੜ੒͸ڐͯ͠΋nullͰͷੜ੒͸ڐ͞ͳ͍ඞཁ͋Γ return !value.isEmpty(); } @Override public String toString() { return value; } ... } ໊લ΍໊ࣈʹ΋ಉ༷ͷੑ࣭͸͋Γͦ͏ ˠ4USJOH͔Β஋ΦϒδΣΫτԽΛݕ౼ͯ͠΋ྑͦ͞͏ ஋ͷൣғΛ੍ݶͯ͠ ϓϩάϥϜΛΘ͔Γ ΍҆͘͢શʹ
  7. public class MiddleName { private static final int MIN_LENGTH =

    1; private static final int MAX_LENGTH = 100; // null͕ೖΒͳ͍͜ͱ͸ݕূ͞Ε͍ͯΔ૝ఆ → ࢦఆ͕ͳ͍͜ͱ͸ۭจࣈͰදݱ private String value; // ίϯετϥΫλ΍ੜ੒ϝιουͰݕূ͢Δ͔ར༻ଆͰݕূ͔ͯ͠Β౉͔͢͸·ͨผͷ೰ΈϙΠϯτ… ... // ඞཁʹԠͯ͡༻ҙ public boolean isSpecified() { // value͕ۭจࣈͰͷੜ੒͸ڐͯ͠΋nullͰͷੜ੒͸ڐ͞ͳ͍ඞཁ͋Γ return !value.isEmpty(); } @Override public String toString() { return value; } ... } όϦσʔγϣϯΛͲ͜ʹஔ͔͘͸·ͨผͷ೰ΈϙΠϯτ
  8. public class MiddleName { private static final int MIN_LENGTH =

    1; private static final int MAX_LENGTH = 100; // null͕ೖΒͳ͍͜ͱ͸ݕূ͞Ε͍ͯΔ૝ఆ → ࢦఆ͕ͳ͍͜ͱ͸ۭจࣈͰදݱ private String value; // ίϯετϥΫλ΍ੜ੒ϝιουͰݕূ͢Δ͔ར༻ଆͰݕূ͔ͯ͠Β౉͔͢͸·ͨผͷ೰ΈϙΠϯτ… ... // ඞཁʹԠͯ͡༻ҙ public boolean isSpecified() { // value͕ۭจࣈͰͷੜ੒͸ڐͯ͠΋nullͰͷੜ੒͸ڐ͞ͳ͍ඞཁ͋Γ return !value.isEmpty(); } @Override public String toString() { return value; } ... } Ͳ͏ͯ͠΋ར༻ଆͰ஋ͷ͋Δͳ͠Λؾʹ͢ΔͳΒ࣮૷
  9. public class PersonName { private FirstName firstName; private LastName lastName;

    private MiddleName middleName; ... public String fullName() { return String.format("%s %s %s", firstName, middleName, lastName); } }
  10. public class PersonName { private FirstName firstName; private LastName lastName;

    private MiddleName middleName; ... public String fullName() { return String.format("%s %s %s", firstName, middleName, lastName); } } ͜͜·Ͱຖճࡉ͔͘஋ΦϒδΣΫτԽ͢Δͷ͔
  11. public class PersonName { private String firstName; private String lastName;

    private String middleName; public String fullName() { return String.format("%s %s %s", firstName, middleName, lastName); } } public class PersonName { private FirstName firstName; private LastName lastName; private MiddleName middleName; public String fullName() { return String.format("%s %s %s", firstName, middleName, lastName); } }
  12. public class PersonName { private String firstName; private String lastName;

    private String middleName; public String fullName() { return String.format("%s %s %s", firstName, middleName, lastName); } } public class PersonName { private FirstName firstName; private LastName lastName; private MiddleName middleName; public String fullName() { return String.format("%s %s %s", firstName, middleName, lastName); } } public class PersonName { private static final int MIN_FIRST_NAME_LANGTH = 1; private static final int MAX_FIRST_NAME_LENGTH = 100; private static final int MIN_MIDDLE_NAME_LENGTH = 1; private static final int MAX_MIDDLE_MAME_LENGTH = 100; private static final int MIN_LAST_NAME_LENGTH = 1; private static final int MAX_LAST_NAME_LENGTH = 100; private String firstName; private String lastName; private String middleName; public String fullName() { return String.format("%s %s %s", firstName, middleName, lastName); } } ্Ґ֓೦͕ߏ੒ཁૉͷ ஋ͷൣғΛ୲อ
  13. public class PersonName { private static final int MIN_FIRST_NAME_LANGTH =

    1; private static final int MAX_FIRST_NAME_LENGTH = 100; private static final int MIN_MIDDLE_NAME_LENGTH = 1; private static final int MAX_MIDDLE_MAME_LENGTH = 100; private static final int MIN_LAST_NAME_LENGTH = 1; private static final int MAX_LAST_NAME_LENGTH = 100; private String firstName; private String lastName; private String middleName; public String fullName() { return String.format("%s %s %s", firstName, middleName, lastName); } } public class PersonName { private String firstName; private String lastName; private String middleName; public String fullName() { return String.format("%s %s %s", firstName, middleName, lastName); } } public class PersonName { private FirstName firstName; private LastName lastName; private MiddleName middleName; public String fullName() { return String.format("%s %s %s", firstName, middleName, lastName); } } public class PersonName { private FirstName firstName; private LastName lastName; private MiddleName middleName; public String fullName() { return String.format("%s %s %s", firstName, middleName, lastName); } } ্Ґ֓೦͕ߏ੒ཁૉͷ ஋ͷൣғΛ୲อ ಛʹมΘΓͳ͠
  14. public class PersonName { private static final int MIN_FIRST_NAME_LANGTH =

    1; private static final int MAX_FIRST_NAME_LENGTH = 100; private static final int MIN_LAST_NAME_LANGTH = 1; private static final int MAX_LAST_NAME_LENGTH = 100; private String firstName; private String lastName; private MiddleName middleName; public String fullName() { return String.format("%s %s %s", firstName, middleName, lastName); } } ϛυϧωʔϜ͚͕ͩಛघͱ͍͏ҙਤ͸఻ΘΓͦ͏ ཧղͷෛՙ͸ߴͦ͏