Skip to content

Commit f1286d8

Browse files
committed
设计模式
1 parent e28071e commit f1286d8

31 files changed

Lines changed: 438 additions & 0 deletions
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.heibaiying.creational.prototype;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class Phone implements Cloneable {
7+
8+
private String type;
9+
10+
Phone(String type) {
11+
System.out.println("构造器被调用");
12+
this.type = type;
13+
}
14+
15+
public void call() {
16+
System.out.println(type + "拨打电话");
17+
}
18+
19+
@Override
20+
protected Object clone() throws CloneNotSupportedException {
21+
System.out.println("克隆方法被调用");
22+
return super.clone();
23+
}
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.heibaiying.creational.prototype;
2+
3+
import lombok.Data;
4+
5+
import java.util.Date;
6+
7+
@Data
8+
public class SmartPhone implements Cloneable {
9+
10+
private String type;
11+
private Date productionDate;
12+
13+
SmartPhone(String type, Date productionDate) {
14+
this.type = type;
15+
this.productionDate = productionDate;
16+
}
17+
18+
public void call() {
19+
System.out.println(type + "拨打电话");
20+
}
21+
22+
@Override
23+
protected Object clone() throws CloneNotSupportedException {
24+
SmartPhone smartPhone = (SmartPhone) super.clone();
25+
smartPhone.productionDate = (Date) smartPhone.productionDate.clone();
26+
return smartPhone;
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.heibaiying.creational.prototype;
2+
3+
import java.util.Date;
4+
5+
public class ZTest {
6+
public static void main(String[] args) throws CloneNotSupportedException {
7+
Phone phone = new Phone("3G手机");
8+
Phone clonePhone = (Phone) phone.clone();
9+
clonePhone.call();
10+
11+
SmartPhone smartPhone = new SmartPhone("4G手机", new Date());
12+
SmartPhone cloneSmartPhone = (SmartPhone) smartPhone.clone();
13+
System.out.println(smartPhone == cloneSmartPhone);
14+
System.out.println(smartPhone.getProductionDate() == cloneSmartPhone.getProductionDate());
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.heibaiying.structural.adapter;
2+
3+
public class ChargerAdapter extends PowerSupply implements Target {
4+
@Override
5+
public int output5V() {
6+
int output = output220V();
7+
System.out.println("充电头适配转换");
8+
output = output / 44;
9+
System.out.println("输出电压:" + output);
10+
return output;
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.heibaiying.structural.adapter;
2+
3+
/**
4+
* 电源类
5+
*/
6+
public class PowerSupply {
7+
8+
private final int output = 220;
9+
10+
public int output220V() {
11+
System.out.println("电源电压:" + output);
12+
return output;
13+
}
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.heibaiying.structural.adapter;
2+
3+
public interface Target {
4+
int output5V();
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.heibaiying.structural.adapter;
2+
3+
public class ZTest {
4+
public static void main(String[] args) {
5+
Target target = new ChargerAdapter();
6+
target.output5V();
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.heibaiying.structural.bridge;
2+
3+
public class Blue implements Color {
4+
5+
@Override
6+
public String getDesc() {
7+
return "蓝色";
8+
}
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.heibaiying.structural.bridge;
2+
3+
public interface Color {
4+
String getDesc();
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.heibaiying.structural.bridge;
2+
3+
public class Red implements Color {
4+
@Override
5+
public String getDesc() {
6+
return "红色";
7+
}
8+
}

0 commit comments

Comments
 (0)