
Hi all, In Java, a design pattern is defined by an Interface, like: public interface Foo { ... public void foo (); ... } public interface Bar { ... public void bar (); ... } All methods in the interface are abstract (virtual in C++) and all attributes are static and global. You can then implement this interface on any object. 1 object can implement more then one interface but cannot inherit of more then 1 class, as opposed to C++. Default inheritance is the Object class. That means you have to use the aggregation paradigm with interface to archives multi-inheritance. public class MyFoo implements Foo, Bar { public void foo () { System.out.println ("Hello SAGA foo[l] ;p"); } public void bar () { System.out.println ("A bar of black chocolate!"); } } public class AndreFoo implements Foo { public void foo () { System.out.println ("Hello Andre!"); } } // Code Foo foo= new MyFoo(); foo.foo(); foo= new AndreFoo(); foo.foo(); If your program have different implementations of the same Foo interface that are supposed to do the same (API contract), that is possible, see Hidemoto's example. Like the XML parsers you can implement a SPI (Service Provider Interface) to access the different implementations without having to care about the full class names and locations (packages). Some default SPI are directly endorsed by the Java runtime, like the XML stuff. I did not check the implementation yet, but it should be implicit that the implementation uses design patterns otherwise it will be a hard job to switch between implementations. Best regards, Pascal Kleijer Andre Merzky wrote:
Hi Hidemote, Graeme,
please excuse my ignorance - as said before, I am Java agnostic, and my question might be off target...
Hidemote mentioned the use of the factory pattern at GGF already, and other people I know which do Java issued similar statements.
How does that actually compare to other languages? E.g. in C/C++, you can usually link your code against a specific _API_ in a shared library - as long as a library implements the same API, there is no need to recompile (just exchange library).
Also, in C/C++ it is possible to implement an API with plugins: so you create a thin API layer, which forwards to dunamically loaded plugins, which can then provide different implementations of the API calls.
The latter way is what we actually do in our C++ implementation (plugins are called adaptors though).
The difference I see is actually that in both cases, the fact that the API is implemented by different vendors is completely hidden on API level, w/o the need for any additional patterns like factories.
So, is there a difference between these approaches? And if not: what is the advantage of the factory approach?
Thanks,
Andre.
Quoting [Hidemoto Nakada] (May 17 2006):
All,
I have a question on the Java Binding. Does the current Java binding include explicit factory notion?
Factory mechanism is important to - reuse a source code with several implementation of the API without modification of the code. - leverage several implementation simultaneously from one client code.
One good example of factory design can be found in the Dom parser definition; org.w3c.dom.DomImplementation. When you create a Dom document
- get a DomImplementation from org.w3c.dom.bootstrap.DomImplementationRegistory - then create a Document object using DomImplementation.
This mechanism might be too complicated for SAGA, but I think some flavor of this is required.
-hidemoto