策略模式
策略模式定義了一系列算法,并將每個算法封裝起來,使他們可以相互替換,且算法的變化不會影響到
使用算法的客戶。需要設計一個接口,為一系列實現(xiàn)類提供統(tǒng)一的方法,多個實現(xiàn)類實現(xiàn)該接口,設計
一個抽象類(可有可無,屬于輔助類)
案例一
統(tǒng)一接口
/**
* Created by Wgs on 2017/9/25.
*/
public interface ICalculator {
public int calculate(String exp);
}
提供輔助類
/**
* Created by Wgs on 2017/9/25.
*/
public abstract class AbstractCalculator {
public int[] split(String exp, String opt) {
String[] array = exp.split(opt);
int[] arrayInt = new int[2];
arrayInt[0] = Integer.parseInt(array[0]);
arrayInt[1] = Integer.parseInt(array[1]);
return arrayInt;
}
}
三個實現(xiàn)類
/**
* Created by Wgs on 2017/9/25.
*/
public class Plus extends AbstractCalculator implements ICalculator {
@Override
public int calculate(String exp) {
int arrayInt[] = split(exp,"\\+");
return arrayInt[0] + arrayInt[1];
}
}
/**
* Created by Wgs on 2017/9/25.
*/
public class Multiply extends AbstractCalculator implements ICalculator {
@Override
public int calculate(String exp) {
int[] arrayInt = split(exp, "\\*");
return arrayInt[0] * arrayInt[1];
}
}
測試類
/**
* Created by Wgs on 2017/9/25.
*/
public class StrategyTest {
public static void main(String[] args) {
String exp = "2-8";
ICalculator iCalculator = new Minus();
int result = iCalculator.calculate(exp);
System.out.println(result);
}
}
策略模式的決定權在用戶,系統(tǒng)本身提供不同算法的實現(xiàn),新增或者刪除算法,對各種算法做封裝。因
此,策略模式多用在算法決策系統(tǒng)中,外部用戶只需要決定用哪個算法即可。
案例二
劉備要到江東娶老婆了,走之前諸葛亮給趙云(伴郎)三個錦囊妙計,說是按天機拆開解決棘手問題,
嘿,還別說,真是解決了大問題,搞到最后是周瑜陪了夫人又折兵呀,那咱們先看看這個場景是什么樣子
的。
先說這個場景中的要素:三個妙計,一個錦囊,一個趙云,妙計是小亮同志給的,妙計是放置在錦囊
里,俗稱就是錦囊妙計嘛,那趙云就是一個干活的人,從錦囊中取出妙計,執(zhí)行,然后獲勝
三個妙計是同一類型的東東,那咱就寫個接口:
/**
* Created by Wgs on 2017/9/25.
*/
public interface IStrategy {
// 每一幾個錦囊妙計都是一個可執(zhí)行的算法
public void operate();
}
三個實現(xiàn)類
/**
* Created by Wgs on 2017/9/25.
*/
public class BackDoor implements IStrategy {
@Override
public void operate() {
System.out.println("找喬國老幫忙,讓吳國太給孫權施加壓力");
}
}
/**
* Created by Wgs on 2017/9/25.
*/
public class BlockEnemy implements IStrategy {
@Override
public void operate() {
System.out.println("孫夫人斷后,擋住追兵");
}
}
妙計有了,還要有錦囊
/**
* Created by Wgs on 2017/9/25.
*/
public class Context {
private IStrategy iStrategy;
// 構造方法,你要私用那個妙計
public Context(IStrategy iStrategy) {
this.iStrategy = iStrategy;
}
// 使用妙計
public void operate() {
iStrategy.operate();
}
}
然后就是趙云雄赳赳的揣著三個錦囊,拉著已步入老年行列的、還想著娶純情少女的、色迷迷的劉老
爺子去入贅了,嗨,還別說,小亮的三個妙計還真是不錯,瞅瞅:
/**
* Created by Wgs on 2017/9/25.
*/
public class ZaoYun {
public static void main(String[] args) {
/**
* 趙云出場了,他根據(jù)諸葛亮給他的交代,依次拆開妙計
*/
IStrategy iStrategy = new BackDoor();
Context context = new Context(iStrategy);
context.operate();
}
}
代理模式
案例一
其實每個模式名稱就表明了該模式的作用,代理模式就是多一個代理類出來,替原對象進行一些操作,
比如我們在租房子的時候回去找中介,為什么呢?因為你對該地區(qū)房屋的信息掌握的不夠全面,希望找
一個更熟悉的人去幫你做,此處的代理就是這個意思。再如我們有的時候打官司,我們需要請律師,因
為律師在法律方面有專長,可以替我們進行操作
接口
/**
* Created by Wgs on 2017/9/25.
*/
public interface SourceAble {
public void method();
}
被代理類
/**
* Created by Wgs on 2017/9/25.
*/
public class Source implements SourceAble {
@Override
public void method() {
System.out.println("Source.method");
}
}
被代理類
/**
* Created by Wgs on 2017/9/25.
*/
public class Proxy implements SourceAble {
private SourceAble sourceAble;
public Proxy() {
this.sourceAble = new Source();
}
@Override
public void method() {
before();
sourceAble.method();
after();
}
private void after() {
System.out.println("Proxy.after");
}
public void before() {
System.out.println("Proxy.before");
}
}
測試類
/**
* Created by Wgs on 2017/9/25.
*/
public class ProxyTest {
public static void main(String[] args) {
SourceAble proxy = new Proxy();
proxy.method();
}
}
案例二
什么是代理模式呢?我很忙,忙的沒空理你,那你要找我呢就先找我的代理人吧,那代理人總要知道
被代理人能做哪些事情不能做哪些事情吧,那就是兩個人具備同一個接口,代理人雖然不能干活,但是被
代理的人能干活呀。
比如西門慶找潘金蓮,那潘金蓮不好意思答復呀,咋辦,找那個王婆做代理,表現(xiàn)在程序上時這樣的:
先定義一種類型的女人:
/**
* Created by Wgs on 2017/9/25.
* 定義一種類型的女人,王婆和潘金蓮都屬于這個類型的女人
*/
public interface KindWomen {
//這種類型的女人能做什么事情呢?
public void makeEyesWithMan(); //拋媚眼
public void happyWithMan(); //happy what? You know that!
}
潘金蓮
/**
* Created by Wgs on 2017/9/25.
*/
public class PanJinLian implements KindWomen {
@Override
public void makeEyesWithMan() {
System.out.println("潘金蓮破媚眼..");
}
@Override
public void happyWithMan() {
System.out.println("潘金蓮在和男人做那個.....");
}
}
王婆
/**
* Created by Wgs on 2017/9/25.
*/
public class WangPo implements KindWomen {
private KindWomen kindWomen;
public WangPo() {
this.kindWomen = new PanJinLian();
}
// 她可以是KindWomen的任何一個女人的代理,只要你是這一類型
public WangPo(KindWomen kindWomen) {
this.kindWomen = kindWomen;
}
@Override
public void makeEyesWithMan() {
//王婆這么大年齡了,誰看她拋媚眼?!
this.kindWomen.makeEyesWithMan();
}
@Override
public void happyWithMan() {
// 自己老了,干不了,可以讓年輕的代替
this.kindWomen.happyWithMan();
}
}
西門慶
/**
* Created by Wgs on 2017/9/25.
*/
public class XiMenQing {
/*
* 水滸里是這樣寫的:西門慶被潘金蓮用竹竿敲了一下難道,癡迷了,
* 被王婆看到了, 就開始撮合兩人好事,王婆作為潘金蓮的代理人
* 收了不少好處費,那我們假設一下:
* 如果沒有王婆在中間牽線,這兩個不要臉的能成嗎?難說的很!
*/
public static void main(String[] args) {
//把王婆叫出來
KindWomen kindWomen = new WangPo();
//然后西門慶就說,我要和潘金蓮happy,然后王婆就安排了西門慶丟筷子的那出戲
kindWomen.makeEyesWithMan();
kindWomen.happyWithMan();
}
}