当前位置: 移动技术网 > IT编程>开发语言>Java > 使用Java 8中的Lambda表达式实现工厂模式

使用Java 8中的Lambda表达式实现工厂模式

2019年07月22日  | 移动技术网IT编程  | 我要评论
前言 工厂模式是面向对象设计模式中大家最为熟知的设计模式之一。传统的实现方式大家都在熟悉不过了,今天将向大家介绍使用java8 lambda 表达式更加优雅的实现工厂模式

前言

工厂模式是面向对象设计模式中大家最为熟知的设计模式之一。传统的实现方式大家都在熟悉不过了,今天将向大家介绍使用java8 lambda 表达式更加优雅的实现工厂模式。

封面

工厂模式在java中最常用的设计模式之一,它提供了一种很好的实例化对象的方法,是替代new操作的一种模式常用的方式。工厂设计模式可以让你实例化对象的逻辑不用暴露给客户端。

在下面的文章中我将给出使用传统的代码实现工厂模式的一个例子,然后再使用 java8 lambada 方式重新实现

一个例子

首先我将创建一个 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();
 }
}

程序输出

inside circle::draw() method.
inside rectangle::draw() method.

使用lambada实现工厂模式

lambda表达式允许我们定义一个匿名方法,并允许我们以函数式接口的方式使用它。我们也希望能够在已有的方法上实现同样的特性。

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

下面例子展示了构造方法引用

supplier circlesupplier = circle::new;
circle circle = circlesupplier.get();

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

final static map<string, supplier> map = new hashmap<>();
 static {
 map.put("circle", circle::new);
 map.put("rectangle", rectangle::new);
 }

现在我们可以使用这个map来实例化不同的shapes

public class shapefactory {
 final static map<string, supplier> map = new hashmap<>();
 static {
 map.put("circle", circle::new);
 map.put("rectangle", rectangle::new);
 } 
 public shape getshape(string shapetype){
  supplier 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::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)

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网