设计模式 之 组合模式

 

著名的Javapns  中使用 模式种类 很多 ,大框架 就是个 组合模式 ,我把拆分下来,如下

 

package 设计模式.组合模式;
public class Push {

public static PushQueue queue(int numberOfThreads){
PushQueue queue = numberOfThreads <= 1 ? new  NotificationThread() :   new  NotificationThreads(numberOfThreads);
return queue;
}
}

 

 

package 设计模式.组合模式;

public interface PushQueue {

	PushQueue add(String msg);
	PushQueue start();
}

 

package 设计模式.组合模式;

import java.util.List;
import java.util.Vector;

public class NotificationThread implements Runnable, PushQueue {

	Thread thread;
	private boolean busy = false;
	private boolean started = false;
	private List<String> messages = new Vector<String>();

	public NotificationThread() {
		this(null);
	}

	public NotificationThread(NotificationThreads threads) {
		super();
		thread = new Thread(threads,this,"thread");
		this.thread.setDaemon(true);
	}

	@Override	
	public void run() { 
		while (!messages.isEmpty()) {
			busy = true;
			String message = messages.get(0);
			messages.remove(message);

			System.out.println("--------->" + message);
			try {
				Thread.sleep(1 * 1000);
			} catch (Exception e) {
			}
			busy = false;
		}
		try {
			Thread.sleep(1 * 1000);
		} catch (Exception e) {
		}
	}

	public boolean isBusy() {
		return busy;
	}

	@Override
	public PushQueue add(String msg) {
		try {
			messages.add(msg);
			this.thread.interrupt();
		} catch (Exception e) {
		}
		return this;
	}

	@Override
	public PushQueue start() { 
		if (started) return this;
		started = true;
		try {
			this.thread.start();
		} catch (IllegalStateException e) {
		}
		return this;
	}

}

 

package 设计模式.组合模式;

import java.util.List;
import java.util.Vector;

public class NotificationThreads extends ThreadGroup implements PushQueue {

	private List<NotificationThread> threads = new Vector<NotificationThread>();
	private int nextThread = 0;
	private boolean started = false;
	
	public NotificationThreads(String name) {
		super(name); 
	}

	public NotificationThreads(int numberOfThreads) {
		super("multi");
		for (int i = 0; i < numberOfThreads; i++) {
			threads.add(new NotificationThread(this));
		}
	}

	@Override
	public PushQueue add(String msg) {
		start(); 
		NotificationThread targetThread = getNextAvailableThread();
		targetThread.add(msg);
		return targetThread;
	}
	 
	protected NotificationThread getNextAvailableThread() {
		for (int i = 0; i < threads.size(); i++) {
			NotificationThread thread = getNextThread();
			boolean busy = thread.isBusy();
			if (!busy) return thread;
		}
		return getNextThread(); /* All threads are busy, return the next one regardless of its busy status */
	}
 
	protected synchronized NotificationThread getNextThread() {
		if (nextThread >= threads.size()) nextThread = 0;
		NotificationThread thread = threads.get(nextThread++);
		return thread;
	}


	@Override
	public PushQueue start() { 
		if (started) return this;
		started = true; 
		for (NotificationThread thread :threads) {
		    thread.start();
		}
		return this;
	}

}

 

 

package 设计模式.组合模式;


//将对象组合成树形结构以表示“部分整体”的层次结构。组合模式使得用户对单个对象和使用具有一致性。
public class CompositeTest {

	 
	public static void main(String[] args) {
		PushQueue apush =  Push.queue(2);
		apush.start();
		apush.add("baoyou0");

		new Thread(){
			public void run() {
				try {
					Thread.sleep(100000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			};
		}.start();
	}
}

 

 

 

 

 

 

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