Java8之被簡化的設(shè)計(jì)模式

Java8命令模式簡化



public class Lignt {

    //開燈操作
    public void on(){
        System.out.println("Open the Light");
    }

    //關(guān)燈操作
    public void off(){
        System.out.println("關(guān)燈操作");
    }
}


public interface Command {

    //執(zhí)行函數(shù)接口
    public void execute();
}


//關(guān)燈指令
public class FilpDownCommand implements  Command {

    private Lignt lignt;

    public FilpDownCommand(Lignt lignt) {
        this.lignt = lignt;
    }

    public Lignt getLignt() {
        return lignt;
    }

    public void setLignt(Lignt lignt) {
        this.lignt = lignt;
    }

    @Override
    public void execute() {
        lignt.off();
    }
}


//開燈指令
public class FillUpCommand implements  Command {
    private Lignt lignt;

    public Lignt getLignt() {
        return lignt;
    }

    public void setLignt(Lignt lignt) {
        this.lignt = lignt;
    }

    public FillUpCommand(Lignt lignt) {
        this.lignt = lignt;
    }

    @Override
    public void execute() {
        this.lignt.on();
    }
}

//執(zhí)行者
public class LightSwitch {

    //執(zhí)行順序
    private List <Command>queue=new ArrayList <>(  );

    //添加執(zhí)行命令
    public void add(Command command){
        this.queue.add(  command);
    }

    //執(zhí)行
    public void execute(){
        queue.forEach( e->{
            //執(zhí)行命令操作
            e.execute();
        } );
    }
}

public class LightCommandTest {
    public static void main(String[] args) {
        //實(shí)例化燈對象
        Lignt light=new Lignt();
        //開燈操作
        Command fileUpcommand=new FillUpCommand(  light);
        //關(guān)燈指令
        Command fileDownCommand=new FilpDownCommand( light );

        //執(zhí)行者
        LightSwitch lightSwitch=new LightSwitch();
        lightSwitch.add( fileUpcommand );
        lightSwitch.add( fileDownCommand );
        lightSwitch.add( fileUpcommand );
        lightSwitch.add( fileDownCommand );
        lightSwitch.execute();

    }
}

----------------------------------------------------Java8------------------------------------------------------
public class LightSwitchFP {
    //首先我們直接使用Java8提供的Consumer函數(shù)接口作為我們的命令接口,因?yàn)橛辛薼ambda表達(dá)式,
   // 我們根本無需在單獨(dú)為具體命令對象創(chuàng)建類型,而通過傳入labmda表達(dá)式來完成具體命令對象的創(chuàng)建;

    private List <Consumer<Lignt>>queue=new ArrayList <>(  );

    public void add(Consumer<Lignt>consumer){
        queue.add( consumer );
    }

    //執(zhí)行操作
    public void execute(Lignt lignt){
        queue.forEach( e->{
            e.accept( lignt);
        } );
    }
}

public class LightJava8Command {
    public static void main(String[] args) {
        Lignt lignt=new Lignt();
         //開燈
        Consumer<Lignt>onLignt=lignt1 -> lignt.on();
        //關(guān)燈
        Consumer<Lignt>ofLignt=lignt1 -> lignt.off();
        //創(chuàng)建執(zhí)行者
        onLignt.andThen( ofLignt ).andThen( onLignt ).andThen( ofLignt ).accept( lignt );
//        LightSwitchFP lightSwitch = new LightSwitchFP();
//        lightSwitch.add(onLignt);
//        lightSwitch.add(ofLignt);
//        lightSwitch.add(onLignt);
//        lightSwitch.add(ofLignt);
//        lightSwitch.execute(lignt);
    }
}

Consumer簡介
Consumer的作用是給定義一個(gè)參數(shù),對其進(jìn)行(消費(fèi))處理,處理的方式可以是任意操作.

public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *接收單個(gè)參數(shù),返回為空
     * @param t the input argument
     */
    void accept(T t);

    /**
  這是一個(gè)用來做鏈?zhǔn)教幚淼姆椒?,該方法返回的是一個(gè)Consumer對象,假設(shè)調(diào)用者的Consumer對象為A,輸入?yún)?shù)Consumer對象設(shè)為B,那么返回的Consumer對象C的accept方法的執(zhí)行體就是A.accept()+B.accept()
     */
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}

demo如下


public class Java8_Example {
    public static void main(String[] args) {
        //Java8當(dāng)中的設(shè)計(jì)模式
        //1.命令模式
        Lignt lignts=new Lignt();
        Consumer<Lignt>consumer=lignt -> System.out.println("TestManager");
        Consumer<Lignt>consumer1=lignt -> System.out.println("TestManager1");
        consumer.andThen( consumer1 ).accept( lignts );

    }
}
控制臺輸出  
TestManager
TestManager1
應(yīng)用場景可以為多個(gè)操作同時(shí)執(zhí)行
Process finished with exit code 0

策略模式

//策略模式
public interface Strategy {
    //計(jì)算
    public Integer compute(Integer a,Integer b);
}

//加
public class Add implements  Strategy {

    @Override
    public Integer compute(Integer a, Integer b) {
        return a+b;
    }
}
//乘
public class Multiply implements  Strategy {

    @Override
    public Integer compute(Integer a, Integer b) {
        return a*b;
    }
}
public class StrageGyTest {
    public static void main(String[] args) {
        Strategy strategy = new Add();
        Context context = new Context( strategy );
        Integer c = context.use( 1, 2 );
        System.out.println( c );
    }
}
----------------------------------------------Java8----------------------------------------------------------
public class ContextFp {
    private BinaryOperator<Integer>binaryOperator;
    public ContextFp(BinaryOperator binaryOperator){
        this.binaryOperator=binaryOperator;
    }

    public Integer use(Integer first,Integer second){
        Integer apply = binaryOperator.apply( first,second);
        return apply;
    }
}

枚舉封裝方法

public enum StrageEnum {
    ADD( () -> (x, y) -> x + y ),
    MULTIPLY( () -> (x, y) -> x * y );

    //suppiler  作為java8的接口 返回一個(gè)對象的實(shí)例
    //BinaryOperator  接受2個(gè)參數(shù) 
    private Supplier <BinaryOperator <Integer>> operation;

    private StrageEnum(Supplier <BinaryOperator <Integer>> operation) {
        this.operation = operation;
    }

    public BinaryOperator <Integer> get() {
        return operation.get();
    }
}

public class ContextFPEnum {
    private StrageEnum strageEnum;

    public ContextFPEnum(StrageEnum strageEnum){
        this.strageEnum=strageEnum;
    }

    public Integer use(Integer first,Integer second){
        return strageEnum.get().apply( first,second );
    }

}


public class StrageGyJava8Test {

  //  public static final BinaryOperator <Integer> addBinary = (op1, op2) -> op1 + op2;
   // public static final BinaryOperator <Integer> multiply = (op1, op2) -> op1 * op2;

    public static void main(String[] args) {
//        ContextFp contextFp=new ContextFp( addBinary );
//        System.out.println(contextFp.use( 10,11));
//        ContextFp contextFp1=new ContextFp( multiply );
//        System.out.println(contextFp1.use( 1,2 ));
        //累加
        ContextFPEnum contextFPEnum = new ContextFPEnum( StrageEnum.ADD );
        System.out.println( contextFPEnum.use( 1, 2 ) );
        //乘
        ContextFPEnum contextFPEnum1 = new ContextFPEnum( StrageEnum.MULTIPLY );
        System.out.println( contextFPEnum1.use( 2, 3 ) );
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容