Article View: borland.public.jbuilder.java.api
Article #1606Re: Making plug in modules (newbe to Java)
From: John Baker
Date: Mon, 05 Nov 2001 09:47
Date: Mon, 05 Nov 2001 09:47
82 lines
3709 bytes
3709 bytes
Here's a snippet from a previous post of mine that answers your question too
I think.
Think of a module as some kind of interface with a defined life cycle... For
some this may be more simple than others. There are many more advanced
design patterns than this by this should get you started. After trying this
this think about using factories (classes that create classes) to create the
implementations rather directly in the host app... Also think about reading
a design patterns book such as Gamma et al.
More often that not it is better to use an interface as the basis of some
module and have an adapter class (default implementation for simplicity like
MouseAdapter etc) that you extend from. This leaves you the option later to
not extend from the base class if necessary (this often happens when a
program evolves and requirements change). An example if you like..
// The basic skeletal definition of a module
public interface MyModule
{
public void init();
public void open();
public void close();
public void destroy();
}
// A convenient adapter class to extend to avoid rewriting every method
public class MyModuleDefaultImpl implements MyModule
{
public void init() { /* noop - override if req */ }
public void open() { /* noop - override if req */ }
public void close() { /* noop - override if req */ }
public void destroy() { /* noop - override if req */ }
}
// This
public class MyModuleImplA extends MyModuleDefaultImpl
{
public void init() { /* override to do something useful */ }
}
public class HostApp
{
public static void main(String[] args)
{
try
{
// Use a dynamically loaded and instantiated module at runtime
// Notice we only ever refer to the interface not the actual
implementation at runtime
MyModule m = (MyModule)Class.forName("MyModuleImplA").newInstance();
m.init();
m.open(); // etc.... depending on reuired lifecycle behaviour
}
catch (Exception x)
{
x.printStackTrace();
}
}
}
J. Deas <jdeas@jadsystems.com> wrote in message news:3be61f09_1@dnews...
> Is there a standard way to make a "plugin module" in Java?
> I have been writing an application were different modules will need
> to be written after the main software is done and distrubuted like
> plugin modules are done in web browsers.
> I am currently using a local socket but was wondering if I could use
> 'interfaces' to link classes written at a later date.
> It would seem to be possible if I knew how to reference the plugin's
class.
>
> Can anyone point me in the proper direction? Is writing an interface
> a dead end for this application? If so, is a local socket the best way?
>
> Regards,
> J. Deas
>
>
>
Technical Details
Message-ID:
<3be65d53_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnewsReferences:
<3be61f09_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,7c97ac1429a802dc X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2001-11-05 01:38:41 PST From: "John Baker" <jbaker@SoftComms.com> Newsgroups: borland.public.jbuilder.java.api References: <3be61f09_1@dnews> Subject: Re: Making plug in modules (newbe to Java) Date: Mon, 5 Nov 2001 09:47:22 -0000 Lines: 84 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2314.1300 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 NNTP-Posting-Host: 217.135.248.73 Message-ID: <3be65d53_1@dnews> X-Trace: dnews 1004952915 217.135.248.73 (5 Nov 2001 01:35:15 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews