Java design conundrum

I have a design conundrum that I have been wrestling with for a while and have not been able to come up with a viable solution. So I decided to post it here in hopes someone can recommend a solution. Here it goes;
I have an applet with many packages that make it up. I have a status window that is always displayed as part of the applet. I use this window for Objects to write their status to. Currently I am using a static method to write to this window. This works great in that any object in any package that wants to write to the window merely has to invoke the static method to write it's status. The problem arises if I then spawn another applet. The new applet's status window displays the text that was in the previous applets status window. I looked at event/listener, observer/observable et al. The problem is that a object in another package does not have access to the window to add itself as a listener. If I try and arrange it so the object that contains the status window gets passed around the code will get very ugly and very tightly coupled, something I want to avoid. I am trying to do this without using static's or singletons. Any ideas?

This works great in that any object in any packagethat wants to write to
the window merely has to invoke the static methodto
write it's status. Any object in any package that wants to write to
object ABC merely has to invoke the method to write
its status. Sure, it has to obtain a reference to
this object first, but a well-designed program can do
this without compromising anything. What's your
point?
I am trying to come up with a way to rewrite the classes so that static's are not used. However the behavior is such that I need away to write to a single point from any class that wants to. Example the applets window has a split pane with the bottom half displaying status(text) from other classes. During the prototype I just used a static to write the text. I want to eliminate the static's so multiple invocations of the applet are separate, even though they are running in the same JVM. I have not had much luck comeing up with a design and/or way to implement it. That's why I hoping someone here can point me in the right direction or slap me upside the head so I see the really simple solution.
The problem arises if I then spawn
another applet. The new applet's status window
displays the text that was in the previous applets
status window. Definition of static variables. Use instance
variables, which belong to an object rather than a
class.
Yep, I trying to come up with that.
>
I looked at event/listener, observer/observable etal. The problem is that a
object in another package does not have access tothe window to add
itself as a listener. If I try and arrange it sothe object that contains the
status window gets passed around the code will getvery ugly and very
tightly coupled, something I want to avoid. I amtrying to do this without
using static's or singletons. Any ideas?Your code is already tightly coupled. Static calls
are the worst possible dependency for the client
class -- there is no way to abstract it. So if you
are going to make a client class coupled with a
static class, that static class should be pretty
reusable and project non-specific.
Yes, I am trying to un-couple it. I agree static's are bad but they are soooo easy. At least initially then of course, yuck.
Instead of using the status window class staticly,
the objects in your model layer should be passed an
abstraction of the status window view -- be it an
interface (preferred), abstract class, high-level
concrete class, etc. In order to do this you will
have to make objects out of your status window rather
than utilizing it staticly, since you cannot abstract
static-based classes.That is what I hope to be able to come up with but so far no luck.

