Sounds like you need a Façade of some sort. The problem I see with how you
have this is that Bar inherits from Foo. Downcast Bar back to a Foo instance
and you have all of Foo's methods and properties, undoing what you were
trying to accomplish in the first place.

A façade on the other hand basically creates another class with its own
methods that work on the classes you don't necessarily want to expose to
outside assemblies. It won't be as clean as you want most likely, but it
does provide you with the ability to make large changes to your Foo and Bar
class without needing to fix clients who use your classes.

Here's an example, with a Façade actually being just a wrapper for the
classes:

class Foo{
        void Method1() {}
        void Method2() {}
        void Method3() {}
        void Method4() {}
}

class FooFacade {
        private Foo foo = new Foo();
        void Method1() { foo.Method1(); }
        void Method2() { foo.Method2(); } 
}
class BarFacade {
        private Foo foo = new Foo();
        void Method2() { foo.Method2(); }
        void Method3() { foo.Method3(); }
}

This isn't the best example of a façade, but you should hopefully get the
point. What's better is you can create facades for large portions of your
system (subsystems) and expose only your façade to outside assemblies,
allowing you to maintain your façade interfaces while changing your classes
without breaking anything.

Adam..

-----Original Message-----
From: Chris Jenkin [mailto:[EMAIL PROTECTED]] 
Sent: Thursday, May 09, 2002 4:50 PM
To: [EMAIL PROTECTED]
Subject: [DOTNET] General object question?


I have a class that has grown quite large. It has come to pass that I would
like to expose a very small subset of this class' functionality as another
class(?). Here's the example:

I have class Foo with 10 methods lets say (Meth1 - Meth10)

class Foo {
        Meht1 {
        }

        Meth2 {
        }

        etc...
}

What I would like to do is something like the following:

1) Create a class that exposes only a portion of the functionality of class
Foo, Bar is a good name for this.

class Bar : inherits Foo (?) {

        Meth1 {
        }

        Meth7 {
        }
}

2) Give the "user" a choice of which class they would like to use depending
on the functionality they need.

class TestFooAndBar {
        Foo myFoo = new Foo();
        Bar myBar = new Bar();

        myFoo.Meth1 //works
        myFoo.Meth5 //works

        myBar.Meth1 //works just like the myFoo call above
        myBar.Meth5 //oops, the method doesn't exist

}

Is this doable? I don't really want to rewrite Foo into a new Bar, because
Bar would need about 90% of the internal Foo code to work properly and I
would like to avoid big changes to Foo as it is already implemented in a
large volume of code. What to do?

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to