diff --git a/README.md b/README.md index a592bde..a1304cc 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,6 @@ 酷玩Android你最牛

- ---- ## 介绍 @@ -58,7 +57,12 @@ Android只是太多太杂乱,很多人都不知道从何学起。也一些东 :pencil: :one: **二维码处理(待完善)** -> ### 4、开源库整理 +> ### 4、Java知识点解读 + +**设计模式:** [Java23种设计模式](design_patterns) + + +> ### 5、开源库整理 :fire: :one: **[全面总结国内BATH等大厂开源的安卓有关的库](https://github.com/AweiLoveAndroid/CommonDevKnowledge/blob/master/%E5%9B%BD%E5%86%85BATH%E7%AD%89%E5%A4%A7%E5%8E%82%E5%BC%80%E6%BA%90%E7%9A%84%E5%AE%89%E5%8D%93%E6%9C%89%E5%85%B3%E7%9A%84%E5%BA%93/%E5%85%A8%E9%9D%A2%E6%80%BB%E7%BB%93%E5%9B%BD%E5%86%85BATH%E7%AD%89%E5%A4%A7%E5%8E%82%E5%BC%80%E6%BA%90%E7%9A%84%E5%AE%89%E5%8D%93%E6%9C%89%E5%85%B3%E7%9A%84%E5%BA%93.md)** 这一部分同步发布在我的简书博客,文章地址:[https://www.jianshu.com/p/2a6c806ba2fc](https://www.jianshu.com/p/2a6c806ba2fc) diff --git a/design_patterns/README.md b/design_patterns/README.md new file mode 100644 index 0000000..7ed8435 --- /dev/null +++ b/design_patterns/README.md @@ -0,0 +1,5 @@ +由光头强引发的Java23种设计模式的思考: + +代码|详细文章讲解 +----|---- +[](src/com/lzw/part1_creation_mode/builder)| [Java设计模式4之建造者模式(光头强买电锯引发的思考)](https://www.jianshu.com/p/713120e9dc59) \ No newline at end of file diff --git a/design_patterns/src/com/lzw/part1_creation_mode/README.md b/design_patterns/src/com/lzw/part1_creation_mode/README.md new file mode 100644 index 0000000..d42bb9b --- /dev/null +++ b/design_patterns/src/com/lzw/part1_creation_mode/README.md @@ -0,0 +1,7 @@ +结构型模式分为以下 5 种(有的版也把简单工厂模式算在里面,就是6种): + +单例(Singleton)模式:某个类只能生成一个实例,该类提供了一个全局访问点供外部获取该实例,其拓展是有限多例模式。 +原型(Prototype)模式:将一个对象作为原型,通过对其进行复制而克隆出多个和原型类似的新实例。 +工厂方法(FactoryMethod)模式:定义一个用于创建产品的接口,由子类决定生产什么产品。 +抽象工厂(AbstractFactory)模式:提供一个创建产品族的接口,其每个子类可以生产一系列相关的产品。 +建造者(Builder)模式:将一个复杂对象分解成多个相对简单的部分,然后根据不同需要分别创建它们,最后构建成该复杂对象。 \ No newline at end of file diff --git a/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/ChainsawBuilder.java b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/ChainsawBuilder.java new file mode 100644 index 0000000..74133d1 --- /dev/null +++ b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/ChainsawBuilder.java @@ -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(); + +} diff --git a/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/ChainsawBuilderImpl.java b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/ChainsawBuilderImpl.java new file mode 100644 index 0000000..cf7300a --- /dev/null +++ b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/ChainsawBuilderImpl.java @@ -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; + } +} diff --git a/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/ChainsawBuilderImpl2.java b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/ChainsawBuilderImpl2.java new file mode 100644 index 0000000..44b638b --- /dev/null +++ b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/ChainsawBuilderImpl2.java @@ -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; + } +} diff --git a/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/Chainsaws.java b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/Chainsaws.java new file mode 100644 index 0000000..71a4084 --- /dev/null +++ b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/Chainsaws.java @@ -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" + '}'; + } + + +} diff --git a/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/HardwareStoreBoss.java b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/HardwareStoreBoss.java new file mode 100644 index 0000000..e32d0a6 --- /dev/null +++ b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/HardwareStoreBoss.java @@ -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(); + } +} diff --git a/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/Test.java b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/Test.java new file mode 100644 index 0000000..917d476 --- /dev/null +++ b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw/Test.java @@ -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()); + } + + +} diff --git a/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw2/ChainsawBuilder.java b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw2/ChainsawBuilder.java new file mode 100644 index 0000000..1a5ce98 --- /dev/null +++ b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw2/ChainsawBuilder.java @@ -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(); + +} diff --git a/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw2/ChainsawBuilderImpl2.java b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw2/ChainsawBuilderImpl2.java new file mode 100644 index 0000000..56e64a9 --- /dev/null +++ b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw2/ChainsawBuilderImpl2.java @@ -0,0 +1,66 @@ +package com.lzw.part1_creation_mode.builder.chainsaw2; + +/** + * 链锯建筑者(具体建筑者) + */ +public class ChainsawBuilderImpl2 implements ChainsawBuilder { + + private Chainsaws chainsaws; + + public ChainsawBuilderImpl2() { + chainsaws = new Chainsaws(); + } + + @Override + public ChainsawBuilder battery(String name) { + chainsaws.setBattery(name); + return this; + } + + @Override + public ChainsawBuilder motor(String name) { + chainsaws.setMotor(name); + return this; + } + + @Override + public ChainsawBuilder reductionGearbox(String name) { + chainsaws.setReductionGearbox(name); + return this; + } + + @Override + public ChainsawBuilder hood(String name) { + chainsaws.setHood(name); + return this; + } + + @Override + public ChainsawBuilder handle(String name) { + chainsaws.setHandle(name); + return this; + } + + @Override + public ChainsawBuilder switches(String name) { + chainsaws.setSwitches(name); + return this; + } + + @Override + public ChainsawBuilder plug(String name) { + chainsaws.setPlug(name); + return this; + } + + @Override + public ChainsawBuilder circularSaw(String name) { + chainsaws.setCircularSaw(name); + return this; + } + + @Override + public Chainsaws getChainsaws() { + return chainsaws; + } +} diff --git a/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw2/Chainsaws.java b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw2/Chainsaws.java new file mode 100644 index 0000000..e800a69 --- /dev/null +++ b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw2/Chainsaws.java @@ -0,0 +1,108 @@ +package com.lzw.part1_creation_mode.builder.chainsaw2; + +/** + * 产品(链锯) + * 保持不变 + */ +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" + '}'; + } + + +} diff --git a/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw2/Test.java b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw2/Test.java new file mode 100644 index 0000000..53bcf3d --- /dev/null +++ b/design_patterns/src/com/lzw/part1_creation_mode/builder/chainsaw2/Test.java @@ -0,0 +1,24 @@ +package com.lzw.part1_creation_mode.builder.chainsaw2; + +/** + * 测试类 + */ +public class Test { + + public static void main(String[] args) { + ChainsawBuilderImpl2 chainsawBuilderImpl2 = new ChainsawBuilderImpl2(); + Chainsaws chainsaws = chainsawBuilderImpl2.battery("环保大容量锂电池") + .motor("动力强耐高温散热好的纯铜电机") + .reductionGearbox("减震变频高功率变速箱") + .hood("加厚加固耐磨抗打击防护罩") + .handle("人体工体学包胶手柄") + .switches("调速防误触智能开关") + .plug("环保防触电插头") + .circularSaw("高硬度锯片") + .getChainsaws(); + + System.out.println(chainsaws.toString()); + } + + +}