Original address: Https://dzone.com/articles/factory-pattern-using-lambda-expression-in-java-8?utm_source=Top%205&utm_ medium=email&utm_campaign=2017-03-04
Factory mode is one of the most famous patterns in Java. If you use lambda expressions, you can use them to implement patterns, but be careful to scale them. through Monika Goel March 01, 17 · Java area
Factory mode is one of the most common design patterns in Java. This type of design pattern belongs to the creation mode of the host, because this pattern provides the best way to create an object. Factory design mode allows you to create objects without exposing the instantiation logic to the client.
In this article, I want to give an example of a factory pattern, and then rewrite the same example using the lambda expression introduced in Java 8. Factory Model: an example
I will create a shape interface and a concrete class that implements the shape interface. A factory class shapefactory is defined as the next step.
Create an interface: Shape.java
Public interface Shape {
void Draw ();
}
Consider two implementations of this shape interface: Circle.java & Rectangle.java
public class Rectangle implements the Shape {
@Override public
Void Draw () {
System.out.println ("Inside Rectangle::d Raw () method. ");
}
The public class Circle implements the Shape {
@Override public
Void Draw () {
System.out.println ("Inside Circle:: Draw () method. ");
}
}
Create an object that generates a specific class from the given information.
public class Shapefactory {
//use Getshape method to get object of type shape public
shape Getshape (String shape Type) {
if (ShapeType = null) {return
null;
}
if (Shapetype.equalsignorecase ("CIRCLE")) {return
new CIRCLE ();
} else if (Shapetype.equalsignorecase (" RECTANGLE ")) {return
new RECTANGLE ();
}
return null;
}
}
Use factory to get objects of a specific class by passing information of type: Factorypatterndemo.java
public class Factorypatterndemo {public
static void Main (string[] args) {
shapefactory shapefactory = new Shapefa Ctory ();
Get an object of Circle and call it draw method.
Shape shape1 = Shapefactory.getshape ("CIRCLE");
Call draw method of Circle
Shape1.draw ();
Get an object of Rectangle and call it draw method.
Shape shape2 = Shapefactory.getshape ("RECTANGLE");
Call draw method of Rectangle
Shape2.draw ();
}
Verify output:
Inside Circle::d Raw () method.
Inside Rectangle::d Raw () method.
Factory mode: Refactoring using lambda expressions
In Java 8, we can refer to constructors, just as we refer to methods by using the
Method Reference. For example, here's how to reference the Circle constructor:
supplier<shape> circlesupplier = circle::new;
Circle Circle = Circlesupplier.get ();
With this technique, we can rewrite the previous code by creating a map of the mapped shape
Name to its constructor:
Final static map<string, supplier<shape>> Map = new hashmap<> ();
static {
Map.put ("CIRCLE", circle::new);
Map.put ("RECTANGLE", rectangle::new);
}
We can now use this map to instantiate different shapes, just like our previous factory code:
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 ());
}
Use a factory to get objects of specific classes, using lambda expressions: 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 ();
}
Verify output:
Inside Circle::d Raw () method.
Inside Rectangle::d Raw () method.
This is a pretty neat way to use the Java 8 feature to implement the same intent factory pattern. However, this technique does not scale well if the factory method Getshape need to take multiple parameters to pass to the shape constructor. You must provide a different functional interface with a simple supplier.