Refactoring Design Pattern with new features in Java 8

Tram Ho

In this article, I will show you how to use some new features in Java 8 such as Lambda , Function , Supplier , etc. to refactor the code of some Design Pattern .

Refactoring Strategy Design Pattern

What is strategy pattern?

You review the article ” Tutorial Java Design Pattern – Strategy “.

For example Strategy Pattern

Strategy.java

Strategy Pattern does not use Lambda

StartegyPatternExample.java

Strategy Pattern uses Lambda

Refactoring Observer Design Pattern

What is the Observer Pattern?

Have you reviewed the article ” Tutorial Java Design Pattern – Observer “.

Example Observer Pattern

Observer.java

Subject.java

AccountService.java

Observer Pattern does not use Lambda

Observer Pattern uses Lambda

Running the above 2 programs, we get the same results:

Refactoring Chain of Responsibility Pattern

What is the Chain of Responsibility Pattern?

Have you reviewed the article ” Tutorial Java Design Pattern – Chain of Responsibility “.

Example of Chain of Responsibility Pattern

Filter.java

Chain of Responsibility Pattern does not use Lambda

Chain of Responsibility Pattern uses Lambda and Function

Note: UnaryOperator is a Function, has the same input and output data types. UnaryOperator <String> is equivalent to writing Function <String, String>.

Running the above 2 programs, we have the same result:

Refactoring Factory Method Design Pattern

What is the Factory Method Pattern?

Have you reviewed the article ” Tutorial Java Design Pattern – Factory Method “.

Example Factory Pattern

Bank.java

TPBank.java

VietcomBank.java

BankType.java

Factory Method Pattern does not use Java 8

Factory Method Pattern uses Supplier and Method Reference

Java 8 brings us a lot of utilities, you should try refactor your code to Java 8 to make the code cleaner.

Original article link: https://gpcoder.com/6200-refactoring-design-pattern-voi-tinh-nang-moi-rong-java-8/

Share the news now

Source : Viblo