The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

dakbari (34) [Avatar] Offline
#1
Hi Chris,

I like to know your thoughts about the creation of frameworks and the reuse of framework components in an J2EE environmet. You mention the word framework a lot but not one time in sense of building your own framework.

I wonder why? Isn't a framework a good thing? I see a framework as a semi-finished product which needs some domain or customer specific extras to become the final product or application. The framework we created at our company is based on one POJO domain model and a number of components (modules containing delegates, services, facade etc.) all referencing to the one and only one POJO model (domain model).

The problem with using this framework come when we want to realize our customer specific solutions with it. In general you can say that we need 20% of extra domain or customer specific stuff on top of our framework to satisfy our customer requirements. However programmers don't know how to use the framework the right way. And it always ends up in a mess when it comes to reuse the framework or its components.

What programmers tend to do, is to extend the domain model and reinvent the framework components in their own project environments. Since we work in an J2EE environment our components follow J2EE. Maybe the technology forces programmers at first to take this approach. But I don't think that this is the right way our frameworks should be used and I think for another approach but don't know exactly how smilie

Interestingly I also don't find solutions that tackle this problem.

What are your thoughts? Do you have some best practices or patterns one should use here.

Any hints are welcome smilie

Regards,
Darya
ceracm (113) [Avatar] Offline
#2
Re: Framework as semi-finished product and component reuse
Darya,

You ask some interesting questions. Here are a few random thoughts about frameworks:

1. I like the idea of developing your own frameworks provided that they really do help.

2. The best way to develop a framework is to first develop multiple applications, then identify commonalities between applications and finally create the framework. i.e. don't create the framework and then try to use it. This paper describes some good ideas: http://st-www.cs.uiuc.edu/~droberts/evolve.html

3. It sounds like the programmers don't know how to use the framework. Perhaps this is a documentation issue? Ralph Johnson wrote a paper about documenting frameworks using patterns: ftp://st.cs.uiuc.edu/pub/patterns/papers/documenting-frameworks.ps (I don't know of a PDF)

In the 1990s, quite a few interesting papers were written about frameworks. The above links were just a couple. You might want to google for work by Ralph Johnson, Doug Schmidt, and others.

I hope this helps.

Chris
dakbari (34) [Avatar] Offline
#3
Re: Framework as semi-finished product and component reuse
Hi Chris,



I am not sure whether in case of J2EE the theory from the 90's still holds or not. Could you open the .ps document? I couldn't download that one smilie



To make the whole thing more tangible smilie , let's discuss the topic further with an example framework. I've choosen a scenario which is described in Core J2EE Patterns at page 368 for an order processing system.



View the following class diagram. I want to use this framework in my example application.







I still need to find a little scenario for a customer specific application where I don't change the framework code but add some customer specific stuff. If you in the meantime have some scenario in mind feel free and adapt the application with above framework smilie. Maybe you have an idea.



Is there a pattern you have in mind when plugging the framework into the application?



Regards,
Darya

Message was edited by:
dakbari

Message was edited by:
dakbari
dakbari (34) [Avatar] Offline
#4
A Scenario - What is a elegant solution?
Now, having the framework visualized I'm going to use it. There are multiple scenarios thinkable and I start with a first one.



Think about you want to use the framework in your specific application. The only difference is that in your application the Order class needs an addidtional attribute let's say for a priority.



Think about that you can not simply extend the framework Order class and add priority, because that would mean that you can't use the component anymore.



What would be a elegant solution to it. The goal is to reuse the component based framework at best.



Any ideas smilie ?



Regards,



Darya
ceracm (113) [Avatar] Offline
#5
Re: A Scenario - What is a elegant solution?
hmm...

Why do you think you could not subclass Order?
Presumably there would be a factory responsible for instantiating the appropriate subclass of Order.
And, you would have to change the O/R mapping to persist the subclass.

When you say "you can't use the component anymore" - what do you mean by component? The entire Order subsystem? You could certainly reuse the code.

Please elaborate.

Chris
dakbari (34) [Avatar] Offline
#6
Re: A Scenario - What is a elegant solution?
Hi Chris,



With component I mean the whole left side in above class diagram. The right side in the same class diagramm shows the domain model. Together both sides manifest the framework which I want to use in my customer (domain specific) application. Both sides reside in seperate archive files.



My goal is to reuse the whole component in my customer project. A constraint is that I can't change the framework when I am in a customer project, it's only possible as a rare exception.



The domain model should also be used. However all customer specific things should happen outside the framework's model and placed into the customer project. When I subclass Order I don't see how I can use this subclass by reusing the component. The framework's OrderSessionFacade is a stateless session EJB and OrderAppService points to Order and not the subclass.



With my customer project I'm outside of the class diagram. So anything that want to connect to the class diagram happens from outside. I fear the only thing I can reuse is the model and not the component. The worst case is to create a similar component again as already exists in the framework in my customer project which wouldn't be reuse code but duplicate code smilie .



It's all possible that I miss something here.



