设计模式之建造者模式
Posted juncaif
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式之建造者模式相关的知识,希望对你有一定的参考价值。
前言
建造者模式也是常用的设计模式,Lombok的@Builder就是一个典型的应用。
google guava cache创建cache就只能通过builder方式
建造者模式
建造者模式有四个角色: 1product 2抽象builder 3实现builder 4 director
建造者模式是的创建对象算法独立,调用者可以不可见对象创建的逻辑。
Lombok @Builder
Lombok @Builder通过Builder静态内部类的方式,使用Builder创建对象。
@Builder是一个无序的创建过程
import lombok.Builder;
import lombok.Data;
@Builder
@Data
public class Person1 {
String name;
int age;
}
/* 编译后再反编译
public class Person1 {
String name;
int age;
Person1(final String name, final int age) {
this.name = name;
this.age = age;
}
public static Person1.Person1Builder builder() {
return new Person1.Person1Builder();
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public void setName(final String name) {
this.name = name;
}
public void setAge(final int age) {
this.age = age;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Person1)) {
return false;
} else {
Person1 other = (Person1)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$name = this.getName();
Object other$name = other.getName();
if (this$name == null) {
if (other$name == null) {
return this.getAge() == other.getAge();
}
} else if (this$name.equals(other$name)) {
return this.getAge() == other.getAge();
}
return false;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof Person1;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $name = this.getName();
int result = result * 59 + ($name == null ? 43 : $name.hashCode());
result = result * 59 + this.getAge();
return result;
}
public String toString() {
return "Person1(name=" + this.getName() + ", age=" + this.getAge() + ")";
}
public static class Person1Builder {
private String name;
private int age;
Person1Builder() {
}
public Person1.Person1Builder name(final String name) {
this.name = name;
return this;
}
public Person1.Person1Builder age(final int age) {
this.age = age;
return this;
}
public Person1 build() {
return new Person1(this.name, this.age);
}
public String toString() {
return "Person1.Person1Builder(name=" + this.name + ", age=" + this.age + ")";
}
}
}
public class BuilderTest01 {
public static void main(String[] args) {
test01();
}
//测试Lombok @Builder使用静态内部类一步步建造对象
public static void test01(){
Person1 person1 = Person1.builder().name("jack").age(10).build();
System.out.println(person1.toString());
}
}
完整的建造者代码
实现四个角色:1product 2抽象builder 3实现builder 4director
product
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class House {
private String buleprint;
private String partA;
private String partB;
public House(String buleprint, String partA, String partB) {
this.buleprint =buleprint;
this.partA = partA;
this.partB = partB;
}
public static HouseBuiler createBuilder(){
return new HouseBuiler();
}
/* public String toString() {
return "House(buleprint=" + this.getBuleprint() + ", partA=" + this.getPartA() + ", partB=" + this.getPartB() + ")";
}*/
//public属性
@ToString
public static class HouseBuiler extends Builder {
private String buleprint;
private String partA;
private String partB;
@Override
public House build(){
return new House(this.buleprint,this.partA,this.partB);
}
@Override
public HouseBuiler buildPartA(String partA){
this.partA = partA;
return this;
}
@Override
public HouseBuiler buildPartB(String partB){
this.partB = partB;
return this;
}
@Override
public HouseBuiler buildBuleprint(String buleprint){
this.buleprint = buleprint;
return this;
}
/* public String toString() {
return "House.HouseBuiler(buleprint=" + this.buleprint + ", partA=" + this.partA + ", partB=" + this.partB + ")";
}*/
}
}
抽象builder
package com.learn.java.javabase.designpattern.builder;
public abstract class Builder {
public abstract Builder buildPartA(String partA);
public abstract Builder buildPartB(String part);
public abstract Builder buildBuleprint(String buleprint);
public abstract House build();
}
builder实现类
package com.learn.java.javabase.designpattern.builder;
public class ConcreteBuilder extends Builder {
private String buleprint;
private String partA;
private String partB;
@Override
public House build(){
return new House(this.buleprint,this.partA,this.partB);
}
@Override
public ConcreteBuilder buildPartA(String partA){
this.partA = partA;
return this;
}
@Override
public ConcreteBuilder buildPartB(String partB){
this.partB = partB;
return this;
}
@Override
public ConcreteBuilder buildBuleprint(String buleprint){
this.buleprint = buleprint;
return this;
}
}
director
package com.learn.java.javabase.designpattern.builder;
public class Director {
public static House build(Builder builder){
return builder.buildBuleprint("buluptint").buildPartA("part a").buildPartB("part b").build();
}
}
测试类
public class BuilderTest02 {
public static void main(String[] args) {
test02();
}
public static void test01(){
//无序调用,使用者其实拥有Director角色
House house = House.createBuilder().buildBuleprint("蓝图").buildPartA("A部分").buildPartB("B部分").build();
System.out.println(house.toString());
}
public static void test02(){
//调用方不可见创建算法
House house = Director.build(new ConcreteBuilder());
System.out.println(house.toString());
}
}
以上是关于设计模式之建造者模式的主要内容,如果未能解决你的问题,请参考以下文章