d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Object Oriented Design Problem
Add Reply New Topic New Poll
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Jun 4 2014 12:19am
I'm currently working on a project and I've reached a roadblock, and I need some perspective on how to design this

To give an analogy, I'll pretend this has to do with dogs - they have similar traits but each differ in their own way
Code
public class Doberman {
functionA();
functionB();
functionC();
}


Code
public class Chihuahua {
functionA();
functionB();
functionC();
}

Now I have a class called Dog - what I would like to do , although I can't, is design it so that Dog will extend Doberman iff some condition is met, and it will extend Chihuahua if the condition is not met.
The problem is I HAVE to use the Dog class because it is plugged in and Im not supposed to alter the code behind that - so in other words I can't put conditions outside of the dog class.

So ideally Dog would act as a layer between the plugin and the 2 types of dogs. If a condition is met, start calling the doberman code. Otherwise, call the chihuahua code
Code
public class Dog {

if (condition == true)
call functionA() from the doberman
else
call functionA() from the Chihuahua


}

But since I can't extend 2 classes or extend based on a condition, my idea is this. I think it works but I'm not sure it's the best solution since I'm inlining a conditionFunction() call in several functions
Code
public class [strikethrough]Dog[/strikethrough] Chihuahua extends Doberman{
boolean Conditionfunction(){
//return true if Doberman
}

functionA(){
if (conditionFunction()) super.functionA(); //Use the doberman code
else
//continue functions code as a chihuahua
}

//functionB() <--doesnt need to call it here since doberman is abstract and both chihuahua and doberman have the same trait here (eliminates redundancy)

functionC(){
if (conditionFunction()) super.functionC(); //Use the doberman code
else
//continue functions code as a chihuahua
}


Code
public abstract class Doberman{
functionA(){
//coded as a Doberman (but also works for chihuahua
}

functionB(){
//coded as a Doberman (doesnt work for chihuahua)
}

functionC(){
//coded as a Doberman (but also works for chihuahua
}


Since the chihuahua (formerly "dog") class is plugged in, its getting its function calls elsewhere. So it will try to call functionB() in chihuahua, but since chihuahua doesnt have it, it will grab from Doberman


Thanks :)

This post was edited by oOn on Jun 4 2014 12:34am
Member
Posts: 2,267
Joined: Jan 3 2006
Gold: 1,399.31
Jun 4 2014 03:02am
Seems like a simple interface inheritance problem to me, but I might have misunderstood your needs.

Anyway:
Dog is an interface:
Code

public interface Dog {
public void bark();
public void bite();
}


and there are some classes that implement it:
Code

public class Chihuahua implements Dog {
@Override
public void bite() {
System.out.println("Only scratch!");
}

@Override
public void bark() {
System.out.println("Ears popping!");
}
}

public class Doberman implements Dog {
@Override
public void bite() {
System.out.println("Bites the leg off!");
}

@Override
public void bark() {
System.out.println("Silence...");
}
}


You instantiate appropriate class on basis of your needs and define variable of interface type:

Code

public class Application {
public static void main(String[] args) {
Dog dog;
boolean needBigDog = true;

System.out.println("Need BIG dog!");
if (needBigDog) {
dog = new Doberman();
} else {
dog = new Chihuahua();
}

dog.bark();
dog.bite();


needBigDog = false;
System.out.println("Need SMALL dog!");
if (!needBigDog) {
dog = new Chihuahua();
} else {
dog = new Doberman();
}


dog.bark();
dog.bite();

}
}



When run prints:

Need BIG dog!
Silence...
Bites the leg off!
Need SMALL dog!
Ears popping!
Only scratch!
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Jun 4 2014 10:29am
Will try later thanks

This post was edited by oOn on Jun 4 2014 10:30am
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Jun 4 2014 11:08am
The dog class can't be instantiated because the way it's set up, the program makes function calls directly to the class that is plugged in. So in other words it will try to call functionA() inside of application
Member
Posts: 5,988
Joined: May 6 2006
Gold: 30.00
Jun 4 2014 05:36pm
Well I hve a solution now so mark this as resolved!
Member
Posts: 2,267
Joined: Jan 3 2006
Gold: 1,399.31
Jun 4 2014 10:54pm
If this was an assignment and you really don't care about then fine, otherwise they were trying to tech you a lesson to program to interface and you should really know what gets called when.

You don't really care how something is implemented, you care what it can do.
So when you for example implement German Shepherd the same program should work because it also implements Dog's required methods.

Also notice when you instantiate appropriate class it should be done in some other place which serves for only that purpose, not in the main program.
It's the ubiquitous concept of factories, parts of program that sole purpose is to create objects on basis of some rules known to them.

You can try to implement this concept by creating DogFactory class that has some method that returns Dog on basis of some parameter.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll