本文共 7583 字,大约阅读时间需要 25 分钟。
工厂模式和策略模式
public class TestDemo { //策略模式实现 public static void testDemo(){ // CatchSuper catchSuper = CatchFactory.createCatchAccept(CatchPlicy.CatchRebat);// double accptMoney = catchSuper.acceptCatch(1000); double accptMoney = new CatchContext(CatchPlicy.CatchRebat).GetResult(1000); System.out.print(accptMoney); }}abstract class CatchSuper { public abstract double acceptCatch(double money);}//不打折class NormarCatc extends CatchSuper { @Override public double acceptCatch(double money) { return money; }}//打折class CatchRebat extends CatchSuper{ private double rebat = 1d; public CatchRebat(double rebat){ this.rebat = rebat; } @Override public double acceptCatch(double money) { return money*this.rebat; }}//满减class CatchReturn extends CatchSuper{ private double moneyCondition = 0.0d; private double moneyReturn = 0.0d; public CatchReturn(double moneyCo,double moneyre){ this.moneyCondition = moneyCo; this.moneyReturn = moneyre; } @Override public double acceptCatch(double money) { double result = money; if (money >= this.moneyCondition){ result = money - Math.floor(money/moneyCondition)*moneyReturn; } return result; }}//活动策略enum CatchPlicy{ NormarCatc,CatchRebat,CatchReturn}// 工厂实现//class CatchFactory{ // public static CatchSuper createCatchAccept(CatchPlicy policy){ // CatchSuper cs = null;// switch (policy){ // case NormarCatc:// cs = new NormarCatc();// break;// case CatchReturn:// cs = new CatchReturn(300,100);// break;// case CatchRebat:// cs = new CatchRebat(0.85);// break;// }// return cs;// }////}//策略管理类实现class CatchContext{ CatchSuper cs = null; public CatchContext(CatchPlicy policy){ switch (policy){ case NormarCatc: cs = new NormarCatc(); break; case CatchReturn: cs = new CatchReturn(300,100); break; case CatchRebat: cs = new CatchRebat(0.85); break; } } public double GetResult(double money){ return cs.acceptCatch(money); }}
代理模式
/** 实现通过代理实现送礼物给校花,本人不用直接出面* */public class TestDelegateMode { public static void testDelegate(){ BeautyGirl girl = new BeautyGirl("校花"); Proxy proxy = new Proxy(girl); proxy.GiveDolls(); proxy.GiveFlowers(); }}//接口interface IGiveGift { void GiveDolls(); void GiveFlowers();}//被代理方class BeautyGirl{ public String name; public BeautyGirl(String name){ this.name = name; } public String getName() { return name; }}//代理方class Pursuit implements IGiveGift { String name; BeautyGirl beauty; public Pursuit(BeautyGirl beauty){ this.beauty = beauty; this.name = "john"; } @Override public void GiveDolls() { System.out.print(this.name+" GiveDolls to "+beauty.name); } @Override public void GiveFlowers() { System.out.print(this.name+" GiveFlowers to "+beauty.name); }}//代理class Proxy implements IGiveGift{ Pursuit pursuit; public Proxy(BeautyGirl beautyGirl){ this.pursuit = new Pursuit(beautyGirl); } @Override public void GiveDolls() { pursuit.GiveDolls(); } @Override public void GiveFlowers() { pursuit.GiveFlowers(); }}
观察者模式也叫发布订阅模式
第一次画uml图,mac使用umlStarimport java.lang.reflect.Array;import java.util.ArrayList;import java.util.Iterator;public class ObserModeDemo { public static void testObserverDemo(){ Boss boss = new Boss(); StockObserver worker1 = new StockObserver("bob",boss); StockObserver worker2 = new StockObserver("john",boss); StockObserver worker3 = new StockObserver("tom",boss); StockObserver worker4 = new StockObserver("lili",boss); boss.Attach(worker1); boss.Attach(worker2); boss.Attach(worker3); boss.Attach(worker4); boss.setSubjectState("老板回来了,注意点"); boss.Notify(); }}//被观察者要继承的抽象类abstract class Observer{ String name; Subject subject; public Observer(String name,Subject sub){ this.name = name; this.subject = sub; } public abstract void Update();}//接口interface Subject{ void Attach(Observer observer); void Detech(Observer observer); void Notify(); String getSubjectState(); void setSubjectState(String state);}//观察者class Boss implements Subject{ private String state; private ArrayListobservers = new ArrayList (); @Override public void Attach(Observer observer) { Iterator it = observers.iterator(); while (it.hasNext()){ Observer ob = (Observer) it.next(); if (observer.equals(ob) ){ //判断之前是否已经添加过 return; } } observers.add(observer); } @Override public void Detech(Observer observer) { Boolean isContain = false; Iterator it = observers.iterator(); while (it.hasNext()){ Observer ob = (Observer) it.next(); if (observer.equals(ob) ){ isContain = true; } } //判断是否存在 if (isContain==true){ observers.remove(observer); } } @Override public void Notify() { Iterator it = observers.iterator(); while (it.hasNext()){ //消息分发,当然也可以用链表,让观察者可以截断后续的接收者,类似广播 //这里可以进行消息分发顺序处理 Observer ob = (Observer) it.next(); ob.Update(); } } @Override public String getSubjectState() { return this.state; } @Override public void setSubjectState(String state) { this.state = state; //当然也可以设置一旦state状态发生改变就分发// this.Notify(); }}//被观察者class StockObserver extends Observer{ public StockObserver(String name,Subject sub) { super(name,sub); } @Override public void Update() { System.out.print(subject.getSubjectState()+"通知"+name+"注意 \n"); }}
观察者代理模式使用多代理回调实现
import java.util.HashMap;public class TestDelegateForNew { public static void testDelegateNew(){ Work1 work1 = new Work1(); Work2 work2 = new Work2(); callbackUtils.addCallback("work1",work1); callbackUtils.addCallback("work2",work2); callbackUtils.delegateCallback("work1"); callbackUtils.publish(); }}//多代理回调interface CallBack{ public void dowork();}//回调管理类class callbackUtils{ private static HashMapcallbacks= new HashMap<>(); public static void addCallback(String s,CallBack bac){ callbacks.put(s,bac); } public static void delegateCallback(String s){ callbacks.remove(s); } public static void publish(){ if (callbacks.size()==0){ return; } for (CallBack callback:callbacks.values()){ callback.dowork(); } }}class Work1 extends Object implements CallBack{ @Override public void dowork() { System.out.print("设计程序 \n"); }}class Work2 extends Object implements CallBack{ @Override public void dowork() { System.out.print("敲代码 \n"); }}
转载地址:http://denvb.baihongyu.com/