前序:
相关引读
单例设计模式&工厂方法模式
传送门:https://www.cnblogs.com/powerZhangFly/p/13837675.html
模板方法模式&观察者模式&责任链模式
传送门:https://www.cnblogs.com/powerZhangFly/p/13800875.html
正文:
抽象工厂模式&建造者模式
抽象工厂模式
模式定义: 提供一个创建一系列相关或互相依赖对象的接口,而无需指定它们具体的类代码示例:
1 package com.zhangjiang.designpattern.abstractfactory; 2 3 /** 4 * 5 */ 6 public class AbstractFactoryTest { 7 public static void main(String[] args) { 8 IDatabaseUtils iDatabaseUtils=new OracleDataBaseUtils(); // 9 IConnection connection=iDatabaseUtils.getConnection(); 10 connection.connect(); 11 ICommand command=iDatabaseUtils.getCommand(); 12 command.command(); 13 14 15 } 16 } 17 18 // 变化: mysql , oracle. ... 19 // connection , command , 20 21 interface IConnection{ 22 void connect(); 23 } 24 interface ICommand{ 25 void command(); 26 } 27 interface IDatabaseUtils{ 28 IConnection getConnection(); 29 ICommand getCommand(); 30 } 31 class MysqlConnection implements IConnection{ 32 33 @Override 34 public void connect() { 35 System.out.println("mysql connected."); 36 } 37 } 38 class OracleConnection implements IConnection{ 39 40 @Override 41 public void connect() { 42 System.out.println("oracle connected."); 43 } 44 } 45 46 47 class MysqlCommand implements ICommand{ 48 49 @Override 50 public void command() { 51 System.out.println(" mysql command. "); 52 } 53 } 54 55 class OracleCommand implements ICommand{ 56 57 @Override 58 public void command() { 59 System.out.println("oracle command."); 60 } 61 } 62 63 class MysqlDataBaseUtils implements IDatabaseUtils{ 64 65 @Override 66 public IConnection getConnection() { 67 return new MysqlConnection(); 68 } 69 70 @Override 71 public ICommand getCommand() { 72 return new MysqlCommand(); 73 } 74 } 75 76 class OracleDataBaseUtils implements IDatabaseUtils{ 77 78 @Override80 public IConnection getConnection() { 79 return new OracleConnection(); 80 } 81 82 @Override 83 public ICommand getCommand() { 84 return new OracleCommand(); 85 } 86 }View Code 应用场景: 程序需要处理不同系列的相关产品,但是您不希望它依赖于这些产品的 具体类时, 可以使用抽象工厂 优点: 1.可以确信你从工厂得到的产品彼此是兼容的。 2.可以避免具体产品和客户端代码之间的紧密耦合。 3.符合单一职责原则 4.符合开闭原则 JDK源码中的应用: java.sql.Connection java.sql.Driver
建造者模式
模式定义: 将一个复杂对象的创建与他的表示分离,使得同样的构建过程可以创建不同的表示
建造者模式实例 :
1 package com.zhangjiang.designpattern.builder; 2 /** 3 * 4 */ 5 public class BuilderTest { 6 public static void main(String[] args) { 7 // Product product=new Product( ); 8 // product.setCompanyName( "xxx" ); 9 // product.setPart1( "xxx" ); 10 // // ... 11 // 12 // // .... 13 // // .... 14 // 15 16 ProductBuilder specialConcreteProductBuilder=new SpecialConcreteProductB 17 uilder(); 18 Director director=new Director(specialConcreteProductBuilder); 19 Product product=director.makeProduct( "productNamexxx", "cn...", 20 "part1", "part2", "part3", "part4" ); 21 System.out.println(product); 22 23 24 } 25 } 26 27 interface ProductBuilder{ 28 void builderProductName(String productName); 29 void builderCompanyName(String companyName); 30 void builderPart1(String part1); 31 void builderPart2(String part2); 32 void builderPart3(String part3); 33 void builderPart4(String part4); 34 35 Product build(); 36 } 37 38 class DefaultConcreteProductBuilder implements ProductBuilder{ 39 private String productName; 40 private String companyName; 41 private String part1;42 private String part2; 42 private String part3; 43 private String part4; 44 @Override 45 public void builderProductName(String productName) { 46 this.productName=productName; 47 } 48 49 @Override 50 public void builderCompanyName(String companyName) { 51 this.companyName=companyName; 52 } 53 54 @Override 55 public void builderPart1(String part1) { 56 this.part1=part1; 57 } 58 59 @Override 60 public void builderPart2(String part2) { 61 this.part2=part2; 62 } 63 64 @Override 65 public void builderPart3(String part3) { 66 this.part3=part3; 67 } 68 69 @Override 70 public void builderPart4(String part4) { 71 this.part4=part4; 72 } 73 74 @Override 75 public Product build() { 76 return new Product( this.productName,this.companyName,this.part1,this.pa 77 rt2,this.part3,this.part4 ); 78 } 79 } 80 81 class SpecialConcreteProductBuilder implements ProductBuilder{82 private String productName; 82 private String companyName; 83 private String part1; 84 private String part2; 85 private String part3; 86 private String part4; 87 @Override 88 public void builderProductName(String productName) { 89 this.productName=productName; 90 } 91 92 @Override 93 public void builderCompanyName(String companyName) { 94 this.companyName=companyName; 95 } 96 97 @Override 98 public void builderPart1(String part1) { 99 this.part1=part1; 100 } 101 102 @Override 103 public void builderPart2(String part2) { 104 this.part2=part2; 105 } 106 107 @Override 108 public void builderPart3(String part3) { 109 this.part3=part3; 110 } 111 112 @Override 113 public void builderPart4(String part4) { 114 this.part4=part4; 115 } 116 117 @Override 118 public Product build() { 119 return new Product( this.productName,this.companyName,this.part1,this.p 120 art2,this.part3,this.part4 ); 121 } 122 } 123 class Director{ 124 125 private ProductBuilder builder; 126 127 public Director(ProductBuilder builder) { 128 this.builder=builder; 129 } 130 131 public Product makeProduct(String productName, String companyName, Stri 132 ng part1, String part2, String part3, String part4){ 133 134 builder.builderProductName(productName ); 135 builder.builderCompanyName(companyName ); 136 builder.builderPart1(part1 ); 137 138 builder.builderPart2(part2 ); 139 builder.builderPart3(part3 ); 140 builder.builderPart4(part4 ); 141 142 Product product=builder.build(); 143 return product; 144 145 } 146 147 148 } 149 150 class Product { 151 152 private String productName; 153 private String companyName; 154 private String part1; 155 private String part2; 156 private String part3; 157 private String part4; 158 // ...... 159 160 161 public Product() { 162 } 163 public Product(String productName, String companyName, String part1, St 164 ring part2, String part3, String part4) { 165 this.productName=productName; 166 this.companyName=companyName; 167 this.part1=part1; 168 this.part2=part2; 169 this.part3=part3; 170 this.part4=part4; 171 } 172 173 public String getProductName() { 174 return productName; 175 } 176 177 public void setProductName(String productName) { 178 this.productName=productName; 179 } 180 181 public String getCompanyName() { 182 return companyName; 183 } 184 185 public void setCompanyName(String companyName) { 186 this.companyName=companyName; 187 } 188 189 public String getPart1() { 190 return part1; 191 } 192 193 public void setPart1(String part1) { 194 this.part1=part1; 195 } 196 197 public String getPart2() { 198 return part2; 199 } 200 201 public void setPart2(String part2) { 202 this.part2=part2; 203 } 204 205 public String getPart3() { 206 return part3; 207 } 208 209 public void setPart3(String part3) { 210 this.part3=part3; 211 } 212 213 public String getPart4() { 214 return part4; 215 } 216 217 public void setPart4(String part4) { 218 this.part4=part4; 219 } 220 221 @Override 222 public String toString() { 223 return "Product{" + 224 "productName='" + productName + '\'' + 225 ", companyName='" + companyName + '\'' + 226 ", part1='" + part1 + '\'' + 227 ", part2='" + part2 + '\'' + 228 ", part3='" + part3 + '\'' + 229 ", part4='" + part4 + '\'' + 230 '}'; 231 } 232 }View Code 建造者模式与不可变对象配合使用
1 public class ProductTest2 { 2 public static void main(String[] args) { 3 Product.Builder builder=new Product.Builder().productName( "xxxx" ).compa 4 nyName( "xxxxxx" ).part1( "xxxxx" ).part2( "xxxx" ); 5 6 // 7 builder.part3( "part3" ); 8 Product product=builder.build(); 9 System.out.println(product); 10 } 11 12 } 13 class Product { 14 15 private final String productName; 16 private final String companyName; 17 private final String part1; 18 private final String part2; 19 private final String part3; 20 private final String part4; 21 // ...... 22 23 24 25 26 public Product(String productName, String companyName, String part1, Str 27 ing part2, String part3, String part4) { 28 this.productName=productName; 29 this.companyName=companyName; 30 this.part1=part1; 31 this.part2=part2; 32 this.part3=part3; 33 this.part4=part4; 34 } 35 36 37 38 static class Builder{ 39 40 private String productName; 41 private String companyName; 42 private String part1; 43 private String part2; 44 private String part3; 45 private String part4; 46 47 48 public Builder productName(String productName){ 49 this.productName=productName;49 return this; 50 } 51 public Builder companyName(String companyName){ 52 this.companyName=companyName; 53 return this; 54 } 55 public Builder part1(String part1){ 56 this.part1=part1; 57 return this; 58 } 59 public Builder part2(String part2){ 60 this.part2=part2; 61 return this; 62 } 63 public Builder part3(String part3){ 64 this.part3=part3; 65 return this; 66 } 67 public Builder part4(String part4){ 68 this.part4=part4; 69 return this; 70 } 71 72 Product build(){ 73 // 74 Product product=new Product( this.productName, this.companyName, this.pa 75 rt1, this.part2, this.part3, this.part4 ); 76 return product; 77 } 78 79 80 } 81 82 83 @Override 84 public String toString() { 85 return "Product{" + 86 "productName='" + productName + '\'' + 87 ", companyName='" + companyName + '\'' + 88 ", part1='" + part1 + '\'' + 89 ", part2='" + part2 + '\'' +89 ", part3='" + part3 + '\'' + 90 ", part4='" + part4 + '\'' + 91 '}'; 92 } 93 }View Code 应用场景 1.需要生成的对象具有复杂的内部结构 2.需要生成的对象内部属性本身相互依赖 3.与不可变对象配合使用 优点: 1、建造者独立,易扩展。 2、便于控制细节风险。 Spring源码中的应用 org.springframework.web.servlet.mvc.method.RequestMappingInfo org.springframework.beans.factory.support.BeanDefinitionBuilder