How to inherit if base class is burned?

  • Thread starter Thread starter Brett Romero
  • Start date Start date
B

Brett Romero

Say I have several classes that have implemented the same code to do
something. Perhaps its connection information. They all have inherited
some class and not necessarily the same one. I'd like to take the
duplicated code and reuse it. I can't put it into ClassA, for example,
and allow these classes to inherit ClassA since they have used the one
inheritance slot. I could make ClassA a singleton, since ClassA needs
state.

Besides the singleton, how else can I reuse this code? Basically, I
need a file include (used so much in Cold Fusion and SHTML files).

Thanks,
Brett
 
Brett Romero said:
Say I have several classes that have implemented the same code to do
something. Perhaps its connection information. They all have inherited
some class and not necessarily the same one. I'd like to take the
duplicated code and reuse it. I can't put it into ClassA, for example,
and allow these classes to inherit ClassA since they have used the one
inheritance slot. I could make ClassA a singleton, since ClassA needs
state.

Besides the singleton, how else can I reuse this code? Basically, I
need a file include (used so much in Cold Fusion and SHTML files).

Specify an interface with the appropriate behaviour, and implement the
interface by delegating to an aggregated instance of another class
which has the behaviour you want.
 
Brett Romero said:
Thanks Jon. Can you give a small example of what this would look like?

Sure:

public interface IFoo
{
void Bar();
}

public class Foo : IFoo
{
public void Bar()
{
Console.WriteLine ("Bar!");
}
}

public class SomethingElse : AnotherClass, IFoo
{
IFoo m_foo;

public SomethingElse()
{
// Alternatively, the implementation could be
// given to the class in the constructor or a property
m_foo = new Bar();
}

public void Bar()
{
m_foo.Bar();
}
}
 
Most AOP (Aspect Oriented Programming) frameworks such as naspect or aspect#
support doing this dynamically (at runtime) as well so you never write any
code such as this, they write this code for you. This is an AOP concept
known as an inner type member or "mixin" (See background on mixin
http://en.wikipedia.org/wiki/Mixin)

This pattern is also known as an interface/implementor pattern.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
 
Thanks Jon. After seeing the code, how can it work for this sceario?
I have two assemblies
dllA
dllB

dllB file references dllA but not the other way around. I have
dllB.ClassB.UserId

dllA needs the value in UserId and will return it via
dllA.State.UserId.
From what I see in the code you posted, dllA will need to new up
ClassB, which isn't possible since dllA doesn't reference ClassB. Is
there a way around that?

Thanks,
Brett
 

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

Back
Top