HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML

"Object-Producer-Consumer Idiom"
Vol 1., Issue 1, P.23


Program Listing

import java.util.Vector;

// Section 1:      Classes to support MessageX

class Message { 	// extends Object whos identification capabili
							// ties are used has whatever other general
							// capabilities a Message might have

abstract class MessageProducer {
    public abstract boolean handles(Message m);
						// return true can handle message

    public abstract void produce (Message m);   
						// interface to produce the message

//================================================================ // Section 2: Classes to support MessageX //================================================================ class MessageX extends Message { } //Some message class // Any object that gets a MessageX implements an // interface to consume a MessageX. interface MessageConsumerX { public abstract void consume (MessageX m); } class MessageProducerX extends MessageProducer { private MessageConsumerX consumer; public MessageProducerX (MessageConsumerX c) { consumer = c; } public boolean handles(Message m) { return ( m instanceof MessageX); } public void produce (Message m) { consumer.consume ((MessageX)m); } } // Classes to support MessageY and MessageZ removed for brevity // but they are exactly like those for MessageX with the type // changed

//================================================================= // Section 3 The MessageReceiver uses general abstractions // only //=============================================================== class MessageReceiver { private Vector producers = new Vector(); public void add (MessageProducer producer) { producers.addElement (producer); } public void postMessage(Message m) { for (int i=0; i < producers.size(); i++) { MessageProducer producer = (MessageProducer)producers.elementAt(i); if (producer.handles(m)) { producer.produce(m); break; } } } }

//=============================================================== // Section 4 The MessageConsumer defines which messages to // process the interface to process thhose message once received //=============================================================== class MessageConsumer implements MessageConsumerX, MessageConsumerY { private MessageReceiver receiver; public MessageConsumer (MessageReceiver r) { receiver = r; receiver.add (new MessageProducerX(this)); receiver.add (new MessageProducerY(this)); } public void consume (MessageX m) { System.out.println("Received a MessageX"); } public void consume (MessageY m) { System.out.println("Received a MessageY"); } }

//================================================================ // Section 5 main() to do a simple test of the code //================================================================ class TestProducerConsumer { static public void main (String[] args) { MessageReceiver receiver = new MessageReceiver(); MessageConsumer consumer = new MessageConsumer(receiver); receiver.postMessage(new MessageX()); receiver.postMessage(new MessageY()); receiver.postMessage(new MessageZ()); } }


All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.