建造者模式(Builder Pattern),将一个复杂对象的构建与他的表示分离,是的同样的构造过程可以创建不同的实例表示。这样用户只需要根据自己的业务要求,指定建造的类型就可以得到对应的实例,不需要关注建造的过程和细节。适合于一些基本部件不变,而其组合经常变化的时候,不适合构造类很多的场景。
一、需求举例:
1.构造小的团队,包含一个高级工程师,一个产品经理和3个初级工程师;
1.构造大的团队,包含一个高级工程师,3个产品经理和10个初级工程师。
二、类图:
三、代码实战:
1.工程师父接口:Engineer
package com.lance.builder;
public interface Engineer {
default void develop(String program) {
System.out.println("Engineer default refuse " + program);
}
}
2.初级工程师类:JuniorEngineer
package com.lance.builder;
public class JuniorEngineer implements
Engineer {
@Override
public void develop(String program) {
System.out.println("Junior Engineer write code for " + program);
}
}
3.高级工程师类:SeniorEngineer
package com.lance.builder;
public class SeniorEngineer implements Engineer {
@Override
public void develop(String program) {
System.out.println("Senior Engineer develop " + program);
}
}
4.产品经理类:ProductManager
package com.lance.builder;
public class ProductManager {
public void plan(String program) {
System.out.println("ProductManager plan " + program);
}
}
5.团队类:Team
package com.lance.builder;
import java.util.List;
public class Team {
private SeniorEngineer leader;
private List<ProductManager> productManagers;
private List<Engineer> engineers;
public SeniorEngineer getLeader() {
return leader;
}
public void setLeader(SeniorEngineer leader) {
this.leader = leader;
}
public List<ProductManager> getProductManagers() {
return productManagers;
}
public void setProductManagers(List<ProductManager> productManagers) {
this.productManagers = productManagers;
}
public List<Engineer> getEngineers() {
return engineers;
}
public void setEngineers(List<Engineer> engineers) {
this.engineers = engineers;
}
}
6.团队构造者接口类:TeamBuilder
package com.lance.builder;
public interface TeamBuilder {
void builderLeader();
void builderEngineer();
void builderProductManager();
void run(String program);
}
7.大团队构造者类:BigTeamBuilder
package com.lance.builder;
import java.util.ArrayList;
import java.util.List;
public class BigTeamBuilder implements TeamBuilder {
private Team team = new Team();
@Override
public void run(String program) {
for (ProductManager productManager : team.getProductManagers()) {
productManager.plan(program);
}
team.getLeader().develop(program);
for (Engineer engineer : team.getEngineers()) {
engineer.develop(program);
}
}
@Override
public void builderLeader() {
if (team.getLeader() == null) {
team.setLeader(new SeniorEngineer());
}
}
@Override
public void builderEngineer() {
for (int i = 0; i < 10; i++) {
if (team.getEngineers() == null) {
Engineer engineer = new JuniorEngineer();
List<Engineer> engineers = new ArrayList();
engineers.add(engineer);
team.setEngineers(engineers);
} else {
team.getEngineers().add(new JuniorEngineer());
}
}
}
@Override
public void builderProductManager() {
for (int i = 0; i < 3; i++) {
ProductManager productManager = new ProductManager();
List<ProductManager> productManagers = new ArrayList();
productManagers.add(productManager);
team.setProductManagers(productManagers);
}
}
}
8.小团队构造者类:SmallTeamBuilder
package com.lance.builder;
import java.util.ArrayList;
import java.util.List;
public class SmallTeamBuilder implements TeamBuilder {
private Team team = new Team();
@Override
public void run(String program) {
for(ProductManager productManager:team.getProductManagers()) {
productManager.plan(program);
}
team.getLeader().develop(program);
for(Engineer engineer:team.getEngineers()) {
engineer.develop(program);
}
}
@Override
public void builderLeader() {
if(team.getLeader() == null) {
team.setLeader(new SeniorEngineer());
}
}
@Override
public void builderEngineer() {
for (int i = 0; i < 3; i++) {
if (team.getEngineers() == null) {
Engineer engineer = new JuniorEngineer();
List<Engineer> engineers = new ArrayList();
engineers.add(engineer);
team.setEngineers(engineers);
} else {
team.getEngineers().add(new JuniorEngineer());
}
}
}
@Override
public void builderProductManager() {
if(team.getProductManagers() == null) {
ProductManager productManager = new ProductManager();
List<ProductManager> productManagers = new ArrayList();
productManagers.add(productManager);
team.setProductManagers(productManagers);
} else {
team.getProductManagers().add(new ProductManager());
}
}
}
9.构造指挥者类:TeamBuilderDirector
package com.lance.builder;
public class TeamBuilderDirector {
public void construct(TeamBuilder teamBuilder) {
teamBuilder.builderEngineer();
teamBuilder.builderLeader();
teamBuilder.builderProductManager();
}
}
10.主类:BuilderPattern
package com.lance.builder;
public class BuilderPattern {
public static void main(String[] args) {
System.out.println("==========start==========");
TeamBuilderDirector teamBuilderDirector = new TeamBuilderDirector();
TeamBuilder smallTeamBuilder = new SmallTeamBuilder();
teamBuilderDirector.construct(smallTeamBuilder);
smallTeamBuilder.run("[small program]");
System.out.println("==========================");
TeamBuilder bigTeamBuilder = new BigTeamBuilder();
teamBuilderDirector.construct(bigTeamBuilder);
bigTeamBuilder.run("[big program]");
System.out.println("==========end==========");
}
}
输出结果:
==========start==========
ProductManager plan [small program]
Senior Engineer develop [small program]
Junior Engineer write code for [small program]
Junior Engineer write code for [small program]
Junior Engineer write code for [small program]
==========================
ProductManager plan [big program]
Senior Engineer develop [big program]
Junior Engineer write code for [big program]
Junior Engineer write code for [big program]
Junior Engineer write code for [big program]
Junior Engineer write code for [big program]
Junior Engineer write code for [big program]
Junior Engineer write code for [big program]
Junior Engineer write code for [big program]
Junior Engineer write code for [big program]
Junior Engineer write code for [big program]
Junior Engineer write code for [big program]
==========end==========
网友评论