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 ...

+ Reply to Thread
Results 1 to 5 of 5

Any design pattern for this?

  1. Default 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?


  2. Default 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();
    }
    }

  3. Default 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++)?


  4. Default 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.)

  5. Default 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


+ Reply to Thread

Similar Threads

  1. Replies: 1
    Last Post: 08-30-2007, 11:10 PM
  2. Replies: 1
    Last Post: 08-22-2007, 06:58 AM
  3. Any design pattern for this?
    By Application Development in forum Object
    Replies: 0
    Last Post: 07-19-2007, 10:38 AM
  4. class design/ design pattern question
    By Application Development in forum c++
    Replies: 2
    Last Post: 06-12-2007, 03:52 PM
  5. Design Patterns - Which design pattern to use?
    By Application Development in forum Software-Eng
    Replies: 3
    Last Post: 12-09-2005, 11:35 PM