Java8命令模式简化
public class Lignt {
//开灯操作
public void on(){
System.out.println("Open the Light");
}
//关灯操作
public void off(){
System.out.println("关灯操作");
}
}
public interface Command {
//执行函数接口
public void execute();
}
//关灯指令
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();
}
}
//执行者
public class LightSwitch {
//执行顺序
private List <Command>queue=new ArrayList <>( );
//添加执行命令
public void add(Command command){
this.queue.add( command);
}
//执行
public void execute(){
queue.forEach( e->{
//执行命令操作
e.execute();
} );
}
}
public class LightCommandTest {
public static void main(String[] args) {
//实例化灯对象
Lignt light=new Lignt();
//开灯操作
Command fileUpcommand=new FillUpCommand( light);
//关灯指令
Command fileDownCommand=new FilpDownCommand( light );
//执行者
LightSwitch lightSwitch=new LightSwitch();
lightSwitch.add( fileUpcommand );
lightSwitch.add( fileDownCommand );
lightSwitch.add( fileUpcommand );
lightSwitch.add( fileDownCommand );
lightSwitch.execute();
}
}
----------------------------------------------------Java8------------------------------------------------------
public class LightSwitchFP {
//首先我们直接使用Java8提供的Consumer函数接口作为我们的命令接口,因为有了lambda表达式,
// 我们根本无需在单独为具体命令对象创建类型,而通过传入labmda表达式来完成具体命令对象的创建;
private List <Consumer<Lignt>>queue=new ArrayList <>( );
public void add(Consumer<Lignt>consumer){
queue.add( consumer );
}
//执行操作
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();
//关灯
Consumer<Lignt>ofLignt=lignt1 -> lignt.off();
//创建执行者
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的作用是给定义一个参数,对其进行(消费)处理,处理的方式可以是任意操作.
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*接收单个参数,返回为空
* @param t the input argument
*/
void accept(T t);
/**
这是一个用来做链式处理的方法,该方法返回的是一个Consumer对象,假设调用者的Consumer对象为A,输入参数Consumer对象设为B,那么返回的Consumer对象C的accept方法的执行体就是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当中的设计模式
//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
应用场景可以为多个操作同时执行
Process finished with exit code 0
策略模式
//策略模式
public interface Strategy {
//计算
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的接口 返回一个对象的实例
//BinaryOperator 接受2个参数
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 ) );
}
}