Similar Messages

  • UML and Java Design Patters

    I am new to UML and Java Design Patterns
    Please suggest some good books for UML and Java Design Patterns.
    Venu

    I am new to UML and Java Design
    Patterns
    Please suggest some good books for UML and Java
    Design Patterns.
    VenuThere are only a few of them:
    http://www.amazon.com/gp/product/0596007124/ref=pd_bxgy_text_b/102-9140088-0344105?%5Fencoding=UTF8
    http://www.amazon.com/gp/product/0130935387/ref=pd_sbs_b_3/102-9140088-0344105?%5Fencoding=UTF8&v=glance&n=283155
    http://www.amazon.com/gp/product/0471227293/ref=pd_sim_b_5/102-9140088-0344105?%5Fencoding=UTF8&v=glance&n=283155
    http://www.amazon.com/gp/product/0201485397/102-9140088-0344105?v=glance&n=283155

  • Software reuse with Java design patterns

    Anyone have any suggestions on how software can be reused with Java design patterns? Any concrete examples?

    Buy the GoF book. Lot's of examples there.

  • Java Design for Testability

    Hi All
    Are there any best practices to make Java code / design project to be more testable.
    Working on a web based (java / jsp) project.
    Please let me know if you have any suggestions or good pointers.
    Regards
    bib

    There are lots of good techniques that can make testing much easier, but the simple one I want to suggest to you is this:
    Shrink the size of your persistant "state" down to the smallest it can be. If it's possible, do not hold any state at all. Testing systems built with a functional paradigm in mind is generally easier because you don't have to consider all the different states you could be in

  • Java design concept explanation

    Hi,
    I need help with java concept to make my design better. I have 2 classes which are for different purpose but perform a common step in between.
    For example: I need to deal with groups and users. The two classes at a point check if users are present in more than one group and if they do they display a message.
    I am confused as to how to fit in this step with out replicating the code. If i make a 3rd class with this step and instatiate this class object in first two will it be constly on resources (as in memoryt)? or is there a better way to represent this situation.
    hope my explanation was fine.
    Thanks in advance.

    Why not just decide which class you want to implement your common tasks in and give the second class visibility to it.
    myBigClass bigClass = new myBigClass();
    mySmallClass smallClass = new mySmallClass(bigClass);Then calls to your shared code can be wrappers in your smallClass that just reference the code in your bigClass;

  • Core Java Design Patterns

    Hi folks,
    I wanna know about design pattern to implement my current project, I need some reference book. if you know the book. please mail me
    [email protected]
    Thanks and regards
    SK,India

    [The GoF book|http://www.amazon.co.uk/gp/product/0201633612/ref=s9sims_c3_at1-rfc_g1?pf_rd_m=A3P5ROKL5A1OLE&pf_rd_s=center-1&pf_rd_r=1JQJWT69HDE1GTFS4EXH&pf_rd_t=101&pf_rd_p=139045791&pf_rd_i=468294]
    [Head First Design Patterns|http://www.amazon.co.uk/Head-First-Design-Patterns/dp/0596007124/ref=pd_sim_b_2]
    [Refactoring to Patterns|http://www.amazon.co.uk/Refactoring-Patterns-Addison-Wesley-Signature-Kerievsky/dp/0321213351/ref=sr_1_1?ie=UTF8&s=books&qid=1216992456&sr=1-1]
    Enjoy

  • Java design book

    Hello,
    I'm looking a good book for Java; what I'm looking for, is not a Desing Pattern book fo Java but a book that shows through many examples how to modelling the objects; I have the Eckel's book and it doesn't help to me since it has not "serious/good" examples. I need examples, more practise....
    Hope this make sense. regards.

    mickey0 wrote:
    a book that shows through many examples how to modelling the objects; I have the Eckel's book and it doesn't help to me since it has not "serious/good" examples. I need examples, more practise....IMHO, this is the best book I've ever read about any computer language.
    It's not exactly a "modelling" book, more of a "best practises" one; but it has plenty of examples (good and bad) on how to write good, safe classes.
    Winston

  • Java Design question

    I'm certain this is not the best forum for my question, but I didn't any other
    that seemed obvious.
    I have a design issue/ OO question. Our system has the very common requirement
    of searching. We have build a session EJB which actually performs the search
    and returns a collection of Value Objects back to the client. My question is
    this: If the search EJB finds no matching records, what is the correct strategy
    for dealing with it?
    1) Throw an application exception, such as "NoDataFound", which the client can
    catch and handle appropriately
    2) Simply return a null collection. In which case, the client will have to check
    for null, and redirect program appropriately.
    3) Simple return type, such as boolean, for the method -- indicating success or
    failure (meaning no records found).
    4) Something entirely different. If so, what?
    Your thoughts on this subject would be greatly appreciated!
    Thanks!
    Brian

    I guess the basic question is how you treat your "Search Mechanism"
    Is it a atomic function or is it integrated in to your business logic in
    anyway...
    I use the following as a rule of thumb:
    there are 2 ways to implement a functionality:
    either as a Procedure or a Function
    Procedures do not return an explicit result to the sender of the
    corresponding message
    -A procedure call corresponds to an instruction
    In which case it makes sense to throw an Exception which has more sematic
    meaning to the business Logic
    AND
    Functions always return a result to the sender of the corresponding message
    -Function calls correspond to expressions
    -The result returned by a function is either a copy of an object, or a
    reference to an object of a class
    In this case it makes sense to return a Empty List and have the client check
    the sematics of the List..
    Hope this helps,
    Naggi
    "Brian Snyder" <[email protected]> wrote in message
    news:[email protected]..
    >
    I'm certain this is not the best forum for my question, but I didn't anyother
    that seemed obvious.
    I have a design issue/ OO question. Our system has the very commonrequirement
    of searching. We have build a session EJB which actually performs thesearch
    and returns a collection of Value Objects back to the client. My questionis
    this: If the search EJB finds no matching records, what is the correctstrategy
    for dealing with it?
    1) Throw an application exception, such as "NoDataFound", which the clientcan
    catch and handle appropriately
    2) Simply return a null collection. In which case, the client will haveto check
    for null, and redirect program appropriately.
    3) Simple return type, such as boolean, for the method -- indicatingsuccess or
    failure (meaning no records found).
    4) Something entirely different. If so, what?
    Your thoughts on this subject would be greatly appreciated!
    Thanks!
    Brian

  • Java design ICQ project.

    Please go to:
    http://jicq.vicp.net
    Download the client:
    http://www.chinafmeili.com/down/jicqsetup.exe
    http://www.chinafmeili.com/down/jicq.zip

    The english version:
    http://www.chinafmeili.com/down/jicqsetup-e.exe
    source code:
    http://www.chinafmeili.com/down/jicq-e.zip

  • Java Design Ideeea'rs

    Ok, I am beginning development of our first proprietary software release for my company this coming week. I want to make it as efficient as possible, not only in terms of the runtime efficiency but also efficient to update the source code. What I would like to know is when developing any gui application how do you separate panes? I use JBuilder to develop the GUI's (I know I don't like it either but my boss wants me to use it). So do you usually make a new class for each pane? For example, if I have a JFrame with a tabbed pane on it would I make a new class for each tabbed pane? Or would I just put everything in one class which contained that tabbed pane? Additionally, would you make a separate class for non gui functionality so that it was kept separate? Is it a good idea to put action listeners in the same class with the gui or make a class file for just actionListeners? Awh... so many questions. Sorry... Please give me any ideas.
    TIA
    Doopsterus

    I really don't like GUI builders - Even that from
    JBuilder. I like to code by hand. There are enough
    nice Layoutmanagers out there (ie JGoodies), and I
    like to have full control. Normally I don't use a new
    class for each panel/pane. Only if it's a logical
    unit. ie. in a program I need to generally insert
    Contact Information. (So zipcode, street, city, tel,
    email, ...) So I created a pane in a class apart. But
    if I have a gui with 3 combo's, 1 jtree and a Jtable
    on different tabs of tabbedpane (which are very
    specific for that job) I won't create different
    classes. At most I would create inner classes. MVC is
    (extremely) nice but I like to implement it also as
    inner classes, not to destroy my package structure
    with 65456 classes for ActionListeners,
    MouseListeners, AbstractTreeModels, .... .I'm right with you... I'm a little far from religious zealotry on the matter--but definitely as a learning tool, I think it would be like using a calculator to learn basic arithmetic in elementary school...then when the calculator is broken, you're on your own, and 9 * 6 = 42 doesn't seem to fix your problem.

  • Design problems [code]

    many times i stumble over a problem, concerning the desing of my code.
    imaging i have a manager class for my game, which holds all of my game objects (space ships, landscape, what the heck) and schedules them for animation etc.
    the main work for the manager will be, to do a fixed task every frame, to move the objects, look for intersection etc. to do so, it will call every object and use it, the the objects themselves doesnt need any communication channel backwards, they only receive commands. but sometimes
    it is necessary that a gameobject sends a request to the manager, e.g.
    the spaceship dies or has expired his time-based dieing animation and wants another ect pp.
    now the solutions i see for this problem:
    the gamemanager asks in every frame for every action, which could occur on an object:
    are you dead, do you want another animation etc.
    i think this is not acceptable.
    or:
    every gameobject gets a reference of the gamemanager. but i think this would destroy the object encapsulation and the good style.
    last thing would be to write a static "command parser"-like method. a method which receives a message and activates the matching action. not too good, i think ...

    I must admit I haven't written any full on games in JAVA, but I've written a lot of other graphical/non-graphical applications, and if there's one thing I've learnt- it's that there's a design pattern for everything.
    This sounds like an ideal area for using the Observer (sometimes called Watcher or Listener) pattern. Say you have your game objects- tanks, sprites, monkeys etc. That may 'die' during a game process. These game objects are held within a collection in the game controller class.
    Now, the problem is you want to notify the controller class when an object has 'died'- without spoiling the encapsultation design etc. The way to do this is have teh game object fire events to regiseterd listeners.
    For this you need a listener interface i.e. IGameObjectListener and a support class i.e. IGameObjectSupport. The support class gives you methods such as addGameObjectListener(), removeGameObjectListener(), fireGameObjectDied(), fireGameObjectCrashed() etc. Your game objects then extend this class so that they all provide the same listener functionality.
    The IGameObjectListener should provide methods for each of the types of game object events that can occur i.e. gameObjectDied().
    You get your game controller to implement the IGameObjectListener interface, and you get the gameObjectDied() method. when creting each game object, simply call the addGameObjectListener(this) method to registeer the controller with each object.
    In each game object, say you have a reduceHealth() method, that reduces your health by -1 each time it's called. When your health reaches 0, you call the fireGameObjectDied() method. This will then notify all registered listeners i.e. the game controller that this object has died- and you can remove it from the controller. This works, keeps within all encapsulation rules- and offers real flexibility.
    For example, say you now wanted to implement a score board/manager. All you have to do is create a class, ScoreBoard say, that implements the IGameObjectListener interface. Through the game controller you register the the Scoreboard with all 'Bad guy' game objects. When the gameObjectdied method gets called on the Scoreboard- you know it's going to be because a 'Bad guy' died- so you just increase the score by XX amount- and there's your scoreboard.
    Check out some of these other design pattern sites, they may eb of some help- remember, there is a pattern for virtually all types of application/design conundrum!!
    http://www.labsoftware.com/Patterns/
    http://www.javacamp.org/designPattern/
    http://www.fluffycat.com/java/JavaNotes-GoFObserver.html
    http://gamejug.org/books/
    Hope this helps,
    Marc

  • Help with Login Form (JSP DB Java Beans Session Tracking)

    Hi, I need some help with my login form.
    The design of my authetication system is as follows.
    1. Login.jsp sends login details to validation.jsp.
    2. Validation.jsp queries a DB against the parameters received.
    3. If the query result is good, I retrieve some information (login id, name, etc.) from the DB and store it into a Java Bean.
    4. The bean itself is referenced with the current session.
    5. Once all that's done, validation.jsp forwards to main.jsp.
    6. As a means to maintain state, I prefer to use url encoding instead of cookies for obvious reasons.I need some help from step 3 onwards please! Some code snippets will do as well!
    If you think this approach is not a good practice, pls let me know and advice on better practices!
    Thanks a lot!

    Alright,here is an example for you.
    Assume a case where you don't want to give access to any JSP View/HTML Page/Servlet/Backing Bean unless user logging system and let assume you are creating a View Object with the name.
    checkout an example (Assuming the filter is being applied to a pattern * which means when a resource is been accessed by webapplication using APP_URL the filter would be called)
    public doFilter(ServletRequest req,ServletResponse res,FilterChain chain){
         if(req instanceof HttpServletRequest){
                HttpServletRequest request = (HttpServletRequest) req;
                HttpSession session = request.getSession();
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                String method = request.getMethod();
                String auth_type  = request.getAuthType();
                if(session.getAttribute("useInfoBean") != null)
                    request.getRequestDispatcher("/dashBoard").forward(req,res);
                else{
                        if(username != null && password != null && method.equaIsgnoreCase("POST") && (auth_type.equalsIgnoreCase("FORM_AUTH") ||  auth_type.equalsIgnoreCase("CLIENT_CERT_AUTH")) )
                             chain.doFilter(req,res);
                        else 
                          request.getRequestDispatcher("/Login.jsp").forward(req,res);
    }If carefully look at the code the autherization is given only if either user is already logged in or making an attempt to login in secured way.
    to know more insights about where these can used and how these can be used and how ?? the below links might help you.
    http://javaboutique.internet.com/tutorials/Servlet_Filters/
    http://e-docs.bea.com/wls/docs92/dvspisec/servlet.html
    http://livedocs.adobe.com/jrun/4/Programmers_Guide/filters3.htm
    http://www.javaworld.com/javaworld/jw-06-2001/jw-0622-filters.html
    http://www.servlets.com/soapbox/filters.html
    http://www.onjava.com/pub/a/onjava/2001/05/10/servlet_filters.html
    and coming back to DAO Pattern hope the below link might help you.
    http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html
    http://java.sun.com/blueprints/patterns/DAO.html
    http://www.javapractices.com/Topic66.cjp
    http://www.ibm.com/developerworks/java/library/j-dao/
    http://www.javaworld.com/javaworld/jw-03-2002/jw-0301-dao.html
    On the whole(:D) it is always a good practice to get back to Core Java/J2EE Patterns.and know answers to the question Why are they used & How do i implement them and where do i use it ??
    http://www.fluffycat.com/java-design-patterns/
    http://java.sun.com/blueprints/corej2eepatterns/Patterns/index.html
    http://www.cmcrossroads.com/bradapp/javapats.html
    Hope that might help :)
    REGARDS,
    RaHuL

  • Java Programming Help

    I'm new to Java programming, and I have a pretty basic question. I'm writing a simple loan calculator, and I need to know how to format the output of my variables to two decimal places. I've been doing this for about a week, so be nice, please.
    Thanks in advance,
    Wayne

    NumberFormat nf = NumberFormat.getCurrencyInstance();
    double cash = 123.45;
    System.out.println(nf.format(cash));
    Your First Cup of Java
    Essentials, Part 1, Lesson 1: Compiling & Running a Simple Program
    The Java Tutorial - A practical guide for programmers
    New to Java Center
    How To Think Like A Computer Scientist
    Introduction to Computer Science using Java
    The Java Developers Almanac 1.4
    JavaRanch: a friendly place for Java greenhorns
    jGuru
    Object-Oriented Programming Concepts
    Object-oriented language basics
    Don't Fear the OOP
    Books:
    Bert Bates and Kathy Sierra's Head First Java
    Bruce Eckel's Thinking in Java (Free online)
    Joshua Bloch's Effective Java
    Java Design: Building Better Apps and Applets (2nd Edition)

  • Java Signals

    I'm posting this here because I do not know of a better way to address the Java development team directly.
    Requests for a signal handling facility have been made since the earliest releases of Java. The response from the Java development team seems to be that signals are platform dependent and therefore does not meet the demands of WORA. However, what about adding Java's own signal handling facility instead of trying to interface with the platform's?
    I was recently working on a project where I needed two threads to be able to notify each. Instead of using signals, I had them routinely stop and check some shared variable. However, that is more expensive than necessary and it would have been preferred if thread 1 could have thrown a singal to thread 2.
    Note that what I needed was not a way for the operating system to signal Java, but instead a way for Java to signal Java.
    I would propose some Signal object, much like the Exception object. Signals in Java would, of course, be implemented as objects and be capable of carrying more information than their C-like counterparts. It would also be subclassable so that even more information could potentially be passed between threads. A thread would then register some method as signal handler for a particular kind of Signal object (similar to the way that current Java code catches different kinds of Exception objects). If the signal is thrown, execution of the catching thread halts, jumps to the signal handler method, clears the handler registration, and returns to the point of discontinuation. This can be done without need for implementing platform-specific signal handling.
    Even though the above description does not include platform specific signals. It would also be possible to subclass Signal and create a generic SystemSignal object. This SystemSignal object would incur the signal handler for any thread that is listening for a SystemSignal object. The object would contain some getName() method to fetch the platform name of the object (i.e. SIGTERM). If a SystemSignal handler had been registered, the JVM would construct a SystemSignal object and set the name. It would then invoke the signal handler and it would be up to the handler to analyze the signal with getName() and determine if it wants to perform an action for this signal. This would encourage more developers to use platform specific signals in their Java code. However, those developers that needs this functionality are not working around it in Java, they are instead turning to C or C++ for the functionality they require for the entire project or for JNI calls. I don't believe it would be in the best interest of the language to try to discourage the practice of using platform specific signals.
    Thanks,
    Jonathan A. Kennedy
    Programmer/Analyst

    Seems to me there are plenty of ways in Java to handle this issue without trying to get the language changed. Here's a simple one to get you started; I don't know if this is exactly what you meant but it sounds like it.
    You need a way for a Thread to be "notified" of some event by another thread. So, write a subclass of Thread (call it NotifiableThread) that has a notify() method. Then NotifiableThread A, or any code for that matter, can call NotifiableThread B's notify() method at any time. NotifiableThread B's notify() method would be coded to do whatever is supposed to happen when the event is signalled. This is just a rough outline, you would have to deal with synchronization issues and so on.
    Another common Java design pattern you could use is the Listener pattern, where one object registers itself as a Listener with another object that is generating events.

  • BitWise Operations in java

    hi Guys
    Can any one help me out in explaining the below program
    public class Bitwise{
    public static void main (String[]args){
    int x=12&13;
    int y=x^6;
    System.out.println(y I 3)
    Pls help me with the above program in detail explanation
    Raghu

    Here's a better idea. You start by:
    1) adding print statements to print out x and y immediately after they're set.
    2) predicting what you think each print statement will produce, consulting your favorite text or tutorial (see below links) or the JLS if you need to.
    3) running the program.
    4) seeing if the results match what you expected.
    5) trying to figure why they didn't match, if they didn't.
    6) poting a specific question here about what you expected and why, and what you saw instead, if you're unable to figure it out.
    Installation Notes - JDK 5.0 Microsoft Windows (32-bit)
    Your First Cup of Java
    New to Java Center
    The Java&#8482; Tutorial - A practical guide for programmers
    The Java&#8482; Tutorial - Trail: Learning the Java Language
    Java Programming Notes - Fred Swartz
    How To Think Like A Computer Scientist
    Introduction to Computer Science using Java
    The Java Developers Almanac 1.4
    Object-Oriented Programming Concepts
    Object-oriented language basics
    Don't Fear the OOP
    Books:
    The Java Programming Language - 4th Edition
    Head First Java, by Bert Bates and Kathy Sierra
    Thinking in Java (Free online), by Bruce Eckel
    Core Java, by Cay Horstmann and Gary Cornell
    Effective Java, by Joshua Bloch
    http://java.sun.com/developer/Books/javaprogramming/
    Intermediate
    The Craftsman Series
    Java Design: Building Better Apps and Applets (2nd Edition)
    Head First Design Patterns

Maybe you are looking for

  • Facebook notifications on ipad not working

    facebook notifications on ipad not working or displaying on ipad notifications. They are displaying on the iPhone though. Is this normal behavior or perhaps I have missed something.? Please let me know Thank you

  • Lightroom Web Gallery which Allows Users to Select and Email Their Photo Choices

    Does this exist...... A gallery option which allows users to click the photos they want, then easily email those photo numbers to a certain email address? Thanks!

  • Timing Settings in LabView

    We have a LabView Program that is set up to start our pump motor and then read data from a pressure transducer.  We also have a program timer to stop the test after 5 seconds.  The program writes to a file what the pressure was at a specific time.  I

  • Problem with Gradient Tool

    I contacted Adobe because I began having difficulty using the Gradient Tool in Photoshop CC 2014 which had previously worked properly.  As I explained to technical support when I tried to generate a gradient a progress bar would appear, and at first

  • No email option

    Heya everyone, this is a really long story, but basically I had  a pay as you go number ported over to my new contract, which for some reason suspended all my blackberry services, including the access to my hotmail account. Not realising what had hap