simpleguy
simpleguy

Reputation: 1

Hierarchy structure for these entities

I need to define an object oriented class hierarchy structure for the following entities: Computer,Processor,Celeron Processor,Athlon Processor,HP,Dell,Mouse,Keyboard.

I came up with something as follows:

public interface Computer
{
    public void mouse();
    public void keyboard();
}
public class Processor
{
   public void Celeron(){ }
   public void Athlon(){ }
}
public class HP extends Processor,implements Computer
{
}

Can someone point out what is wrong with this design?Also,if anyone can suggest a better design it would be great.

Thanks.

Upvotes: 0

Views: 179

Answers (5)

Raoul Gerz
Raoul Gerz

Reputation: 21

I would rather say, a Computer has a Processor. HP is a Computer; Dell is a Computer. Celeron is a Processor; Athlon is a Processor. And a Computer has a Mouse as well as a Keyboard. has a: Association. is a: Inheritance.

Upvotes: 1

seh
seh

Reputation: 15259

Irrespective of programming language, stop and consider how to build an information model to represent the things in question. You have a few kinds of relationships at your disposal:

  • is-a

    Some thing or concept is a refinement of or interchangeable with some other concept, such as a "Celeron processor" being a processor. Inheritance can model this relationship, but not all inheritance is meant to model this relationship.

  • has-a

    Some kind of thing contains or is associated with some other thing, such as a computer having a keyboard connected to it. Member varibles or fields in structures can model this relationship, both as by-value containment (including controlling the life-cycle of the contained thing) or by-reference aggregation (usually not controlling the life-cycle of the associated thing).

  • uses

    More general than has-a, one thing requires a reference to some other thing to get its job done. This can be represented as a required parameter to a member function or method.

For your problem, we can see that a Celeron is a processor, that a computer has a processor (one or more), that HP is a company that sells computers, or, alternately, that a computer has a responsible vendor (one or more) and HP is a vendor. HP could sell mice too, and a computer can have zero or more mice connected.

I'm reluctant to tell you how to use Java to build such a model. There are many solutions, and exploring more than one solution also offers insight into why the relational model is more expressive for information modeling than Java's class and interface facility.

Upvotes: 1

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120987

I think something like the following would probably do:

public interface IInputDevice {}
public interface IProcessor {}

public abstract class Computer 
{
    IInputDevice[] inputDevices;
    IProcessor processor;
}

public class Mouse implements IInputDevice {}
public class KeyBoard implements IInputDevice {}

public class AthlonProcessor implements IProcessor {}
public class CeleronProcessor implements IProcessor {}

public abstract class HP extends Computer
{

}

public abstract class Dell extends Computer
{

}

Upvotes: 2

Flinsch
Flinsch

Reputation: 4340

I would rather say, a Computer has a Processor.
HP is a Computer; Dell is a Computer.
Celeron is a Processor; Athlon is a Processor.
And a Computer has a Mouse as well as a Keyboard.
has a: Association.
is a: Inheritance.

Upvotes: 5

bluehallu
bluehallu

Reputation: 10285

You're implementing mouse and keyboard, and celeron and athlon as methods. They should be separated Classes. Computer should be a class too and not an interface.

And I don't see why would you want "HP" to extend processor and to implement Computer.

Upvotes: 0

Related Questions