中介者模式Mediator

//Purpose.  Mediator design pattern

//1. Create an "intermediary" that decouples "senders" from "receivers"
//2. Producers are coupled only to the Mediator
//3. Consumers are coupled only to the Mediator
//4. The Mediator arbitrates the storing and retrieving of messages

class Mediator { // 1. The "intermediary"
	private boolean slotFull = false; // 4. The Mediator arbitrates
	private int number;

	public synchronized void storeMessage(int num) {
		while (slotFull == true)
			// no room for another message
			try {
				wait();
			} catch (InterruptedException e) {
			}
		slotFull = true;
		number = num;
		notifyAll();
	}

	public synchronized int retrieveMessage() {
		while (slotFull == false)
			// no message to retrieve
			try {
				wait();
			} catch (InterruptedException e) {
			}
		slotFull = false;
		notifyAll();
		return number;
	}
}

class Producer extends Thread {
	private Mediator med; // 2. Producers are coupled only to
	private int id; // the Mediator
	private static int num = 1;

	public Producer(Mediator m) {
		med = m;
		id = num++;
	}

	public void run() {
		int num;
		while (true) {
			med.storeMessage(num = (int) (Math.random() * 100));
			System.out.print("p" + id + "-" + num + "   ");
		}
	}
}

class Consumer extends Thread {
	private Mediator med; // 3. Consumers are coupled only to
	private int id; // the Mediator
	private static int num = 1;

	public Consumer(Mediator m) {
		med = m;
		id = num++;
	}

	public void run() {
		while (true) {
			System.out.print("c" + id + "-" + med.retrieveMessage() + "   ");
		}
	}
}

class MediatorDemo {
	public static void main(String[] args) {
		Mediator mb = new Mediator();
		new Producer(mb).start();
		new Producer(mb).start();
		new Consumer(mb).start();
		new Consumer(mb).start();
		new Consumer(mb).start();
		new Consumer(mb).start();
	}
}

 

你可能感兴趣的:(设计模式)