Results 1 to 7 of 7
Thread: dynamic inherence
- 01-29-2009, 09:53 AM #1
Member
- Join Date
- Dec 2008
- Posts
- 99
- Rep Power
- 0
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
then the fields key is set to 1 , field "label is set to "My name" ext..Java Code:1 text1 "My name" Itai
However - I want to add special feature
if getting this string
then the line 12 imports the properties of another line with key 1Java Code:12 import 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.
What do you advise ?GeneralLine g1 = new GeneralLine(string1);
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
- 02-05-2009, 04:26 PM #2
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.
- 02-06-2009, 08:03 AM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
- 02-06-2009, 12:44 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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?
- 02-07-2009, 12:36 AM #5
I agree with Toadaly and Masijade.
- 02-07-2009, 06:50 AM #6
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
- 02-07-2009, 01:05 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Similar Threads
-
Dynamic Table using JSP
By banie in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 08-27-2008, 03:13 AM -
Dynamic JTable
By eftenpuften in forum AWT / SwingReplies: 0Last Post: 07-24-2008, 03:41 PM -
Dynamic Binding
By javarishi in forum New To JavaReplies: 3Last Post: 04-09-2008, 11:17 AM -
Dynamic DecimalFormatter
By felixtfelix in forum New To JavaReplies: 0Last Post: 03-17-2008, 04:16 PM -
dynamic update in swt
By sandor in forum SWT / JFaceReplies: 0Last Post: 05-14-2007, 08:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks