Results 1 to 7 of 7
  1. #1
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default dynamic inherence

    I have class called GeneralLine that recive String (line with several fields and build itself using this string
    for example - if the constructor get the following strign argument
    Java Code:
    1 text1 "My name" Itai
    then the fields key is set to 1 , field "label is set to "My name" ext..

    However - I want to add special feature
    if getting this string
    Java Code:
    12  import 1
    then the line 12 imports the properties of another line with key 1
    I want to make it subclass - with differnet properites - for example refernce field for the imported GeneralLine and method that update the subclass accordingly.

    The problem is - I dont know which type of class it is- until the string is read within the constructor itself.
    GeneralLine g1 = new GeneralLine(string1);
    What do you advise ?

    It is not just state or addtional method that I can solve with simple if-else
    some of the major method will be sifferent in the subclass
    for example
    GeneralLine.getName -> return field name
    subclass.getName -> return refernceToImport.getname

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    This calls for a design pattern called a "Factory". A factory class exists only to generate instances of other classes.

    So, instead of instantiating your base class directly, you would create a factory class with a method.

    static Base newBase(String parm) {
    Base instance = null;
    // Create the proper class, depending on what is in parm
    return instance;
    }

    Most factory classes require you to instantiate the factory class, but that is only necessary if the factory class itself has to do setup before it can be used. Using a static method makes the factory class much simpler.

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    It sounds like Steve has answered you question, but I would add that it is better to have all the options implement a common interface instead of extending a common base class.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by toadaly View Post
    It sounds like Steve has answered you question, but I would add that it is better to have all the options implement a common interface instead of extending a common base class.
    Not necessarily. If the Classes will be expected to carry out (lets grab a number out of thin air) 20 actions (methods), and 15 of these will have the same implementation details for all (or even just most) of them, then why implement all 20 methods in every Class, when you would, by extending a base/abstract, class only have to implement the 5 that are not the same in the extending classes?

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I agree with Toadaly and Masijade.

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by masijade View Post
    If the Classes will be expected to carry out (lets grab a number out of thin air) 20 actions (methods), and 15 of these will have the same implementation details for all (or even just most) of them, then why implement all 20 methods in every Class...?
    Well sure if that's the case. Interfaces are preferred over extensions when all else is equal, but if all else isn't equal, then it makes no sense to avoid extension.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by toadaly View Post
    Well sure if that's the case. Interfaces are preferred over extensions when all else is equal, but if all else isn't equal, then it makes no sense to avoid extension.
    Often times, the best solution is to create an interface and an abstract class that implements it, leaving all options open.

Similar Threads

  1. Dynamic Table using JSP
    By banie in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-27-2008, 03:13 AM
  2. Dynamic JTable
    By eftenpuften in forum AWT / Swing
    Replies: 0
    Last Post: 07-24-2008, 03:41 PM
  3. Dynamic Binding
    By javarishi in forum New To Java
    Replies: 3
    Last Post: 04-09-2008, 11:17 AM
  4. Dynamic DecimalFormatter
    By felixtfelix in forum New To Java
    Replies: 0
    Last Post: 03-17-2008, 04:16 PM
  5. dynamic update in swt
    By sandor in forum SWT / JFace
    Replies: 0
    Last Post: 05-14-2007, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •