設計模式

工廠模式和策略模式

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();
    }
}

觀察者模式也叫發佈訂閱模式

import 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 ArrayList<Observer> observers  =  new ArrayList<Observer>();
    @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 HashMap<String,CallBack> callbacks= 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");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章