Reputation: 368
So I'm not very good with OOP, and am a little stumped. If I already have classes that are FruitHandlers from the FruitHandler library that look something like this:
public partial class AppleCleaner : FruitHandler {
public AppleCleaner(int fruitSize) {
CleanApple(fruitSize);
}
void CleanApple(int size) {
//code to clean an apple;
}
}
public partial class PearCleaner : FruitHandler {
public PearCleaner(int fruitSize) {
CleanPear(fruitSize);
}
void CleanPear(int size) {
//code to clean a pear;
}
}
public partial class BananaCleaner : FruitHandler {
public BananaCleaner(int fruitSize) {
CleanBanana(fruitSize);
}
void CleanBanana(int size) {
//code to clean a banana;
}
}
and I want to make a class that also has the base class FruitHandler, but is capable of using CleanApple, CleanPear, or CleanBanana that looks something like this:
public partial class FruitEater : FruitHandler {
public FruitEater(Fruit fruit) {
if (fruit.Name == "Apple") {
CleanApple(fruit.size);
} else if (fruit.Name == "Pear") {
CleanPear(fruit.size);
} else if (fruit.Name == "Banana") {
CleanBanana(fruit.size);
}
EatFruit(fruit);
}
void EatFruit(Fruit fruit) {
// eat it;
}
}
I can refactor these pretty liberally, but the caveat is that the base class for all of them must be a FruitHandler (because in real life these are all Forms and the base class must be Form).
Upvotes: 0
Views: 166
Reputation: 33094
You should use interfaces for this. Unless each class is going to execute code in their base class (which isn't the case here), you should use an interface to define the common methods across the classes:
public interface IFruit {
void Clean(int Size);
}
public partial class AppleCleaner : IFruit, FruitHandler {
public AppleCleaner(int fruitSize) {
Clean(fruitSize);
}
void Clean(int size) {
//code to clean an apple;
}
}
public partial class PearCleaner : IFruit, FruitHandler {
public PearCleaner(int fruitSize) {
Clean(fruitSize);
}
void Clean(int size) {
//code to clean a pear;
}
}
public partial class BananaCleaner : IFruit, FruitHandler {
public BananaCleaner(int fruitSize) {
Clean(fruitSize);
}
void Clean(int size) {
//code to clean a banana;
}
}
This also simplifies your FruitEater
class quite a bit:
public partial class FruitEater : FruitHandler {
public FruitEater(IFruit fruit, int size) {
fruit.Clean(size);
EatFruit(fruit);
}
void EatFruit(IFruit fruit) {
// eat it;
}
}
Upvotes: 2
Reputation: 18474
Normally you'd put clean in the base class, but as you said it was a Form in real world example, you could use an interface.
public interface IFruitCleaner {
void Clean(int size);
}
public partial class AppleCleaner : FruitHandler, IFruitCleaner
{
public AppleCleaner(int fruitSize)
{
Clean(fruitSize);
}
void Clean(int size)
{
//code to clean an apple;
}
}
public partial class PearCleaner : FruitHandler, IFruitCleaner
{
public PearCleaner(int fruitSize)
{
Clean(fruitSize);
}
void Clean(int size)
{
//code to clean a pear;
}
}
public partial class BananaCleaner : FruitHandler, IFruitCleaner
{
public BananaCleaner(int fruitSize)
{
Clean(fruitSize);
}
void Clean(int size)
{
//code to clean a banana;
}
}
Upvotes: 1
Reputation: 30082
You could do something like this:
interface IFruitCleaner {
void Clean(int size)
}
public partial class AppleCleaner : FruitHandler, IFruitCleaner {
public AppleCleaner(int fruitSize) {
Clean(fruitSize);
}
void Clean(int size) {
//code to clean an apple;
}
}
public partial class FruitEater : FruitHandler {
public FruitEater(Fruit fruit, IFruitCleaner cleaner) {
cleaner.Clean(fruit.size);
EatFruit(fruit);
}
void EatFruit(Fruit fruit) {
// eat it;
}
}
Seems like you would want to pass the fruit to AppleCleaner
as opposed to just the size, but I've left it as you have it.
Upvotes: 3