OO question

F

Frank

Hello,
how should I do this?
Class X
classes A, B and C inherit from X.
A,B and C are basically the same with the exception of 1 routine that is
different for each class and is used by several methods in the classes. The
only way I see is to put all methods from X in A,B and C. But then I don't
see the advantages of OO!?!?
How can I do this in a better way?
Thanks
Frank
 
E

Erik Molenaar

Maybe this is the answer you were looking for:

You can put all methods that are common for A,B and C in
an abstract class X. They can use the abstract method1()
that should be implemented in all inhereted classes A,B and C

example:

public abstract class X
{
protected abstract string method1();
public X(){}

public string GetIt()
{
return method1();
}
}

public class A : X
{
public A(){}
protected override string method1()
{
return "Class A";
}
}

public class B : X
{
public B(){}
protected override string method1()
{
return "Class B";
}
}

//call like this:
A a = new A();
textBox1.Text = a.GetIt();
B b = new B();
textBox2.Text = b.GetIt();


Erik
 
N

Nick Malik [Microsoft]

Hi Frank.

Simple. Create a base class. Abstract.

In that class, create all your common methods. Also create a single
abstract method, private, that the common methods will call in to. Do not
define this abstract method's code.

Now, in each descendent, create ONLY the private method that is unique.

This is called the Template Method pattern.
http://www.dofactory.com/Patterns/PatternTemplate.aspx

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
J

Joanna Carter \(TeamB\)

how should I do this?
Class X
classes A, B and C inherit from X.
A,B and C are basically the same with the exception of 1 routine that is
different for each class and is used by several methods in the classes. The
only way I see is to put all methods from X in A,B and C. But then I don't
see the advantages of OO!?!?

There is a Design Pattern called the Template Method that allows for
circumstances like this.

In your base class declare the method that differs, either as abstract or as
an empty method, unless there is some common code.

Call the differing method from the other methods of your base class

Override the differing method in sub-classes, calling base if necessary for
any common behaviour.

//////////////////////////
class X
{
protected abstract void MethodThatChanges();

// or

protected virtual void MethodThatChanges()
{
// Base behaviour
}

public void MethodThatCallsMTC()
{
MethodThatChanges();
}
}

class A : X
{
protected override void MethodThatChanges()
{
// optionally
base.MethodThatChanges();

// Class A code
}
}

class B : X
{
protected override void MethodThatChanges()
{
// Class B code
}
}

classC : X
{
protected override void MethodThatChanges()
{
// Class C code
}
}
////////////////////////////////

Joanna
 
J

Joanna Carter \(TeamB\)

In that class, create all your common methods. Also create a single
abstract method, private, that the common methods will call in to. Do not
define this abstract method's code.

Now, in each descendent, create ONLY the private method that is unique.

PMFJI but surely the primitive methods must at least be protected and
virtual to allow polymorphism to work from the common methods in the base
class ?

Joanna
 
N

Nick Malik [Microsoft]

Joanna Carter (TeamB) said:
PMFJI but surely the primitive methods must at least be protected and
virtual to allow polymorphism to work from the common methods in the base
class ?

Hi Joanna

Actually, the distinctions made by virtual are not needed for the basic
pattern.

However, these distinctions become much more important if you have an
inheritance tree where the method body is defined at more than one level.

Protected is not needed in my example because the derived class has the
implementation of the abstract method and the shared methods are public. If
I wanted to have a private method in the BASE class that the methods in the
derived classes can call, it would be best to declare them as protected.

However, your point is a good one. There are some pretty fine distinctions
here.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 
J

Jeff Louie

Frank....

There are actually two distinct approaches to this problem. You can
declare
an abstract base class with an abstract method _or_ you can declare an
interface with the "abstract" method signature. You can then create a
concrete
class with a concrete method that takes a reference of the interface
type. Both
of these are examples of deferring the implementation to a more
knowledgable class.

http://www.geocities.com/jeff_louie/OOP/twisted2.htm

Regards,
Jeff
 
F

Frank

Thanks all, I can use this.
Frank
Erik Molenaar said:
Maybe this is the answer you were looking for:

You can put all methods that are common for A,B and C in
an abstract class X. They can use the abstract method1()
that should be implemented in all inhereted classes A,B and C

example:

public abstract class X
{
protected abstract string method1();
public X(){}

public string GetIt()
{
return method1();
}
}

public class A : X
{
public A(){}
protected override string method1()
{
return "Class A";
}
}

public class B : X
{
public B(){}
protected override string method1()
{
return "Class B";
}
}

//call like this:
A a = new A();
textBox1.Text = a.GetIt();
B b = new B();
textBox2.Text = b.GetIt();


Erik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top