[Biojava-l] Access to variables

Richard Holland richard.holland at ebi.ac.uk
Tue May 9 13:28:28 UTC 2006


There is no easy way, but BioJava handles things like this using a
listener model. 

What this means is that some objects are EventListeners, whilst other
fire events to a central EventManager. The EventManager sends these
events to all EventListeners registered as being interested in that kind
of event.

The simplest form is:

	public class Event {
		public final Object object;
		public final Object eventType;
		public Event(Object object, Object eventType) {
			this.object = object;
			this.eventType = eventType;
		}
	}

	public interface EventListener {
		public void eventOccurred(Event e);
	}
	
	public class EventManager {
		private static final Map eventListeners = new HashMap();
		public static void registerEventListener(EventListener eventListener,
Object eventType) {
			if (!eventListeners.containsKey(eventType))
				eventListeners.put(eventType, new ArrayList());
			((List)eventListeners.get(eventType)).add(eventListener);
		}
		public static void fireEvent(Event e) {
			for (Iterator i = ((List)eventListeners.get(e.eventType)).iterator();
i.hasNext(); ) 
				((EventListener)i.next()).eventOccurred(e);
		}
	}

In your example, the class representing the alignment would fire an
event whenever the alignment changed, by calling EventManager.fireEvent
() from the method which made the change. For instance, assuming the
method which made the change was insertGap():

	public void insertGap(int gapPosition) {
		// do the work of inserting the gap here.
		...
		...
		// Fire an event.
		EventManager.fireEvent(new Event(this, "gapInserted"));
	}

In the class representing the consensus, which may or may not be the
same class as the alignment, you would do this:

	public class Consensus implements EventListener {
		private Alignment alignment;
		public Consensus(Alignment alignment) {
			this.alignment = alignment;
			this.updateConsensus();
			EventManager.registerEventListener(this, "gapInserted");
		}
		public void eventOccurred(Event e) {
			if (e.eventType.equals("gapInserted")) {
				this.updateConsensus();
			}
		}
		private void updateConsensus() {
			// do the updating here
			...
			...
		}
	}

This is by far a simplistic example, but I hope you get the idea.

There is much more out there on the web - Wikipedia is a good starting
point for programming concepts such as these.

cheers,
Richard


On Tue, 2006-05-09 at 14:09 +0100, Nathan S. Haigh wrote:
> Well, I've jumped straight in and am already planning to use get/set methods
> for most of my variables :o)
> 
> In my app I plan to have a multiple alignment displayed and the user opts to
> calculate a consensus sequence as part of a larger process. The user will
> also be able to make changes to the alignment. Therefore, if a consensus
> sequence has already been calculated I'd like this to be automatically
> updated to reflect the changes in the alignment. Do you know of a small
> coded example of how this is done i.e. in your example: detecting if the
> sequence changed and processing a block of code if it has.
> 
> Cheers
> Nath
> 
> 
> > -----Original Message-----
> > From: Richard Holland [mailto:richard.holland at ebi.ac.uk]
> > Sent: 09 May 2006 13:57
> > To: n.haigh at sheffield.ac.uk
> > Cc: biojava-l at lists.open-bio.org
> > Subject: Re: [Biojava-l] Access to variables
> > 
> > hi there.
> > 
> > Get/Set methods with private fields are by far the preferred way of
> > doing things. This ensures that the object gets to know whenever one of
> > its variables has changed.
> > 
> > For example, assume you had a class that represented a sequence, and one
> > of the methods in that class computed some expensive statistic on that
> > sequence and stored that statistic in another variable. If the sequence
> > itself changed then you'd need to recompute the statistic too. Without
> > get/set, there'd be no way of knowing the sequence had changed, and no
> > way of knowing when to recompute the statistic.
> > 
> > cheers,
> > Richard
> > 
> > On Tue, 2006-05-09 at 12:19 +0100, Nathan S. Haigh wrote:
> > > Apologies if this comes through more than once - I forgot to send in
> > plain
> > > text without attachments!
> > >
> > > In case you don't know - I'm new to Java..
> > >
> > > I'm working out an interface/class structure for part of an app I want
> > to
> > > convert from Perl to Java and I have a question about the best way to
> > > provide access to variables to the client programmer:
> > >
> > > Is it best to have variables you want the client programmer to access
> > just
> > > made public or is it best to provide access to them via a get/set
> > method?
> > > >From my limited reading of "Thinking in Java" I would think it best to
> > hide
> > > the implementation from the user and provide methods to access these
> > > variables e.g. setThreshold and getThreshold modify the private variable
> > > threshold - is that correct or am I way off the mark!?
> > >
> > > Thanks for any clarification.
> > >
> > > Nath
> > >
> > > ------------------------------------------------------------------------
> > ----
> > > ------
> > > Dr. Nathan S. Haigh
> > > Bioinformatics PostDoctoral Research Associate
> > >
> > > Room B2 211                                            Tel: +44 (0)114
> > 22
> > > 20112
> > > Department of Animal and Plant Sciences                Mob: +44 (0)7742
> > 533
> > > 569
> > > University of Sheffield                                Fax: +44 (0)114
> > 22
> > > 20002
> > > Western Bank                                           Web:
> > > www.bioinf.shef.ac.uk
> > > Sheffield
> > > www.petraea.shef.ac.uk
> > > S10 2TN
> > > ------------------------------------------------------------------------
> > ----
> > > ------
> > >
> > > ---
> > > avast! Antivirus: Outbound message clean.
> > > Virus Database (VPS): 0615-2, 12/04/2006
> > > Tested on: 09/05/2006 12:18:14
> > > avast! - copyright (c) 1988-2006 ALWIL Software.
> > > http://www.avast.com
> > >
> > >
> > >
> > >
> > > ---
> > > avast! Antivirus: Outbound message clean.
> > > Virus Database (VPS): 0615-2, 12/04/2006
> > > Tested on: 09/05/2006 12:19:29
> > > avast! - copyright (c) 1988-2006 ALWIL Software.
> > > http://www.avast.com
> > >
> > >
> > >
> > >
> > >
> > > _______________________________________________
> > > Biojava-l mailing list  -  Biojava-l at lists.open-bio.org
> > > http://lists.open-bio.org/mailman/listinfo/biojava-l
> > >
> > --
> > Richard Holland (BioMart Team)
> > EMBL-EBI
> > Wellcome Trust Genome Campus
> > Hinxton
> > Cambridge CB10 1SD
> > UNITED KINGDOM
> > Tel: +44-(0)1223-494416
> 
> ---
> avast! Antivirus: Outbound message clean.
> Virus Database (VPS): 0615-2, 12/04/2006
> Tested on: 09/05/2006 14:09:48
> avast! - copyright (c) 1988-2006 ALWIL Software.
> http://www.avast.com
> 
> 
> 
> 
> 
-- 
Richard Holland (BioMart Team)
EMBL-EBI
Wellcome Trust Genome Campus
Hinxton
Cambridge CB10 1SD
UNITED KINGDOM
Tel: +44-(0)1223-494416




More information about the Biojava-l mailing list