开发者

Java interface implementation class file

开发者 https://www.devze.com 2023-04-13 01:11 出处:网络
I have a protocol stack implementation, where each layer receives the below layer in the constructor in order to communicate with them, like:

I have a protocol stack implementation, where each layer receives the below layer in the constructor in order to communicate with them, like:

ApplicationLayer app = 
       new ApplicationLayer(
              new DataLinkLayer(
                     new PhysicalLayer()
));

What I need here it's to control the classes of the instatiated objects, in order to to change the layers type just by changing a file (not a .java one, something like .xml). One of the possible usages it's to implement logger layers between each layer, like:

ApplicationLayer app = 
           new ApplicationLayer( 
                  new AppLogLayer( 
                        new DataLinkLayer(
                              new DataLinkLogLayer(
                                    new PhysicalLayer()
))));

With that, my source code keeps the same in production (where we don't need log) and in development (where I need logging), just by changing an external (to the .jar) file.

Is there any framework to do that? Preferentially with开发者_StackOverflow社区 Eclipse integration.


Sounds like you want a dependency injection/inversion of control library. Spring and Guice are the canonical examples, although for something this simple you might just spin your own.


You could have a look at dependency injection using the Google Guice library.


SLF4J you change de log method at deployment only changing a jar.

See: http://www.slf4j.org/ maybe can helps you.


If you are only looking for a way to enable logging in development times versus no logging in production time, you might rather use the log level facilities of the logging framework or facilitate the much unused assert functionality in Java (>1.4). That would be much cleaner than alter your sourcecode for different environments.

A word on assertions: you can enable/disable the assertion facilities in the JVM by passing an argument to your JVM. The assert code than gets either executed (if the flag is on) or not if the flag is of. See http://java.sun.com/developer/technicalArticles/JavaLP/assertions/

The assert practice is recommended and described in Robert Simmons "Hardcore Java" book, if i recall correctly.

0

精彩评论

暂无评论...
验证码 换一张
取 消