-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
547 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
由光头强引发的Java23种设计模式的思考: | ||
|
||
代码|详细文章讲解 | ||
----|---- | ||
[](src/com/lzw/part1_creation_mode/builder)| [Java设计模式4之建造者模式(光头强买电锯引发的思考)](https://www.jianshu.com/p/713120e9dc59) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
结构型模式分为以下 5 种(有的版也把简单工厂模式算在里面,就是6种): | ||
|
||
单例(Singleton)模式:某个类只能生成一个实例,该类提供了一个全局访问点供外部获取该实例,其拓展是有限多例模式。 | ||
原型(Prototype)模式:将一个对象作为原型,通过对其进行复制而克隆出多个和原型类似的新实例。 | ||
工厂方法(FactoryMethod)模式:定义一个用于创建产品的接口,由子类决定生产什么产品。 | ||
抽象工厂(AbstractFactory)模式:提供一个创建产品族的接口,其每个子类可以生产一系列相关的产品。 | ||
建造者(Builder)模式:将一个复杂对象分解成多个相对简单的部分,然后根据不同需要分别创建它们,最后构建成该复杂对象。 |
28 changes: 28 additions & 0 deletions
28
design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/ChainsawBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.lzw.part1_creation_mode.builder.chainsaw; | ||
|
||
/** | ||
* 链锯建筑者 | ||
*/ | ||
public interface ChainsawBuilder { | ||
|
||
// 蓄电池 | ||
void battery(); | ||
// 电动机 | ||
void motor(); | ||
// 减速箱 | ||
void reductionGearbox(); | ||
// 防护罩 | ||
void hood(); | ||
// 手柄 | ||
void handle(); | ||
// 开关 | ||
void switches(); | ||
// 插头 | ||
void plug(); | ||
// 圆锯片 | ||
void circularSaw(); | ||
|
||
// 获取链锯(产品)的实例 | ||
Chainsaws getChainsaws(); | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/ChainsawBuilderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.lzw.part1_creation_mode.builder.chainsaw; | ||
|
||
/** | ||
* 链锯建筑者(具体建筑者) | ||
*/ | ||
public class ChainsawBuilderImpl implements ChainsawBuilder{ | ||
|
||
private Chainsaws chainsaws; | ||
|
||
public ChainsawBuilderImpl() { | ||
chainsaws = new Chainsaws(); | ||
} | ||
|
||
|
||
@Override | ||
public void battery() { | ||
chainsaws.setBattery("普通锂电池"); | ||
} | ||
|
||
@Override | ||
public void motor() { | ||
chainsaws.setMotor("普通铝电动机"); | ||
} | ||
|
||
@Override | ||
public void reductionGearbox() { | ||
chainsaws.setReductionGearbox("普通减速箱"); | ||
} | ||
|
||
@Override | ||
public void hood() { | ||
chainsaws.setHood("普通PVC塑料"); | ||
} | ||
|
||
@Override | ||
public void handle() { | ||
chainsaws.setHandle("普通PVC塑料"); | ||
} | ||
|
||
@Override | ||
public void switches() { | ||
chainsaws.setSwitches("普通拉线开关"); | ||
} | ||
|
||
@Override | ||
public void plug() { | ||
chainsaws.setPlug("普通三口插头"); | ||
} | ||
|
||
@Override | ||
public void circularSaw() { | ||
chainsaws.setCircularSaw("普通钢锯片"); | ||
} | ||
|
||
@Override | ||
public Chainsaws getChainsaws() { | ||
return chainsaws; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/ChainsawBuilderImpl2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.lzw.part1_creation_mode.builder.chainsaw; | ||
|
||
/** | ||
* 链锯建筑者(具体建筑者) | ||
*/ | ||
public class ChainsawBuilderImpl2 implements ChainsawBuilder{ | ||
|
||
private Chainsaws chainsaws; | ||
|
||
public ChainsawBuilderImpl2() { | ||
chainsaws = new Chainsaws(); | ||
} | ||
|
||
|
||
@Override | ||
public void battery() { | ||
chainsaws.setBattery("环保大容量锂电池"); | ||
} | ||
|
||
@Override | ||
public void motor() { | ||
chainsaws.setMotor("动力强耐高温散热好的纯铜电机"); | ||
} | ||
|
||
@Override | ||
public void reductionGearbox() { | ||
chainsaws.setReductionGearbox("减震变频高功率变速箱"); | ||
} | ||
|
||
@Override | ||
public void hood() { | ||
chainsaws.setHood("加厚加固耐磨抗打击防护罩"); | ||
} | ||
|
||
@Override | ||
public void handle() { | ||
chainsaws.setHandle("人体工体学包胶手柄"); | ||
} | ||
|
||
@Override | ||
public void switches() { | ||
chainsaws.setSwitches("调速防误触智能开关"); | ||
} | ||
|
||
@Override | ||
public void plug() { | ||
chainsaws.setPlug("环保防触电插头"); | ||
} | ||
|
||
@Override | ||
public void circularSaw() { | ||
chainsaws.setCircularSaw("高硬度锯片"); | ||
} | ||
|
||
@Override | ||
public Chainsaws getChainsaws() { | ||
return chainsaws; | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/Chainsaws.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.lzw.part1_creation_mode.builder.chainsaw; | ||
|
||
/** | ||
* 产品(链锯) | ||
*/ | ||
public class Chainsaws { | ||
|
||
// 蓄电池 | ||
private String battery; | ||
// 电动机 | ||
private String motor; | ||
// 减速箱 | ||
private String reductionGearbox; | ||
// 防护罩 | ||
private String hood; | ||
// 手柄 | ||
private String handle; | ||
// 开关 | ||
private String switches; | ||
// 插头 | ||
private String plug; | ||
// 圆锯片 | ||
private String circularSaw; | ||
|
||
|
||
public String getBattery() { | ||
return battery; | ||
} | ||
|
||
public void setBattery(String battery) { | ||
this.battery = battery; | ||
} | ||
|
||
public String getMotor() { | ||
return motor; | ||
} | ||
|
||
public void setMotor(String motor) { | ||
this.motor = motor; | ||
} | ||
|
||
public String getReductionGearbox() { | ||
return reductionGearbox; | ||
} | ||
|
||
public void setReductionGearbox(String reductionGearbox) { | ||
this.reductionGearbox = reductionGearbox; | ||
} | ||
|
||
public String getHood() { | ||
return hood; | ||
} | ||
|
||
public void setHood(String hood) { | ||
this.hood = hood; | ||
} | ||
|
||
public String getHandle() { | ||
return handle; | ||
} | ||
|
||
public void setHandle(String handle) { | ||
this.handle = handle; | ||
} | ||
|
||
public String getSwitches() { | ||
return switches; | ||
} | ||
|
||
public void setSwitches(String switches) { | ||
this.switches = switches; | ||
} | ||
|
||
public String getPlug() { | ||
return plug; | ||
} | ||
|
||
|
||
public void setPlug(String plug) { | ||
this.plug = plug; | ||
} | ||
|
||
public String getCircularSaw() { | ||
return circularSaw; | ||
} | ||
|
||
public void setCircularSaw(String circularSaw) { | ||
this.circularSaw = circularSaw; | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return "Chainsaws{" + | ||
"\n battery='" + battery + '\'' + | ||
", motor='" + motor + '\'' + | ||
", reductionGearbox='" + reductionGearbox + '\'' + | ||
", hood='" + hood + '\'' + | ||
", handle='" + handle + '\'' + | ||
", switches='" + switches + '\'' + | ||
", plug='" + plug + '\'' + | ||
", circularSaw='" + circularSaw + '\'' + | ||
"链锯已经做好了\n" + '}'; | ||
} | ||
|
||
|
||
} |
29 changes: 29 additions & 0 deletions
29
design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/HardwareStoreBoss.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.lzw.part1_creation_mode.builder.chainsaw; | ||
|
||
/** | ||
* 五金店老板(验收方) | ||
*/ | ||
public class HardwareStoreBoss { | ||
|
||
// 电话通知建造方,查看进度如何了 | ||
public Chainsaws notify(ChainsawBuilder builder){ | ||
|
||
// 蓄电池 | ||
builder.battery(); | ||
// 电动机 | ||
builder.motor(); | ||
// 减速箱 | ||
builder.reductionGearbox(); | ||
// 防护罩 | ||
builder.hood(); | ||
// 手柄 | ||
builder.handle(); | ||
// 开关 | ||
builder.switches(); | ||
// 插头 | ||
builder.plug(); | ||
// 圆锯片 | ||
builder.circularSaw(); | ||
return builder.getChainsaws(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.lzw.part1_creation_mode.builder.chainsaw; | ||
|
||
/** | ||
* 测试类 | ||
*/ | ||
public class Test { | ||
|
||
public static void main(String[] args) { | ||
|
||
HardwareStoreBoss hardwareStoreBoss = new HardwareStoreBoss(); | ||
|
||
Chainsaws chainsaws = hardwareStoreBoss.notify(new ChainsawBuilderImpl()); | ||
System.out.println(chainsaws.toString()); | ||
|
||
// 新链锯 | ||
Chainsaws chainsaws2 = hardwareStoreBoss.notify(new ChainsawBuilderImpl2()); | ||
System.out.println(chainsaws2.toString()); | ||
} | ||
|
||
|
||
} |
28 changes: 28 additions & 0 deletions
28
design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw2/ChainsawBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.lzw.part1_creation_mode.builder.chainsaw2; | ||
|
||
/** | ||
* 链锯建筑者 | ||
*/ | ||
public interface ChainsawBuilder { | ||
|
||
// 蓄电池 | ||
ChainsawBuilder battery(String name); | ||
// 电动机 | ||
ChainsawBuilder motor(String name); | ||
// 减速箱 | ||
ChainsawBuilder reductionGearbox(String name); | ||
// 防护罩 | ||
ChainsawBuilder hood(String name); | ||
// 手柄 | ||
ChainsawBuilder handle(String name); | ||
// 开关 | ||
ChainsawBuilder switches(String name); | ||
// 插头 | ||
ChainsawBuilder plug(String name); | ||
// 圆锯片 | ||
ChainsawBuilder circularSaw(String name); | ||
|
||
// 获取链锯(产品)的实例 | ||
Chainsaws getChainsaws(); | ||
|
||
} |
Oops, something went wrong.