Skip to content

Commit 6cd2987

Browse files
committed
设计模式
1 parent b42b937 commit 6cd2987

28 files changed

Lines changed: 250 additions & 12 deletions
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.heibaiying.creational.abstract_factory;
2+
3+
/**
4+
* 充电器
5+
*/
6+
public abstract class Charger {
7+
public abstract void Charge(Phone phone);
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.heibaiying.creational.abstract_factory;
2+
3+
public interface Factory {
4+
5+
Phone producePhone();
6+
7+
Charger produceCharger();
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.heibaiying.creational.abstract_factory;
2+
3+
public class HuaiweiCharger extends Charger {
4+
@Override
5+
public void Charge(Phone phone) {
6+
System.out.println("华为充电器给" + phone + "充电");
7+
}
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.heibaiying.creational.abstract_factory;
2+
3+
public class HuaweiPhone extends Phone {
4+
public void call(String phoneNum) {
5+
System.out.println("华为手机拨打电话:" + phoneNum);
6+
}
7+
@Override
8+
public String toString() {
9+
return "华为手机";
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.heibaiying.creational.abstract_factory;
2+
3+
public class HuaweiPhoneFactory implements Factory {
4+
5+
@Override
6+
public Phone producePhone() {
7+
return new HuaweiPhone();
8+
}
9+
10+
@Override
11+
public Charger produceCharger() {
12+
return new HuaiweiCharger();
13+
}
14+
}

code/Java/design-pattern/src/main/java/com/heibaiying/creational/SimpleFactory/Phone.java renamed to code/Java/design-pattern/src/main/java/com/heibaiying/creational/abstract_factory/Phone.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
package com.heibaiying.creational.SimpleFactory;
1+
package com.heibaiying.creational.abstract_factory;
22

3-
/**
4-
* 手机
5-
*/
63
public abstract class Phone {
4+
75
public abstract void call(String phoneNum);
86
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.heibaiying.creational.abstract_factory;
2+
3+
public class XiaomiCharger extends Charger {
4+
@Override
5+
public void Charge(Phone phone) {
6+
System.out.println("小米充电器给" + phone + "充电");
7+
}
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.heibaiying.creational.abstract_factory;
2+
3+
public class XiaomiPhone extends Phone {
4+
public void call(String phoneNum) {
5+
System.out.println("小米手机拨打电话:" + phoneNum);
6+
}
7+
8+
@Override
9+
public String toString() {
10+
return "小米手机";
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.heibaiying.creational.abstract_factory;
2+
3+
public class XiaomiPhoneFactory implements Factory {
4+
5+
@Override
6+
public Phone producePhone() {
7+
return new XiaomiPhone();
8+
}
9+
10+
@Override
11+
public Charger produceCharger() {
12+
return new XiaomiCharger();
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.heibaiying.creational.abstract_factory;
2+
3+
public class ZTest {
4+
public static void main(String[] args) {
5+
XiaomiPhoneFactory xiaomiPhoneFactory = new XiaomiPhoneFactory();
6+
xiaomiPhoneFactory.produceCharger().Charge(xiaomiPhoneFactory.producePhone());
7+
HuaweiPhoneFactory huaweiPhoneFactory = new HuaweiPhoneFactory();
8+
huaweiPhoneFactory.produceCharger().Charge(huaweiPhoneFactory.producePhone());
9+
}
10+
}

0 commit comments

Comments
 (0)