I am at a lookout for a design pattern for my simple problem. Here is a simplified version.
class Animal{...}
class Dog extends Animal{...}
class Cat extends Animal{...}
... // so on, 3 other classes as of now
I have a static method (in reality exposed via web-service but its synonymous) which takes an id and returns an animal.
If a cat is returned then the other team using the cat object generates a CatReport. If Dog, then dog report (They can use it for anything). Obviously Cat & Dog have different attributes. Cat and Dog don't have anything else in common apart from the fact that they are Animals. So making a call like below, is insufficient because I need the precise type:
public static Animal getAnimal(int id){}
Not sufficient because animal does not contain all the information what the precise type can give me.
What is the best way to deal with this problem?
PS: In Scala, I would simply do pattern-matching on the object. This solves the problem elegantly.
One solution I have is: make a call which returns an enum signifying what the id corresponds to. And then have a separate call for each:
public static AnimalType getAnimalType(int id){}
public static Cat getCat(int id){}
public static Dog getDog(int id){}
....
But this is cumbersome.
In a language like Java, you can simulate pattern matching behavior using the Visitor pattern.
You can do it in some steps :
Define an interface Animal representing an Animal with a accept method
Add some subclasses to Animal and give the same implementation like in my little example below.
Define an interface Visitor and give it an implementation. This classe will allows to you to simulate some pattern matching on your classes.
Here a little example :
public interface Animal {
public void accept(AnimalVisitor v);
}
public class Dog extends Animal {
public void accept(AnimalVisitor v) {
v.visit(this);
}
}
public class Cat extends Animal {
public void accdept(AnimalVistior v) {
v.visit(this);
}
}
public interface AnimalVisitor {
public void visit(Dog d);
public void visit(Cat c);
}
public class PrintAnimal implements AnimalVisitor {
public void visit(Dog d) {
System.out.println("Dog");
}
public void visit(Cat c) {
System.out.println("Cat");
}
}
Visitor pattern is a elegant way to solve your problem and also it's avoid the accumulation of if (x instance of bar) in one function. With this pattern, your code will be more readable and easier to extend.
The corresponding Scala code to make an idea of my answer:
abstract class Animal {}
case class Dog() extends Animal
case class Cat() extends Animal
object Animal {
def printAnimal(a : Animal) = a match {
case x : Dog => "Dog"
case x : Cat => "Cat"
case _ => "Unknown"
}
def main(args : Array[String]) = {
println(printAnimal(Dog()))
}
}
Well, I don't see any really elegant solution but you can create a kind of report factory with this kind of code
public Report getCorrespondingReport(Animal animal){
if(animal instanceof Dog) return new DogReport();
if(animal instanceof Cat) return new CatReport();
...
...or you could make a generic report and use reflection to inspect your Animal instance and generate your report following general rules but it might not be doable.
If I understand the question correctly, you want to call correct implementation of methods regards of the type of object you have. So if an animal is a cat, generate report method should be called from Cat class if you have code like below
public static Animal getAnimal(int id){
//your code to return object of either Cat or Dog
}
animal.generateReport();
First of all as you said,
Obviously Cat & Dog have different attributes. Cat and Dog dont have
anything else in common apart from the fact that they are Animals.
As the subclasses do not have any common functionality, define Animal as an interface instead of a class like given below
interface Animal{
public void generateReport();
}
And create Cat and Dog like this
class Cat implements Animal{
//define cat specific attributes here
public void generateReport(){
//your logic to generate cat report
}
}
class Dog implements Animal{
//define dog specific attributes here
public void generateReport(){
//your logic to generate dog report
}
}
Since the generateReport() method is defined in interface, all the classes implementing the interface must have generateReport().
So when you make a call like this,
public static Animal getAnimal(int id){
//your code to return object of either Cat or Dog
}
animal.generateReport();
the underlying object's method will be called.
If you simply want to know what animal object refers to (returned from getAnimal method i.e. either Cat or Dog), you can check it like below
class Animal{
}
class Dog extends Animal{
public String dogName = "Dog1";
}
class Cat extends Animal{
public String catName = "Cat1";
}
public class HelloWorld{
public static void main(String []args){
//call getAnimal and get the object instead of following line
Animal animal = new Cat();
if ( animal instanceof Cat ){
//cast Animal to Cat
Cat cat = (Cat) animal;
System.out.println(cat.catName);
}else if ( animal instanceof Dog ){
//cast Animal to Dog
Dog dog = (Dog) animal;
System.out.println(dog.dogName);
}
}
}
Related
So let's suppose that I have a class Animal in Java, and 3 child classes. The structure looks like this
class Animal{
...
public breed(){
//create a new child class object here
}
}
class Bird extends Animal{
...
}
class Bear extends Animal{
...
}
class Frog extends Animal{
...
}
I want to create another object in breed class, but I want to create an object of the same child class, from which the method breed() was executed. For instance, if frog.breed() was executed I want to create a new Frog object there (assumming frog is a Frog object), if bear.breed() was executed I want to create a new bear object etc.
Is there any way to handle it in animal class, or I have to override method in every child class?
It's possible if you pass a reference to a function. I suspect method references and passing functions as arguments may be a bit beyond your level right now, but it's not that complicated.
class Animal {
private final Supplier<Animal> ctor;
Animal(Supplier<Animal> ctor) {
this.ctor = ctor;
}
public Animal breed() {
return ctor.get();
}
}
class Bird extends Animal {
public Bird() {
super(Bird::new);
}
}
The advantage of overriding is that Java allows to you make the return type more specific. So if you have a Bird bird; you could do Bird chick = bird.breed();.
class Bird extends Animal {
public Bird breed() { //super return type is Animal, this return type is more specific
return new Bird();
}
}
Without overriding, you can't make the return type more specific, so you would have to assign it to a variable of type Animal chick = bird.breed(), unless you were to cast it.
Also achievable with reflection, but you should avoid it if possible.
Yes, you can do it, using reflection. But this approach isn't recommended, cause it's error-prone and you should consider rather using standard Java inheritance constructions.
Example with recursion:
class Animal{
public Animal breed() throws InstantiationException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
return this.getClass().getDeclaredConstructor().newInstance();
}
}
I'm trying to figure out whether implementing individual methods of a subclass in an abstract superclass, or casting is the better way to go about the following scenario.
Suppose I have an abstract class Animal which has two subclasses, Dog and Cat and a Main class where I save objects of Dog and Cat in an Animal array. The following is how I would go about using methods of the subclasses in a more generalized array.
class Main{
public static void main(String[] args){
Animal[] animalArray = new Animal[2];
animalArray[0] = new Cat();
animalArray[1] = new Dog();
for (Animal a : animalArray){
if (a.getClass().equals(Dog.class){
((Dog)a).bark();
} else {
((Cat)a).meow();
}
}
}
}
However a friend suggested that casting isn't best practice, and that I should define each method in the abstract superclass in the following way:
public abstract class Animal{
public abstract String meow(){
return null;
}
public abstract String bark();
return null;
}
}
After setting the return values of these methods to null I would need to use #Override and implement them in the respective subclasses.
Which way is better? I'm afraid the abstract class will be too large and will have methods assigned to subclasses that don't make sense (even if all they do is return null). I think by using casting I can make more precise uses of the methods.
meow() and bark() shouldn't be defined in the Animal class. These methods are specific to Cat and Dog classes.
You should define an abstract method as shown below, in the Animal class and override it in the sub classes.
public abstract class Animal {
public abstract String action() {};
}
public class Dog extends Animal {
#Override
public String action() {
//your implementation (bark)
}
}
public class Cat extends Animal {
#Override
public String action() {
//your implementation (meow)
}
}
Hope it answers your query.
It's hard to properly ask, so I will create an example:
class Animal {
public void doSomething() {
}
}
class Dog extends Animal {
}
class Cat extends Animal {
}
class Sheltie extends Dog {
#Override
public void doSomething() {
super.doSomething();
System.out.println("Exactly the same");
}
}
class Abbysian extends Cat {
#Override
public void doSomething() {
super.doSomething();
System.out.println("Exactly the same");
}
}
Is there a way in this case that I can override the doSomething() method in both Sheltie and Abbysian without duplicating a code and even typing #Override public void doSomething(). On top of that, I'd actually like to call super.doSomething() as well. I cannot change inheritance at all.
If those classes extended directly Animal I would create a class "between" them. I was thinking of using interface with default method but that didn't help as well.
Once you broke out into Cats and Dogs you discovered the limitations of class hierarchy - eventually you'll hit a point of no return.
Thankfully there is a thing called an Entity Component System (ECS) that is commonly used when you want to mish-mash functionality amongst many different entities.
A cat can wag its tail. A dog can wag its tail... but not all animals have tails!
So you register a component called Tail to the tailed animal entities.
I found this on the StackExchange code review group. https://codereview.stackexchange.com/questions/163215/entity-component-system-ecs
Decorator pattern can do this trick but it would be applied to specific instances of Sheltie / Abyssinian not to these classes:
class AnimalDecorator extends Animal {
protected Animal animal;
public AnimalDecorator(Animal animal) {
this.animal = animal;
}
#Override
public void doSomething() {
animal.doSomething(); // instead of call to super.doSomething
System.out.println("Exactly the same");
}
}
class Sheltie extends Dog { // doSomething not overridden
}
class Abyssinian extends Cat { // doSomething not overridden
}
// decorated instances
Animal sheltie = new AnimalDecorator(new Sheltie());
Animal abyssinian = new AnimalDecorator(new Abyssinian());
sheltie.doSomething();
abyssinian.doSomething();
What's definitely the best way to call a different method of a class according to a different object type in a List?
common example:
public class Dog extends Animals{
...
public void say(){
System.out("i'm a dog");
}
public class Cat extends Animals{
...
public void say(){
System.out("i'm a cat");
}
public class OtherClass {
public void route(){
List<Animals> aList = new ArrayList<>();
a.add(new Dog());
a.add(new Cat());
for(Animals a:aList)
methodOverloaded(a); ---> that's the point <---
}
public methodOverloaded(Dog d){
d.say();
}
public methodOverloaded(Cat c){
c.say();
}
}
Of course, the metaphorical goal is print I'm a dog on the first iteration and I'm a cat on second running methodOverloaded().
I tried
Visitor Pattern
instanceOf()
but I'm looking for better solution.
edit : I stricrtly want to call the overloaded methods of example's OtherClass.
The best way is to define abstract method in Animal and override it in child classes. That's how the polymorphism works.
No need of overloadings.
public abstract class Animal {
public abstract void say();
}
public class Dog extends Animal {
#Override
public void say() {
System.out.println("Bark");
}
}
public class Cat extends Animal {
#Override
public void say() {
System.out.println("Meow");
}
}
Usage:
public class Main() {
public static void main(String[] args) {
List<Animals> aList = new ArrayList<>();
a.add(new Dog());
a.add(new Cat());
for (Animals a : aList)
a.say();
}
}
Output:
Bark
Meow
____UPDATE_1
I would like to add some comments why the overloading these methods is not a good idea.
If you will add the following to your code - it will compile:
public methodOverloaded(Animal a) {
a.say();
}
But it will not work as you're expecting. It will call public methodOverloaded(Animal a) for all the elements of List.
Why does this happen?
For all iterations of the loop, the compile-time type of the parameter is Animal. The runtime type is different in each iteration, but this does not affect the choice of overloading. Because the compile-time type of the parameter is Animal, the only applicable overloading is the third one.
The behavior of this program is counterintuitive because selection among overloaded methods is static, while selection among overridden methods is dynamic.
The correct version of an overridden method is chosen at runtime, based on the runtime type of the object on which the method is invoked.
This can be fixed with:
public methodOverloaded(Animal a) {
if (a instanceof Cat) ? "Meow" :
(a instanceof Dog) ? "Bark" : "Unknown Animal"
}
Of course suggested option with overriding methods demonstrates better approach and more clean code.
Also, a safe, conservative policy is never to export two overloadings with
the same number of parameters, because it can confuse the client of the API. You can always give methods different names instead of overloading them.
But there is the case when at least one corresponding formal parameter in each pair of overloadings has a “radically different” (when it's clearly impossible to cast an instance of either type to the other) type in the two overloadings.
For example, ArrayList has one constructor that takes an int and a second constructor that takes a Collection. It is hard to imagine any confusion over which of these two constructors will be invoked under any circumstances.
You need to define the method in Animal and make it abstract
abstract class Animal {
public abstract void say();
}
this way, you can override this method in each child of Animal, and all you got to do is a.say()
each object will call their respective method.
You can do it like this:
for(Animals a:aList){
if(a instanceof Dog){
methodOverloaded((Dog) a);
}else if(a instanceof Cat){
methodOverloaded((Cat) a);
}
}
But according to the scenario you have described in the question, #J-Alex answer is a good way to go.
I can show you how "Factory Design pattern" is the suitable way here.
Define you main class like:
public abstract class Animal {
public abstract void say();
}
public class Dog extends Animal {
#Override
public void say() {
System.out.println("Bark");
}
}
public class Cat extends Animal {
#Override
public void say() {
System.out.println("Meow");
}
}
public class FactoryClass{
public static Animal getCorrectAnimal(String type){
if("Cat".equalsIgnoreCase(type)) return new Cat();
else if ("Dog".equalsIgnoreCase(type)) return new Dog();
return null;
}
}
public class TestFactoryClass {
public static void main(String[] args) {
Animal an = ComputerFactory.getCorrectAnimal("Cat");
List<Animals> aList = new ArrayList<>();
a.add(FactoryClass.getCorrectAnimal("Dog"));
a.add(FactoryClass.getCorrectAnimal("Cat"));
for (Animals a : aList)
a.say();
}
}
}
Trust me, if you will analyse the level of Abstraction here, it is awesome. The client/consumer never has to know the Dog or Cat class, he/she just has to know the type and a general abstract class Animal. You can even get rid of type here if you use one higher level of abstraction; you can read "Abstract Factory Design" for that. In this way, you expose the least of your class features (like here you exposed Dog and Cat class by using them directly with new in main class). Please upvote if you are satisfied.
I'm kind of stuck on the following question:
What two Java language mechanisms allow the type of an object reference variable to be "different" than the type of the object to which it refers? Give specific examples to illustrate. In what sense are they not different at all?
My current answer is that it is "implement" and "extend" right? And they are similar because they both will make a class that at least will posses all of the method signatures of the super class which can be actual, abstract, or an interface. Is this correct? Thanks in advance!
That is more or less correct. The second part of your answer should talk about subtyping. In Java, it is not sufficient for objects to just have the same method signatures. There actually has to be a declared subtyping relationship (via extends / implements).
This is not mere pedantry. In some languages (but not Java), the mere existence of compatible method signatures is sufficient for type compatibility. This is called "duck typing".
Implements
interface Animal {
void attackHuman(); // actually public abstract by default
}
class Horse implements Animal {
public void attackHuman() { }; // must implement
}
// type and reference the same
Horse a1 = new Horse();
// type and reference different
Animal a2 = a1;
Extends
class Animal {
void attackHuman();
}
class Dinosaur extends Animal {
// attackHuman() inherited
}
// type and reference the same
Dinosaur a1 = new Dinosaur();
// type and reference different
Animal a2 = a1;
See this example....
- Here the Animal is the Super-Class, and the Dog and Cat are inherited out of it.
- You can Create a Dog object using an Animal Object Reference Variable.
- This is known as Class Polymorphism.
public class Test {
public static void main(String[] args){
Animal a = new Dog();
new Hospital().treatAnimal(a);
}
}
class Animal {
public void sayIt(){
}
}
class Dog extends Animal{
public void sayIt(){
System.out.println("I am Dog");
}
}
class Cat extends Animal{
public void sayIt(){
System.out.println("I am Cat");
}
}
See the NEXT PAGE for the Remaining Code
class Hospital{
public void treatAnimal(Animal a){
if(a instanceof Dog){
a.sayIt(); // Will output "I am Dog"
}
else{
a.sayIt(); // Will output "I am Cat"
}
}
}