feat(hw-03): implement Builder pattern
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
package org.example;
|
package org.example;
|
||||||
|
|
||||||
import org.example.adapter.*;
|
import org.example.adapter.*;
|
||||||
|
import org.example.builder.*;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@@ -12,6 +13,7 @@ public class App {
|
|||||||
|
|
||||||
private static void run() {
|
private static void run() {
|
||||||
checkAdapterPattern();
|
checkAdapterPattern();
|
||||||
|
checkBuilderPattern();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void checkAdapterPattern() {
|
private static void checkAdapterPattern() {
|
||||||
@@ -20,4 +22,11 @@ public class App {
|
|||||||
|
|
||||||
target.methodWithoutArguments();
|
target.methodWithoutArguments();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void checkBuilderPattern() {
|
||||||
|
Product product =
|
||||||
|
new ProductBuilder().setFirst("first").setSecond("second").setThird(3).build();
|
||||||
|
|
||||||
|
System.out.println(product);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package org.example.builder;
|
||||||
|
|
||||||
|
/** Product */
|
||||||
|
public class Product {
|
||||||
|
private final String first;
|
||||||
|
private final String second;
|
||||||
|
private final int third;
|
||||||
|
|
||||||
|
public Product(String first, String second, int third) {
|
||||||
|
this.first = first;
|
||||||
|
this.second = second;
|
||||||
|
this.third = third;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirst() {
|
||||||
|
return first;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSecond() {
|
||||||
|
return second;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getThird() {
|
||||||
|
return third;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Product{"
|
||||||
|
+ "first='"
|
||||||
|
+ first
|
||||||
|
+ '\''
|
||||||
|
+ ", second='"
|
||||||
|
+ second
|
||||||
|
+ '\''
|
||||||
|
+ ", third="
|
||||||
|
+ third
|
||||||
|
+ '}';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package org.example.builder;
|
||||||
|
|
||||||
|
/** Builder */
|
||||||
|
public class ProductBuilder {
|
||||||
|
private String first;
|
||||||
|
private String second;
|
||||||
|
private int third;
|
||||||
|
|
||||||
|
public ProductBuilder setFirst(String first) {
|
||||||
|
this.first = first;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProductBuilder setSecond(String second) {
|
||||||
|
this.second = second;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ProductBuilder setThird(int third) {
|
||||||
|
this.third = third;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Product build() {
|
||||||
|
return new Product(first, second, third);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user