Ritesh Pareek wrote:
Want more on marker
Interfaces that do not define any methods (such as Serializable, Cloneable) can still be used in
instanceof checks. For example, Object's protected
clone() method will perform such a check, and throw and Exception if it's invoked on a method that does not implement Cloneable (similar logic can be found in the serialization mechanism). By implementing the marker interface, the developer claims that he has done his homework (made sure Object's
clone() is safe to invoke, or that all non-transient fields the class are Serializable).
Ritesh Pareek wrote:
@All
Please consider following architecture for model layer:
Now while using we are doing it like this
here calling of manager method is a kind of proxy calling.
If you can explain me the purpose and concept of above design(specially the role of interfaces here and putting an extra layer of manager)
The returned POJO may be wrapped in a dynamic proxy that can record changes to its properties via setter methods, so SQL to save it in the database can be generated based on those calls. Therefore, the object returned by
myManagerObj.getDaoMethodViaManager(); may not be an instance of the class but a proxy using the same interface.
I think this is pretty much useless in this form. Interaces are nice to isolate the contract from the implementation. With dependency injection, it would be possible to plug in any kind of manager (e.g. for production you have the real class, for
testing, you can have something based on an in-memory collection). However, wiring it up like this means that although you use the interface, you fix the reference to an actual class. Either of these would be better in that aspect:
or
Dependency injection frameworks will support both; when testing, you don't need to rely on the framework, you can simply instantiate your dummy manager and pass it to your object via the constructor or the setter.
You may sometimes still use the interface to make your code easier to change:
Here FooUser does not depend on what exact type of Collection you use in Foo. If you decide you want your names ordered alphabetically instead of keeping insertion order, you could simply replace
with
and keep everything else unchanged. Consider this with exposing ArrayList in Foo's interface, and also expecting an ArrayList in FooUser - you'd have to modify Foo at several places, and modify FooUser, too.