设计模式
This commit is contained in:
parent
b42b937cdf
commit
6cd2987838
@ -0,0 +1,8 @@
|
||||
package com.heibaiying.creational.abstract_factory;
|
||||
|
||||
/**
|
||||
* 充电器
|
||||
*/
|
||||
public abstract class Charger {
|
||||
public abstract void Charge(Phone phone);
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.heibaiying.creational.abstract_factory;
|
||||
|
||||
public interface Factory {
|
||||
|
||||
Phone producePhone();
|
||||
|
||||
Charger produceCharger();
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.heibaiying.creational.abstract_factory;
|
||||
|
||||
public class HuaiweiCharger extends Charger {
|
||||
@Override
|
||||
public void Charge(Phone phone) {
|
||||
System.out.println("华为充电器给" + phone + "充电");
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.heibaiying.creational.abstract_factory;
|
||||
|
||||
public class HuaweiPhone extends Phone {
|
||||
public void call(String phoneNum) {
|
||||
System.out.println("华为手机拨打电话:" + phoneNum);
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "华为手机";
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.heibaiying.creational.abstract_factory;
|
||||
|
||||
public class HuaweiPhoneFactory implements Factory {
|
||||
|
||||
@Override
|
||||
public Phone producePhone() {
|
||||
return new HuaweiPhone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Charger produceCharger() {
|
||||
return new HuaiweiCharger();
|
||||
}
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
package com.heibaiying.creational.SimpleFactory;
|
||||
package com.heibaiying.creational.abstract_factory;
|
||||
|
||||
/**
|
||||
* 手机
|
||||
*/
|
||||
public abstract class Phone {
|
||||
|
||||
public abstract void call(String phoneNum);
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.heibaiying.creational.abstract_factory;
|
||||
|
||||
public class XiaomiCharger extends Charger {
|
||||
@Override
|
||||
public void Charge(Phone phone) {
|
||||
System.out.println("小米充电器给" + phone + "充电");
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.heibaiying.creational.abstract_factory;
|
||||
|
||||
public class XiaomiPhone extends Phone {
|
||||
public void call(String phoneNum) {
|
||||
System.out.println("小米手机拨打电话:" + phoneNum);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "小米手机";
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.heibaiying.creational.abstract_factory;
|
||||
|
||||
public class XiaomiPhoneFactory implements Factory {
|
||||
|
||||
@Override
|
||||
public Phone producePhone() {
|
||||
return new XiaomiPhone();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Charger produceCharger() {
|
||||
return new XiaomiCharger();
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.heibaiying.creational.abstract_factory;
|
||||
|
||||
public class ZTest {
|
||||
public static void main(String[] args) {
|
||||
XiaomiPhoneFactory xiaomiPhoneFactory = new XiaomiPhoneFactory();
|
||||
xiaomiPhoneFactory.produceCharger().Charge(xiaomiPhoneFactory.producePhone());
|
||||
HuaweiPhoneFactory huaweiPhoneFactory = new HuaweiPhoneFactory();
|
||||
huaweiPhoneFactory.produceCharger().Charge(huaweiPhoneFactory.producePhone());
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.heibaiying.creational.builder;
|
||||
|
||||
public abstract class Builder {
|
||||
|
||||
protected Phone phone = new Phone();
|
||||
|
||||
public abstract void addProcessor();
|
||||
|
||||
public abstract void addCamera();
|
||||
|
||||
public abstract void addScreen();
|
||||
|
||||
public Phone produce() {
|
||||
return phone;
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.heibaiying.creational.builder;
|
||||
|
||||
public class HuaweiBuilder extends Builder {
|
||||
@Override
|
||||
public void addProcessor() {
|
||||
phone.setProcessor("海思麒麟处理器");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCamera() {
|
||||
phone.setCamera("莱卡摄像头");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addScreen() {
|
||||
phone.setScreen("OLED");
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.creational.builder;
|
||||
|
||||
public class Manager {
|
||||
|
||||
private Builder builder;
|
||||
|
||||
public Manager(Builder builder) {
|
||||
this.builder = builder;
|
||||
}
|
||||
|
||||
public Phone buy() {
|
||||
builder.addCamera();
|
||||
builder.addProcessor();
|
||||
builder.addScreen();
|
||||
return builder.produce();
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.heibaiying.creational.builder;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Phone {
|
||||
/*处理器*/
|
||||
private String processor;
|
||||
/*摄像头*/
|
||||
private String camera;
|
||||
/*屏幕*/
|
||||
private String screen;
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.heibaiying.creational.builder;
|
||||
|
||||
public class XiaomiBuilder extends Builder {
|
||||
@Override
|
||||
public void addProcessor() {
|
||||
phone.setProcessor("高通骁龙处理器");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCamera() {
|
||||
phone.setCamera("索尼摄像头");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addScreen() {
|
||||
phone.setScreen("OLED");
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.heibaiying.creational.builder;
|
||||
|
||||
public class ZTest {
|
||||
public static void main(String[] args) {
|
||||
Phone huawei = new Manager(new HuaweiBuilder()).buy();
|
||||
System.out.println(huawei);
|
||||
Phone xiaomi = new Manager(new XiaomiBuilder()).buy();
|
||||
System.out.println(xiaomi);
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.heibaiying.creational.factory_method;
|
||||
|
||||
public interface Factory {
|
||||
Phone produce();
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
package com.heibaiying.creational.SimpleFactory;
|
||||
|
||||
package com.heibaiying.creational.factory_method;
|
||||
|
||||
public class HuaweiPhone extends Phone {
|
||||
public void call(String phoneNum) {
|
@ -0,0 +1,9 @@
|
||||
package com.heibaiying.creational.factory_method;
|
||||
|
||||
public class HuaweiPhoneFactory implements Factory {
|
||||
|
||||
@Override
|
||||
public Phone produce() {
|
||||
return new HuaweiPhone();
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.heibaiying.creational.factory_method;
|
||||
|
||||
public abstract class Phone {
|
||||
public abstract void call(String phoneNum);
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.heibaiying.creational.SimpleFactory;
|
||||
package com.heibaiying.creational.factory_method;
|
||||
|
||||
public class XiaomiPhone extends Phone {
|
||||
public void call(String phoneNum) {
|
@ -0,0 +1,9 @@
|
||||
package com.heibaiying.creational.factory_method;
|
||||
|
||||
public class XiaomiPhoneFactory implements Factory {
|
||||
|
||||
@Override
|
||||
public Phone produce() {
|
||||
return new XiaomiPhone();
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.heibaiying.creational.factory_method;
|
||||
|
||||
public class ZTest {
|
||||
public static void main(String[] args) {
|
||||
XiaomiPhoneFactory xiaomiPhoneFactory = new XiaomiPhoneFactory();
|
||||
xiaomiPhoneFactory.produce().call("123");
|
||||
HuaweiPhoneFactory huaweiPhoneFactory = new HuaweiPhoneFactory();
|
||||
huaweiPhoneFactory.produce().call("456");
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.heibaiying.creational.simple_factory;
|
||||
|
||||
public class HuaweiPhone extends Phone {
|
||||
public void call(String phoneNum) {
|
||||
System.out.println("华为手机拨打电话:" + phoneNum);
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.heibaiying.creational.simple_factory;
|
||||
|
||||
public abstract class Phone {
|
||||
public abstract void call(String phoneNum);
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
package com.heibaiying.creational.SimpleFactory;
|
||||
|
||||
package com.heibaiying.creational.simple_factory;
|
||||
|
||||
public class PhoneFactory {
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.heibaiying.creational.simple_factory;
|
||||
|
||||
public class XiaomiPhone extends Phone {
|
||||
public void call(String phoneNum) {
|
||||
System.out.println("小米手机拨打电话:" + phoneNum);
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package com.heibaiying.creational.SimpleFactory;
|
||||
package com.heibaiying.creational.simple_factory;
|
||||
|
||||
|
||||
public class Test {
|
||||
public class ZTest {
|
||||
public static void main(String[] args) {
|
||||
PhoneFactory phoneFactory = new PhoneFactory();
|
||||
phoneFactory.getPhone("xiaomi").call("123");
|
Loading…
x
Reference in New Issue
Block a user