Determine Which Enum Return Is Based on an Object Property

I am wondering if there is any design template that will help me with this problem.

Let's say I have a class Personthat has three attributes name, nicknameand speaksEnglishand Enum PersonTypewith TypeOne, TypeTwoand TypeThree.

Say, if a Personhas nicknameand speaksEnglish, then a TypeOne. If he has nickame, but not speaksEnglish, he a TypeTwo. If he does not have nickame, therefore he is TypeThree.

My first thought would be to have a method with some if-elseand returned related Enum. In the future, I may have more attributes in Personand other types PersonType.

So, my first thought was to create a method with a bundle if (...) { return <PersonType> }or switch-case, but I was wondering if there was any design pattern that I could use instead of ifsand switch-case.

+4
source share
3 answers

I recommend that you use simple inheritance with immutable objects.

So first you need to create an abstract class:

public abstract class AbstractPerson {

    private final String name;
    private final Optional<String> nickname; 
    private final boolean speaksEnglish;
    private final PersonType personType;

    protected AbstractPerson(final String name, final Optional<String> nickname, final boolean speaksEnglish, final PersonType personType) {
        this.name = name;
        this.nickname = nickname;
        this.speaksEnglish = speaksEnglish;
        this.personType = personType;
    }

    public String getName() {
        return name;
    }

    public Optional<String> getNickname() {
        return nickname;
    }

    public boolean getSpeaksEnglish() {
        return speaksEnglish;
    }

    public PersonType getPersonType() {
        return personType;
    }

}

With PersonTypelisting:

public enum PersonType {

    TypeOne, TypeTwo, TypeThree;

}

Now we have three options with the corresponding constructors in the child classes:

public final class EnglishSpeakingPerson extends AbstractPerson {

    public EnglishSpeakingPerson(final String name, final String nickname) {
        super(name, Optional.of(nickname), true, PersonType.TypeOne);
    }

}

public final class Person extends AbstractPerson {

    public Person(final String name, final String nickname) {
        super(name, Optional.of(nickname), false, PersonType.TypeTwo);
    }

    public Person(final String name) {
        super(name, Optional.empty(), false, PersonType.TypeThree);
    }

}

. if-else - , /.

0

, Type Person. @ByeBye , Person, .

X , , . , Manager Developer , , Employee. , , , -else SOLID.

Person .

public abstract class Person {
    public Person(string name) {
        Name = name;
    }
    public abstract string Name { get; set; }
    public abstract string NickName { get; set; }
    public abstract bool SpeaksEnglish { get; set; }
}

public class TypeOnePerson : Person {
    public TypeOnePerson(string name, string nickName) : base(name) {
        NickName = nickName; // Validate empty/ null 
    }
    SpeaksEnglish = true;
}

public class TypeTwoPerson : Person {
    public TypeOnePerson(string name, string nickName) : base(name) {
        NickName = nickName; // Validate empty/ null 
    }
    SpeaksEnglish = false;
}

, , . , , , #. .

0

OO, ? , , , ( ), : "... PersonType .".

Decorator, . Component Optional ( , NickName ) .

Person Concrete Decorator . Decorator Pattern . GOF ( Erich gamma) - " . ". [ .]

0
source

Source: https://habr.com/ru/post/1684078/


All Articles