Any design pattern for this? - Object
This is a discussion on Any design pattern for this? - Object ; I have a class, say A
(the following examples are in Java)
public class A
{}
(I have a lot of code which already uses this class.)
Now, it turns out, I need my application to behave in two different
...
-
Any design pattern for this?
I have a class, say A
(the following examples are in Java)
public class A
{}
(I have a lot of code which already uses this class.)
Now, it turns out, I need my application to behave in two different
ways depending on the value of an environment variable.
I'd like to refactor A into two subclasses: A1 and A2, and would like
to dynamically create objects of either A1 or A2, depending on the
value of the environment variable.
I could use some kind of Factory here, but I really wouldn't like to
break the older code (as I would have to replace all calls like "A a =
new A()" to "A a = AFactory.getInstance().create()" or something on
those lines).
How could I best create objects of subclasses A1 or A2 dynamically, at
the same time not breaking the existing code which uses class A?
-
Re: Any design pattern for this?
Debajit Adhikary <debajit1> wrote:
> I have a class, say A
> (the following examples are in Java)
>
> public class A
> {}
>
> (I have a lot of code which already uses this class.)
>
> Now, it turns out, I need my application to behave in two different
> ways depending on the value of an environment variable.
>
> I'd like to refactor A into two subclasses: A1 and A2, and would like
> to dynamically create objects of either A1 or A2, depending on the
> value of the environment variable.
>
> I could use some kind of Factory here, but I really wouldn't like to
> break the older code (as I would have to replace all calls like "A a =
> new A()" to "A a = AFactory.getInstance().create()" or something on
> those lines).
>
> How could I best create objects of subclasses A1 or A2 dynamically, at
> the same time not breaking the existing code which uses class A?
Use the State Pattern.
interface SubA { }
class A {
private SubA subA;
public A() {
if ( environmentVariable == 1 )
subA = new A1();
else
subA = new A2();
}
// for all other methods, for example:
public void foo() {
subA.foo();
}
}
-
Re: Any design pattern for this?
> Use the State Pattern.
>
> interface SubA { }
>
> class A {
> private SubA subA;
> public A() {
> if ( environmentVariable == 1 )
> subA = new A1();
> else
> subA = new A2();
> }
>
> // for all other methods, for example:
> public void foo() {
> subA.foo();
> }
Thanks 
Is it possible to delegate all the method calls to subA automatically?
I mean.. instead of having to manually write out all the methods
delegated (like foo()) - is it possible to have all such methods
"generated" automatically in some way (lets say in Java or C++)?
-
Re: Any design pattern for this?
Debajit Adhikary <debajit1> wrote:
> > Use the State Pattern.
> >
> > interface SubA { }
> >
> > class A {
> > private SubA subA;
> > public A() {
> > if ( environmentVariable == 1 )
> > subA = new A1();
> > else
> > subA = new A2();
> > }
> >
> > // for all other methods, for example:
> > public void foo() {
> > subA.foo();
> > }
>
>
> Thanks 
>
> Is it possible to delegate all the method calls to subA automatically?
> I mean.. instead of having to manually write out all the methods
> delegated (like foo()) - is it possible to have all such methods
> "generated" automatically in some way (lets say in Java or C++)?
In C++, its easer to write the delegates than to put in the
infrastructure to do the delegates automatically. In any case, if you
have so many methods that it seems a pain to write the delegates, then
the class is probably too big in the first place. (a God class.)
-
Re: Any design pattern for this?
On Jul 19, 11:25 am, Debajit Adhikary <debaj...> wrote:
> I have a class, say A
> (the following examples are in Java)
>
> public class A
> {}
>
> (I have a lot of code which already uses this class.)
>
> Now, it turns out, I need my application to behave in two different
> ways depending on the value of an environment variable.
>
> I'd like to refactor A into two subclasses: A1 and A2, and would like
> to dynamically create objects of either A1 or A2, depending on the
> value of the environment variable.
>
> I could use some kind of Factory here, but I really wouldn't like to
> break the older code (as I would have to replace all calls like "A a =
> new A()" to "A a = AFactory.getInstance().create()" or something on
> those lines).
>
> How could I best create objects of subclasses A1 or A2 dynamically, at
> the same time not breaking the existing code which uses class A?
This might be a good fit for dependency injection. Take a look at
Fowler's article: http://www.martinfowler.com/articles/injection.html.
There are already open source Java IoC containers that you can
download and use.
David
http://www.traceback.org
Similar Threads
-
By Application Development in forum c++
Replies: 1
Last Post: 08-30-2007, 11:10 PM
-
By Application Development in forum CSharp
Replies: 1
Last Post: 08-22-2007, 06:58 AM
-
By Application Development in forum Object
Replies: 0
Last Post: 07-19-2007, 10:38 AM
-
By Application Development in forum c++
Replies: 2
Last Post: 06-12-2007, 03:52 PM
-
By Application Development in forum Software-Eng
Replies: 3
Last Post: 12-09-2005, 11:35 PM