Home arrow static arrow Java Programming [Archive] - Design Pattern
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Design Pattern
This topic has 13 replies on 1 page.

Posts:26
Registered: 11/12/03
Design Pattern  
Jul 19, 2004 3:49 AM



 
I have a group of classes all having the same 2 methods m1 and m2. m1 invokes m2. Both m1 and m2 are static methods (I want to invoke m1 without instantiating the class). m1 is exactly the same in all the classes. m2 is different in different classes, but signature is same.

(1) I want to avoid replicating m1 in all the classes.
(2) I don't want the classes to lie disparate, as if there is no connection between them.

Because the methods are static, I am not able to use either abstract class or interface.

Could you please suggest a good design.

Thanks & Regards.
 

Posts:2,830
Registered: 9/1/03
Re: Design Pattern  
Jul 19, 2004 3:57 AM (reply 1 of 13)



 
Well, it seems a bit strange to require a static method in each class? What
is it for ?

You suggest that m1 is the same in all classes - then what is it in all
classes for ? Does it need to be aware of some static variable within the
class that will be different? - In that case, there is no option but to avoid
duplicating them. It is probably better to avoid that, however, and make it
such that the method can either be non-static, or static but does not need
to be aware of it's location and hence can live inside a utility type class.

What exactly are you trying to do ... ?
 

Posts:18,384
Registered: 21.03.00
Re: Design Pattern  
Jul 19, 2004 3:57 AM (reply 2 of 13)



 
Hi,

Some thing is weird in your design. Why are m1 and m2 abstract? Why do m1 need to call m2?

/Kaj
 

Posts:18,384
Registered: 21.03.00
Re: Design Pattern  
Jul 19, 2004 3:58 AM (reply 3 of 13)



 
Sorry, I should have written, why are they static? What do the classes have in common?

/Kaj
 

Posts:26
Registered: 11/12/03
Re: Design Pattern  
Jul 19, 2004 4:14 AM (reply 4 of 13)



 
This has been designed so for performance reasons. I could have managed the entire functionality with a single utility class.
Functionality Required: For different locales, I have different sets of services and versions. For a given locale parameters and a service name, appropriate version of the service has to be invoked.

(1) Each class in the group corresponds to a different locale setting.
(2) m2 just gives the version no., for the given service (Database retrieval is not permitted and hence the mapping is present in the code).
(3) m1 (input argument is service name) invokes m2 and then invoke a utility class (whose functionality is to use reflection API and invoke the appropriate service).

Regards.
 

Posts:319
Registered: 11/8/00
Re: Design Pattern  
Jul 19, 2004 4:17 AM (reply 5 of 13)



 
Not really sure what your after but maybe somethong like this

// myClass public class MyClass extends something {    public void m1()    {        //Do Something        m2();    }     public abstract void m2();}  // myOtherClass's public class myOtherClass extends MyClass{    public void m2()    {        //Do Something    }}

You would wite as many versions of myOtherClass as required.
 

Posts:26
Registered: 11/12/03
Re: Design Pattern  
Jul 19, 2004 4:19 AM (reply 6 of 13)



 
As per my requirement, I need to invoke the method without instantiating an object i.e. the methods need to be static.
 

Posts:2,909
Registered: 13.8.2003
Re: Design Pattern  
Jul 19, 2004 4:23 AM (reply 7 of 13)



 
This has been designed so for performance reasons.

Performance reasons don't warrant making methods static. A static method is barely faster than an instance method, so if you can avoid bad design by making them instance methods I recommend doing so.
 

Posts:319
Registered: 11/8/00
Re: Design Pattern  
Jul 19, 2004 4:24 AM (reply 8 of 13)



 
// myClass public class MyClass extends something {    public static void m1()    {        //Do Something        m2();    }     public abstract void m2();}  // myOtherClass's public class myOtherClass extends MyClass{    public static void m2()    {        //Do Something    }}


And you would invoke it like
MyOtherClassOne.m1();MyOtherClassTwo.m1();MyOtherClassThree.m1();MyOtherClassFour.m1();
 

Posts:26
Registered: 11/12/03
Re: Design Pattern  
Jul 19, 2004 4:27 AM (reply 9 of 13)



 
This will not compile as static m1 can invoke only "static" m2. If you declare m2 as static, then it cannot be abstract. Also the subclasses cannot override a static m2!!!
 

Posts:319
Registered: 11/8/00
Re: Design Pattern  
Jul 19, 2004 4:32 AM (reply 10 of 13)



 
Sorry, am half asleep, i think i'll have an early lunch and catch 40 winks ;-)
 

Posts:18,384
Registered: 21.03.00
Re: Design Pattern  
Jul 19, 2004 5:00 AM (reply 11 of 13)



 
Hi,

You should really re-think your design. But here is some code that solves your problem, but I'm not proud of it.

public class Test { 	public static void main(String[] args) throws Exception {		Util.m1(A.class);		Util.m1(B.class);	}} class Util {		public static void m1(Class theClass) {		try {			Method m2 = theClass.getMethod("m2", null);			m2.invoke(null, null);		} catch (Exception e) {			e.printStackTrace();		}	}		} class A  { 	public static void m2() {		System.out.println("A");	}} class B { 	public static void m2() {		System.out.println("B");	} }


/Kaj
 

Posts:26
Registered: 11/12/03
Re: Design Pattern  
Jul 19, 2004 5:35 AM (reply 12 of 13)



 
Impressed by the solution. But as advised, let me rethink the design. Thanks a lot.
 

Posts:18,384
Registered: 21.03.00
Re: Design Pattern  
Jul 19, 2004 5:37 AM (reply 13 of 13)



 
Thanks for the dukes :)

/Kaj
 
This topic has 13 replies on 1 page.