patternjavaModerate
Decomposing the animal kingdom
Viewed 0 times
kingdomthedecomposinganimal
Problem
I have implemented a
Is it an intelligent approach, with respect to decomposition, locality and procedural abstraction?
```
package Hierarchies;
public class Mammals {
public Mammals(){
}
public void giveMilk(){
//REQUIRES: nothing
//MODIFIES: nothing
//EFFECTS : Display message of produce Milk
System.out.println("Produce milk to feed their babies.");
}
public void haveHair(){
//REQUIRES: nothing
//MODIFIES: nothing
//EFFECTS : Display message of Have Hair
System.out.println("Have hair on their bodies.");
}
}
public class Cats extends Mammals{
public Cats(){
}
public void Sounds(){
//REQUIRES: nothing
//MODIFIES: nothing
//EFFECTS : Display message of Sound
System.out.println("Meow Meow Meow!!!.");
}
public void Diet(){
//REQUIRES: nothing
//MODIFIES: nothing
//EFFECTS : Display message of Diet
System.out.println("Cats are Carnivores.");
}
}
public class Elephants extends Mammals {
public Elephants(){
}
public void Trunk(){
//REQUIRES: nothing
//MODIFIES: nothing
//EFFECTS : Display message of Have Trunk
System.out.println("Have Trunk.It functions for grasping, breathing, feeding, dusting, smelling, drinking.");
}
public void Diet(){
//REQUIRES: nothing
//MODIFIES: nothing
//EFFECTS : Display message of Diet
System.out.println("Elephants are Herbivorous.");
}
}
public class Dogs extends Mammals {
public Dogs(){
}
public void Bark(){
//REQUIRES: nothing
//MODIFIES: nothing
//EFFECTS : Display message of Bark
System.out.println("Woo Woo!!!.");
}
public void Diet(){
//REQUIRES: nothing
//MODIFIES: nothing
//EFFECTS : Display mess
Mammal class hierarchy in Java.Is it an intelligent approach, with respect to decomposition, locality and procedural abstraction?
```
package Hierarchies;
public class Mammals {
public Mammals(){
}
public void giveMilk(){
//REQUIRES: nothing
//MODIFIES: nothing
//EFFECTS : Display message of produce Milk
System.out.println("Produce milk to feed their babies.");
}
public void haveHair(){
//REQUIRES: nothing
//MODIFIES: nothing
//EFFECTS : Display message of Have Hair
System.out.println("Have hair on their bodies.");
}
}
public class Cats extends Mammals{
public Cats(){
}
public void Sounds(){
//REQUIRES: nothing
//MODIFIES: nothing
//EFFECTS : Display message of Sound
System.out.println("Meow Meow Meow!!!.");
}
public void Diet(){
//REQUIRES: nothing
//MODIFIES: nothing
//EFFECTS : Display message of Diet
System.out.println("Cats are Carnivores.");
}
}
public class Elephants extends Mammals {
public Elephants(){
}
public void Trunk(){
//REQUIRES: nothing
//MODIFIES: nothing
//EFFECTS : Display message of Have Trunk
System.out.println("Have Trunk.It functions for grasping, breathing, feeding, dusting, smelling, drinking.");
}
public void Diet(){
//REQUIRES: nothing
//MODIFIES: nothing
//EFFECTS : Display message of Diet
System.out.println("Elephants are Herbivorous.");
}
}
public class Dogs extends Mammals {
public Dogs(){
}
public void Bark(){
//REQUIRES: nothing
//MODIFIES: nothing
//EFFECTS : Display message of Bark
System.out.println("Woo Woo!!!.");
}
public void Diet(){
//REQUIRES: nothing
//MODIFIES: nothing
//EFFECTS : Display mess
Solution
Summary
Use Singular for Class Names
Use a name which represents a single object, not the whole group. I.e. change
An exception are utility classes, i.e. the utility class for arrays is called
However, your classes are not utility classes. Therefore, use singular.
Appropriate Inheritance
Inheritance is appropriate where the subclass inherits all or almost all traits of the superclass.
Not every noun is a class!
Nouns aren't always classes, they can also be attributes. Pet is such an example. Both, a dog and a cat can be a pet. In fact, one can have any animal as pet. Pet is much more a description of the relationship between the pet holder and the animal.
In your model, all your classes which describe roles should probably simply be variables, not classes.
Keep in mind that in a language like Java, and most (but not all) programming languages, an object cannot change its class during its lifetime.
A dog is a dog is a dog and stays a dog, even if it becomes a pet, goes hunting and so on. It should be one and only one object achieving all these things.
Sometimes it can of course make sense to create classes for roles. You could have a
Then the
Prefer composition over inheritance.
This is not always appropriate, but you might be interested in learning what it means. Inheritance is really over-used. And inheritance is, unlike some people think, not at the heart of object-oriented programming languages. There are, in fact, programming languages out there which we happily call object-oriented which do not support this simple "extends"-type of inheritance at all. JavaScript is such an example.
Code Conventions
In Java, the convention is to start method names in lower case. Change
Use Proper and Consistent Grammar
Sometimes you use the normal verb form, i.e.
Short Methods
Your
A method should do only one thing, it should do it well, and it should do it only. (Robert C. Martin)
So, think of how to split the
- Use singular for class names.
- Use inheritance where appropriate.
- Follow code conventions.
- Be consistent about grammar.
- Write short methods.
Use Singular for Class Names
Use a name which represents a single object, not the whole group. I.e. change
Dogs to Dog.An exception are utility classes, i.e. the utility class for arrays is called
Arrays, the utility class for collections is called Collections. It's not possible to create objects of utility classes (they prevent it by being a zero element enum or a class with a private constructor).However, your classes are not utility classes. Therefore, use singular.
Appropriate Inheritance
Inheritance is appropriate where the subclass inherits all or almost all traits of the superclass.
Not every noun is a class!
Nouns aren't always classes, they can also be attributes. Pet is such an example. Both, a dog and a cat can be a pet. In fact, one can have any animal as pet. Pet is much more a description of the relationship between the pet holder and the animal.
In your model, all your classes which describe roles should probably simply be variables, not classes.
Keep in mind that in a language like Java, and most (but not all) programming languages, an object cannot change its class during its lifetime.
A dog is a dog is a dog and stays a dog, even if it becomes a pet, goes hunting and so on. It should be one and only one object achieving all these things.
Sometimes it can of course make sense to create classes for roles. You could have a
class Pet. But in that case, Pet should not be a subclass of these other classes like class Dog. Instead you should use delegation / composition, like this:class Pet {
Animal animal;
}Then the
class Pet describes a role, and that role can be fulfilled by any type of animal.Prefer composition over inheritance.
This is not always appropriate, but you might be interested in learning what it means. Inheritance is really over-used. And inheritance is, unlike some people think, not at the heart of object-oriented programming languages. There are, in fact, programming languages out there which we happily call object-oriented which do not support this simple "extends"-type of inheritance at all. JavaScript is such an example.
Code Conventions
In Java, the convention is to start method names in lower case. Change
Task() to task(), Sounds() to sounds() and so on.Use Proper and Consistent Grammar
Sometimes you use the normal verb form, i.e.
giveMilk(), sometimes you use the third person form, i.e. sounds(). Be consistent. It's common in Java to always use the normal verb form, not the third person form, in code. And in comments, it's common in Java to always use the third person form, except if you directly address the reader.Short Methods
Your
main() method is far too long. Good method sizes are like 3-4 lines, sometimes 5, only in rare situations a bit more.A method should do only one thing, it should do it well, and it should do it only. (Robert C. Martin)
So, think of how to split the
main() method into a bunch of smaller methods.Code Snippets
class Pet {
Animal animal;
}Context
StackExchange Code Review Q#83546, answer score: 14
Revisions (0)
No revisions yet.