Skip to content

Commit

Permalink
新增Java设计模式示例代码和文档
Browse files Browse the repository at this point in the history
  • Loading branch information
AiNiBaBa committed Aug 22, 2019
1 parent 052e410 commit 2b4e866
Show file tree
Hide file tree
Showing 13 changed files with 547 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<a href="//shang.qq.com/wpa/qunwpa?idkey=c1ffcc74ac64e98b1546b94537c581922548a7e63f78a86fcb59cef47988f453">
<img src="https://img.shields.io/badge/QQ%E7%BE%A4-280891494-informational.svg" target="_blank" alt="酷玩Android你最牛" title="280891494"></a>
</p>

----

## 介绍
Expand Down Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions design_patterns/README.md
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)
7 changes: 7 additions & 0 deletions design_patterns/src/com/lzw/part1_creation_mode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
结构型模式分为以下 5 种(有的版也把简单工厂模式算在里面,就是6种):

单例(Singleton)模式:某个类只能生成一个实例,该类提供了一个全局访问点供外部获取该实例,其拓展是有限多例模式。
原型(Prototype)模式:将一个对象作为原型,通过对其进行复制而克隆出多个和原型类似的新实例。
工厂方法(FactoryMethod)模式:定义一个用于创建产品的接口,由子类决定生产什么产品。
抽象工厂(AbstractFactory)模式:提供一个创建产品族的接口,其每个子类可以生产一系列相关的产品。
建造者(Builder)模式:将一个复杂对象分解成多个相对简单的部分,然后根据不同需要分别创建它们,最后构建成该复杂对象。
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();

}
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;
}
}
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;
}
}
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" + '}';
}


}
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();
}
}
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());
}


}
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();

}
Loading

0 comments on commit 2b4e866

Please sign in to comment.