Static And Singletons Within A Echo2 Environment

Hello everyone,

may be someone can give me some short explanaition or even some nice "best practices". I am currently working on my first echo2 application in our company. I was not involved the first steps of the application, but now I see a lot of ugly code issues, which I would like to resolve.

One of our main issues, is that we have framed our application with different splitpanes. The class holding all this is called "MainScreen" and is doing all the basic stuff of the application. Our goal with the application is also to have alot of different modules, which are all own applications, but they all have the same layout and look-n-feel. Here is also a defined status-bar, where all messages to the user a shown like "entity saved complete". "20 entities found". etc.

In our application frame we have a tab-pane, in one of our splitpanes. This one is called the workbench. When ever a user interacts with a entity in the workbench, he might get a new WindowPane for viewing and editing that entity. The windows allways need to interact with the MainScreen. The allways need to call methods from there, but for every application session within a browser, there is always only one MainScreen. Suddenly in the current step of devellopment, nearly all gui classes need to reference to the MainScreen. This leads to the very ugly way, that every constructor needs the MainScreen. This is bad. I know. I want to change.

My thoughts were to make Singleton-MainScreen.

But today I checked with a second Singleton class for holding current user information, and I see, that every field in a singleton class is static for all current sessions. so in addition every static field is static for all sessions.

while:
WebRenderServlet.getActiveConnection().getUserInstance().getSession()
is not static, even if WebRenderServlet.getActiveConnection() IS a static method.

why? and what would be the best way to have the chance that all gui classes can interact with the mainscreen without need to know a field reference to the MainScreen.

Best Regards.
Alexander

Easiest is probably to maintain a reference to MainWindow in your ApplicationInstance subclass. You can always get a reference to the current ApplicationInstance through the ApplicationInstance.getActive method.

rakesh wrote:

Easiest is probably to maintain a reference to MainWindow in your ApplicationInstance subclass. You can always get a reference to the current ApplicationInstance through the ApplicationInstance.getActive method.

thank you rakesh, I will give it a try.

What about Factory pattern?

Kestas wrote:

What about Factory pattern?

can you explain, what you mean with that? a factory will create a new instance. but I do not need a new instance. i need one instance each browser session. so default static objects are for all active sessions.

Alexander Steinbeisz wrote:

can you explain, what you mean with that? a factory will create a new instance. but I do not need a new instance. i need one instance each browser session. so default static objects are for all active sessions.

your all child forms/workbenchs should be created in main app layer, which should fill some providers.
Example:

public interface ITasksEvents {
public AbstractForm runForm(Class<? extends AbstractForm> clazz);
public void abortForm();
public AbstractMessage runMsg(Class<? extends AbstractMessage> clazz, IObjectSelectEvents provider);
}

public interface IFormActionImplementer {
public void actionRefresh();
public void actionSave();
public void actionEdit();
public void actionNew();
public void actionCopy();
public void actionDelete();
public void actionHistory();
public void actionPrint();
}

so when some child form wants to run something, it call runForm method,
which really is implemented in main app, which creates new instance, fullfills with all providers. Toolbar which also like child form call IFormActionImplementer implementer, and each form should implent this intefaces and so on. At the need no body need main app , even more specific child form can have 2 forms inside and work like main app for them and so on ...