类图
image.png
示例代码:
image.png
import com.zhs.design.builder.demo1.Player;
--------------------------------------------------------------
public abstract class PlayerBuilder {
private Player player = new Player();
public abstract void buildMenu();
public abstract void buildPlayList();
public abstract void buildMainWindow();
public abstract void buildProgressBar();
public Player getPlayer() {
return player;
}
}
--------------------------------------------------------------
public class CompletePlayerBuilder extends PlayerBuilder {
@Override
public void buildMenu() {
this.getPlayer().setMenu("完整模式菜单");
}
@Override
public void buildPlayList() {
this.getPlayer().setPlayList("完整模式播放列表");
}
@Override
public void buildMainWindow() {
this.getPlayer().setMainWindow("完整模式主窗口");
}
@Override
public void buildProgressBar() {
this.getPlayer().setProgressBar("完整模式进度条");
}
}
--------------------------------------------------------------
public class MemoryPlayerBuilder extends PlayerBuilder {
@Override
public void buildMenu() {
this.getPlayer().setMenu("记忆模式菜单");
}
@Override
public void buildPlayList() {
this.getPlayer().setPlayList("记忆模式播放列表");
}
@Override
public void buildMainWindow() {
this.getPlayer().setMainWindow("记忆模式主窗口");
}
@Override
public void buildProgressBar() {
this.getPlayer().setProgressBar("记忆模式进度条");
}
}
--------------------------------------------------------------
public class SimplePlayerBuilder extends PlayerBuilder {
@Override
public void buildMenu() {
this.getPlayer().setMenu("精简模式菜单");
}
@Override
public void buildPlayList() {
this.getPlayer().setPlayList("精简模式播放列表");
}
@Override
public void buildMainWindow() {
this.getPlayer().setMainWindow("精简模式主窗口");
}
@Override
public void buildProgressBar() {
this.getPlayer().setProgressBar("精简模式进度条");
}
}
import lombok.Data;
@Data
public class Player {
private String menu;
private String playList;
private String mainWindow;
private String progressBar;
}
public class PlayerDirector {
public Player concrete(PlayerBuilder playerBuilder){
playerBuilder.buildMenu();
playerBuilder.buildPlayList();
playerBuilder.buildMainWindow();
playerBuilder.buildProgressBar();
return playerBuilder.getPlayer();
}
}
public class Main {
/*
* Sunny软件公司欲开发一个视频播放软件,为了给用户使用提供方便,该播放软件提供多
种界面显示模式,如完整模式、精简模式、记忆模式、网络模式等。在不同的显示模式
下主界面的组成元素有所差异,如在完整模式下将显示菜单、播放列表、主窗口、控制
条等,在精简模式下只显示主窗口和控制条,而在记忆模式下将显示主窗口、控制条、
收藏列表等。尝试使用建造者模式设计该软件。*/
public static void main(String[] args) {
PlayerDirector playerDirector = new PlayerDirector();
Player player = playerDirector.concrete(new MemoryPlayerBuilder());
System.out.println(player.toString());
}
}
特殊建造者模式(构建器)
public class People {
private String color;
private Integer old;
private People(Builder builder) {
this.color = builder.color;
this.old = builder.old;
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String color;
private Integer old;
public Builder color(String color) {
this.color = color;
return this;
}
public Builder old(Integer old) {
this.old = old;
return this;
}
public People build() {
return new People(this);
}
}
@Override
public String toString() {
return "People{" +
"color='" + color + '\'' +
", old=" + old +
'}';
}
}
public class Main {
public static void main(String[] args) {
People people = People.builder().color("red").old(2).build();
System.out.println(people.toString());
}
}
网友评论