【设计模式】Lambda实现工厂模式

来源:互联网 发布:域名管理中心 编辑:程序博客网 时间:2024/05/16 05:48

简单原始例子

public interface Shape {    void draw();}

public class Rectangle implements Shape {    @Override    public void draw() {        System.out.println("Inside Rectangle::draw() method.");    }}public class Circle implements Shape {    @Override    public void draw() {        System.out.println("Inside Circle::draw() method.");    }}

public class ShapeFactory {    //use getShape method to get object of type shape    public Shape getShape(String shapeType){        if(shapeType == null){        return null;        }        if(shapeType.equalsIgnoreCase("CIRCLE")){        return new Circle();        } else if(shapeType.equalsIgnoreCase("RECTANGLE")){        return new Rectangle();        }        return null;    }}

使用Lambada实现工厂模式

Lambda表达式允许我们定义一个匿名方法,并允许我们以函数式接口的方式使用它

方法引用和lambda表达式拥有相同的特性(例如,它们都需要一个目标类型,并需要被转化为函数式接口的实例),不过我们并不需要为方法引用提供方法体,我们可以直接通过方法名称引用已有方法

Supplier<Shape> circleSupplier = Circle::new;Circle circle = circleSupplier.get();

根据构造方法引用的原理,我们可以重写之前的代码,定义一个Map来保存shape name 和它对应的构造方法引用

final static Map<String, Supplier<Shape>> map = new HashMap<>();static {    map.put("CIRCLE", Circle::new);    map.put("RECTANGLE", Rectangle::new);}

使用这个map来实例化不同的shapes

public class ShapeFactory {final static Map<String, Supplier<Shape>> map = new HashMap<>();    static {        map.put("CIRCLE", Circle::new);        map.put("RECTANGLE", Rectangle::new);    }    public Shape getShape(String shapeType){        Supplier<Shape> shape = map.get(shapeType.toUpperCase());        if(shape != null) {            return shape.get();        }        throw new IllegalArgumentException("No such shape " + shapeType.toUpperCase());    }}

使用lambada表达式实现的工厂方法来创建shape对象

public static void main(String[] args) {    Supplier<ShapeFactory> shapeFactory = ShapeFactory::new;    //call draw method of circle    shapeFactory.get().getShape("circle").draw();    //call draw method of Rectangle    shapeFactory.get().getShape("rectangle").draw();}

这里的 Shape::new可以被看作为lambda表达式的简写形式。尽管方法引用不一定会把语法变的更紧凑,但它拥有更明确的语义——如果我们想要调用的方法拥有一个名字,我们就可以通过它的名字直接调用它。

如果 Shape 构造函数需要多个参数,那么你就需要重新实现自己的Supplier

() -> new Circe(args)

原文转载

0 0