工厂模式是面向对象设计模式中你们最为熟知的设计模式之一。传统的实现方式你们都在熟悉不过了,今天将向你们介绍使用Java8 Lambda 表达式更加优雅的实现工厂模式。java
工厂模式在java中最经常使用的设计模式之一,它提供了一种很好的实例化对象的方法,是替代new操做的一种模式经常使用的方式。工厂设计模式可让你实例化对象的逻辑不用暴露给客户端。shell
在下面的文章中我将给出使用传统的代码实现工厂模式的一个例子,而后再使用 Java8 Lambada 方式从新实现express
首先我将建立一个 Shape 接口以及几个实现类,而后会在接下来的步骤中实现ShapeFactory,最后会给出详细的调用实例并输出结果。设计模式
新建接口:Shape.java服务器
public interface Shape { void draw(); }
定义两个 Shape的实现类:Circle.java & Rectangle.java微信
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."); } }
建立Shape的工厂类,并实现根据指定参数返回不一样的Shape的工厂方法:闭包
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; } }
ShapeFactory 根据传入的shapeType 返回不一样的Shape。下面是具体使用的例子。架构
public class FactoryPatternDemo { public static void main(String[] args) { ShapeFactory shapeFactory = new ShapeFactory(); //get an object of Circle and call its draw method. Shape shape1 = shapeFactory.getShape("CIRCLE"); //call draw method of Circle shape1.draw(); //get an object of Rectangle and call its draw method. Shape shape2 = shapeFactory.getShape("RECTANGLE"); //call draw method of Rectangle shape2.draw(); } }
程序输出ide
Inside Circle::draw() method. Inside Rectangle::draw() method.
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对象:
FactoryPatternDemo.java
public class FactoryPatternDemo { 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(); } }
程序输出
Inside Circle::draw() method. Inside Rectangle::draw() method.
这里的Shape::new
能够被看做为lambda表达式的简写形式。尽管方法引用不必定(好比在这个例子里)会把语法变的更紧凑,但它拥有更明确的语义——若是咱们想要调用的方法拥有一个名字,咱们就能够经过它的名字直接调用它。
若是Shape
构造函数须要多个参数,那么你就须要从新实现本身的Supplier
如:
() -> new Circe(args)
做者信息
本文系力谱宿云 LeapCloud技术团队_数据服务组 成员:马传林【翻译】
马传林,从事开发工做已经有多年。当前在MaxLeap数据服务组担任开发工程师,主要负责MaxWon服务器开发。
原文:https://dzone.com/articles/fa...
译文首发:https://blog.maxleap.cn/archi...
做者往期佳做
移动云平台的基础架构之旅(一):云应用
ORM “杀器”之 JOOQ
欢迎关注微信公众号