Object oriented design - when to use getClass and instanceof

During the lecture, a university lecturer said that use getClassand instanceofindicates poor design.

What are some use cases that are bad design? What problems can be caused by using these methods? Are there any valid ways to use these methods that are not bad?

+4
source share
2 answers

Poor use

, . , , , instanceof, , , . - ( ).

private static class A {
    private void printA() {
        System.out.println("A");
    }
}

private static class B {
    private void printB() {
        System.out.println("B");
    }
}

public static void main(String[] args) {
    List<Object> list = asList(new A(), new B(), new A());

    list.forEach(element -> { // this is bad, don't do it!
        if (element instanceof A) {
            ((A) element).printA();
        }
        if (element instanceof B) {
            ((B) element).printB();
        }
    });
}

:

private interface Printer {
    void print();
}

private static class A implements Printer {
    @Override
    public void print() {
        System.out.println("A");
    }
}

private static class B implements Printer  {
    @Override
    public void print() {
        System.out.println("B");
    }
}

public static void main(String[] args) {
    List<Printer> list = asList(new A(), new B(), new A());

    list.forEach(Printer::print);
}

, equals. , . , , . equals Object. , , , , , false, ClassCastException.

, IntelliJ:

public class Person {
    private String name;
    private String surname;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Person person = (Person) o;

        if (!name.equals(person.name)) return false;
        return surname.equals(person.surname);
    }
}

API

, , , POJO-json-mappers, API .

EDIT:

, , , , :

public static abstract class Animal {

    protected final String name;

    public Animal(String name) {
        this.name = name;
    }

    public void run() {
        System.out.println(name + " runs");
    }

    public abstract void move();
}

public static class Dog extends Animal {
    public Dog() {
        super("Dog");
    }

    @Override
    public void move() {
        run();
    }
}

public static class Eagle extends Animal {

    public Eagle() {
        super("Eagle");
    }

    public void fly() {
        System.out.println(name + " flies");
    }

    @Override
    public void move() {
        fly();
    }
}

public static void main(String[] args) {
    List<Animal> animals = Arrays.asList(new Dog(), new Eagle());

    animals.forEach(Animal::move);

    System.out.println("Eagle can run too!");
    new Eagle().run();
}

:

Dog runs
Eagle flies
Eagle can run too!
Eagle runs

, . , , run() Animal. , , , , , , , move() Animal.

+6

, . , , , , , .

, Jaroslaw Pawlak , , (, , , ) instanceof .

, , .

Swing GUI, , JPanel, . / , . , , (, ).

+1

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


All Articles