下载 23种设计模式源码 :http://download.csdn.net/download/knight_black_bob/8936043
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
创建型模式,共五种:
工厂方法模式 抽象工厂模式 单例模式 建造者模式 原型模式
结构型模式,共七种:
适配器模式 装饰器模式 代理模式 外观模式 桥接模式 组合模式 享元模式
行为型模式,共十一种:
策略模式 模板方法模式 观察者模式 迭代子模式 责任链模式 命令模式
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
著名的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(); } }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!