//接口
public interface Build {
public void buildTop(String name);
public void buildLeft(String name);
public void buildMain(String name);
public void buildBootom(String name);
public PageBuild getPageBuild();
}
//工具类
public class PageBuild {
//缺少成员属性的思维
private String top;
private String left;
private String main;
private String bottom;
public void buildTop(String location) {
this.top = location;
System.out.println(location);
}
public void buildLeft(String location) {
this.left = location;
System.out.println(location);
}
public void buildMain(String location) {
this.main = location;
System.out.println(location);
}
public void buildBottom(String location) {
this.bottom = location;
System.out.println(location);
}
}//接口+工具类对象
public class H5Builder implements Build{
private PageBuild pageBuilder;
public H5Builder() {
pageBuilder = new PageBuild();
}
public H5Builder(PageBuild pageBuilder) {
// TODO Auto-generated constructor stub
this.pageBuilder = pageBuilder;
}
@Override
public void buildTop(String name) {
// TODO Auto-generated method stub
//System.out.println("h5 构建 顶部");
pageBuilder.buildTop(name);
}@Override
public void buildLeft(String name) {
// TODO Auto-generated method stub
//System.out.println("h5 构建 左部");
pageBuilder.buildLeft(name);
}@Override
public void buildMain(String name) {
// TODO Auto-generated method stub
//System.out.println("h5 构建 主部分");
pageBuilder.buildMain(name);
}@Override
public void buildBootom(String name) {
// TODO Auto-generated method stub
//System.out.println("h5 构建 底部");
pageBuilder.buildBottom(name);
}
@Override
public PageBuild getPageBuild() {
// TODO Auto-generated method stub
return pageBuilder;
}}
//
//代码重复 sout
public class VueBuilder implements Build{
private PageBuild pageBuilder;
public VueBuilder() {
// TODO Auto-generated constructor stub
this.pageBuilder = new PageBuild();
}
public VueBuilder(PageBuild pageBuilder) {
// TODO Auto-generated constructor stub
this.pageBuilder = pageBuilder;
}
@Override
public void buildTop(String name) {
// TODO Auto-generated method stub
//System.out.println("Vue buld Top");
pageBuilder.buildTop(name);
}@Override
public void buildLeft(String name) {
// TODO Auto-generated method stub
//System.out.println("Vue buld Left");
pageBuilder.buildLeft(name);
}@Override
public void buildMain(String name) {
// TODO Auto-generated method stub
// System.out.println("Vue buld Main");
pageBuilder.buildMain(name);
}@Override
public void buildBootom(String name) {
// TODO Auto-generated method stub
// System.out.println("Vue buld Right");
pageBuilder.buildBottom(name);
}@Override
public PageBuild getPageBuild() {
// TODO Auto-generated method stub
return pageBuilder;
}}
//
public class FactoryBuilder {
private Build build;//默认无参构造 html5
public FactoryBuilder() { this.build = new H5Builder(); }
//当有多个实现类的时候传参
public FactoryBuilder(Build build) {
// TODO Auto-generated constructor stub
this.build = build;
}
public void build() {
build.buildTop("nav");
build.buildLeft("index");
build.buildMain("body");
build.buildBootom("footer");
}
}
//测试
public class Dcemo {
public static void main(String[] args) {
//当应用中返回或者生成PageBuilder,调用有参---服务于需求!!
H5Builder hBuilder = new H5Builder();
FactoryBuilder fBuilder = new FactoryBuilder(hBuilder);
fBuilder.build();
}
}