Regards,



Darya
simbo1905 (30) [Avatar] Offline
#7
Re: A Scenario - What is a elegant solution?
In my applications the OrderService will see any subclass of Order as an will handle it correctly due by OO polymorphism. The application might have passed a CustomOrder. So long as there is a hibernate mapping file (or JDC or iBatis or EJB3 equivalent) then the extra attributes will get saved into the persistence store (the database).

Then I would look to design my Services to have protected methods so that the customer can extend its logic. He can subclass my Service and in his methods to something before and after calling the super method (my original method). My method just handles the object polymorphically and hibernate will save all of it if you configure with the correct mapping file. I just have to ensure that my framework is all compiled against interfaces and is configured by Spring to allow the client to change the spring configure xml to ensure the use of his subclasses not my base class. That is the key win with Spring - it allows for folks to change the implementation of an interface declaratively.

I am not saying that it is easy to design classes to be subclasses well. In fact the book Effective Java http://java.sun.com/docs/books/effective/ shows how many problems you can get yourself into try to allow for re-use. As designing for sub-classing is hard another approach is to explicitly add well documented places where uses can extend a framework. For example think about Servlet Filters. This shows that the designers were anticipating where folks might want to do something different.

Another great example are Hibernate Interceptors. This is an abstract class where you define methods that the Hibernate Session calls at several very important steps when Hibernate is going something important passing the data that it is is working on. You just subclass their 'no nothing' interceptor and overload the methods that to add bespoke behaviour where you need it. Then you specific that your interceptor is passed to the constructor of the hibernate session (you can do this with Spring). When Hibernate is going its work it will keep calling your Interceptor code at the appropriate point in time and you get to do something bespoke.

You can do all sorts of weird and wonderful things with Hibernate Interceptors. One example that I have seen that impressed me was to use reflection in the Save method interceptor to find unmapped fields in the object that is to be saved and marshal the attribute values as XML and set it on a String filed in the base class mapped to a clob column. Then on the Load interceptor method to the reverse to parse the XML from the string reflect to call the setter methods of the unmapped fields. Then people that are sub-classing your domain objects don't have to provide hibernate mappings as you will pack and unpack their subclass fields to XML in the database.

My thoughts are that when wanting to write re-usable framework the trick is to copy all of the patters of the successful framework that use OO principles, practices and patterns.
dakbari (34) [Avatar] Offline
#8
Re: A Scenario - What is a elegant solution?
> In my applications the OrderService will see any
> subclass of Order as an will handle it correctly due
> by OO polymorphism. The application might have passed
> a CustomOrder. So long as there is a hibernate
> mapping file (or JDC or iBatis or EJB3 equivalent)
> then the extra attributes will get saved into the
> persistence store (the database).


This seems to go in hand with what Chris says, to use a factory and the right Hibernate mapping smilie . And I think the current framework has to be broken up and adapted with such a factory which in case of Order would be the OrderAppService class.

> Then I would look to design my Services to have
> protected methods so that the customer can extend its
> logic. He can subclass my Service and in his methods
> to something before and after calling the super
> method (my original method). My method just handles
> the object polymorphically and hibernate will save
> all of it if you configure with the correct mapping
> file. I just have to ensure that my framework is all
> compiled against interfaces and is configured by
> Spring to allow the client to change the spring
> configure xml to ensure the use of his subclasses not
> my base class. That is the key win with Spring - it
> allows for folks to change the implementation of an
> interface declaratively.


Spring is not an option here smilie because there has already been made a big investment to the current framework which allows only minor changes like adding factories for all POJOs but not major changes like implementing Spring in the aftermath.

> My thoughts are that when wanting to write re-usable
> framework the trick is to copy all of the patters of
> the successful framework that use OO principles,
> practices and patterns.


Our framework already is a result of several projects combined. Nevertheless as you see patterns like the factory fall short.

What would you suggest when there should be a further extension like say cancelOrder to be added to the application (NOT to the framework)? Would you add an additional customer specific EJB in addition to the framework's OrderSystemFacade EJB ?

Message was edited by:
dakbari
dakbari (34) [Avatar] Offline
#9
Re: A Scenario - What is a elegant solution?
Hi Chris,

if a factory is responsible for instantiating subclasses of Order (which I think is the right way) and such factory is part of the framework then how does the factory know in advance which subclass it has to instantiate.

The framework with its factory is already there when a new subclass of Order comes into play. Hence how can the factory take the subclass in?

Regards,
Darya
ceracm (113) [Avatar] Offline
#10
Re: A Scenario - What is a elegant solution?
A few different options come to mind (for a non-Spring application):

1. The factory reads the class name of the Order to instantiate from a properties file.
2. The class name of the factory is read from a properties file
3. Some EJB specific mechanism, e.g. specify the class name in the EJB environment/deployment descriptor

In general, you should use GOF patterns such as Strategy, Template Method, Factory etc to make the component extensible. The app reads the concrete class names from some properties file etc.

I hope this helps.

Chris