设计模式
This commit is contained in:
parent
e28071e796
commit
f1286d87ae
@ -0,0 +1,24 @@
|
|||||||
|
package com.heibaiying.creational.prototype;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class Phone implements Cloneable {
|
||||||
|
|
||||||
|
private String type;
|
||||||
|
|
||||||
|
Phone(String type) {
|
||||||
|
System.out.println("构造器被调用");
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void call() {
|
||||||
|
System.out.println(type + "拨打电话");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Object clone() throws CloneNotSupportedException {
|
||||||
|
System.out.println("克隆方法被调用");
|
||||||
|
return super.clone();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.heibaiying.creational.prototype;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class SmartPhone implements Cloneable {
|
||||||
|
|
||||||
|
private String type;
|
||||||
|
private Date productionDate;
|
||||||
|
|
||||||
|
SmartPhone(String type, Date productionDate) {
|
||||||
|
this.type = type;
|
||||||
|
this.productionDate = productionDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void call() {
|
||||||
|
System.out.println(type + "拨打电话");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Object clone() throws CloneNotSupportedException {
|
||||||
|
SmartPhone smartPhone = (SmartPhone) super.clone();
|
||||||
|
smartPhone.productionDate = (Date) smartPhone.productionDate.clone();
|
||||||
|
return smartPhone;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.heibaiying.creational.prototype;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class ZTest {
|
||||||
|
public static void main(String[] args) throws CloneNotSupportedException {
|
||||||
|
Phone phone = new Phone("3G手机");
|
||||||
|
Phone clonePhone = (Phone) phone.clone();
|
||||||
|
clonePhone.call();
|
||||||
|
|
||||||
|
SmartPhone smartPhone = new SmartPhone("4G手机", new Date());
|
||||||
|
SmartPhone cloneSmartPhone = (SmartPhone) smartPhone.clone();
|
||||||
|
System.out.println(smartPhone == cloneSmartPhone);
|
||||||
|
System.out.println(smartPhone.getProductionDate() == cloneSmartPhone.getProductionDate());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.heibaiying.structural.adapter;
|
||||||
|
|
||||||
|
public class ChargerAdapter extends PowerSupply implements Target {
|
||||||
|
@Override
|
||||||
|
public int output5V() {
|
||||||
|
int output = output220V();
|
||||||
|
System.out.println("充电头适配转换");
|
||||||
|
output = output / 44;
|
||||||
|
System.out.println("输出电压:" + output);
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.heibaiying.structural.adapter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 电源类
|
||||||
|
*/
|
||||||
|
public class PowerSupply {
|
||||||
|
|
||||||
|
private final int output = 220;
|
||||||
|
|
||||||
|
public int output220V() {
|
||||||
|
System.out.println("电源电压:" + output);
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.heibaiying.structural.adapter;
|
||||||
|
|
||||||
|
public interface Target {
|
||||||
|
int output5V();
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.heibaiying.structural.adapter;
|
||||||
|
|
||||||
|
public class ZTest {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Target target = new ChargerAdapter();
|
||||||
|
target.output5V();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.heibaiying.structural.bridge;
|
||||||
|
|
||||||
|
public class Blue implements Color {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDesc() {
|
||||||
|
return "蓝色";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.heibaiying.structural.bridge;
|
||||||
|
|
||||||
|
public interface Color {
|
||||||
|
String getDesc();
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.heibaiying.structural.bridge;
|
||||||
|
|
||||||
|
public class Red implements Color {
|
||||||
|
@Override
|
||||||
|
public String getDesc() {
|
||||||
|
return "红色";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.heibaiying.structural.bridge;
|
||||||
|
|
||||||
|
public class Round extends Shape {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void getDesc() {
|
||||||
|
System.out.println(getColor().getDesc() + "圆形");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.heibaiying.structural.bridge;
|
||||||
|
|
||||||
|
public abstract class Shape {
|
||||||
|
|
||||||
|
private Color color;
|
||||||
|
|
||||||
|
public Shape setColor(Color color) {
|
||||||
|
this.color = color;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color getColor() {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void getDesc();
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.heibaiying.structural.bridge;
|
||||||
|
|
||||||
|
public class Square extends Shape {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void getDesc() {
|
||||||
|
System.out.println(getColor().getDesc() + "正方形");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.heibaiying.structural.bridge;
|
||||||
|
|
||||||
|
public class Test {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new Square().setColor(new Red()).getDesc();
|
||||||
|
new Square().setColor(new Blue()).getDesc();
|
||||||
|
new Round().setColor(new Blue()).getDesc();
|
||||||
|
new Round().setColor(new Yellow()).getDesc();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.heibaiying.structural.bridge;
|
||||||
|
|
||||||
|
public class Yellow implements Color {
|
||||||
|
@Override
|
||||||
|
public String getDesc() {
|
||||||
|
return "黄色";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.heibaiying.structural.composite;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public abstract class Component {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Component(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(Component component) {
|
||||||
|
throw new UnsupportedOperationException("不支持添加操作");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove(Component component) {
|
||||||
|
throw new UnsupportedOperationException("不支持删除操作");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void vim(String content) {
|
||||||
|
throw new UnsupportedOperationException("不支持使用vim编辑器打开");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cat() {
|
||||||
|
throw new UnsupportedOperationException("不支持查看操作");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void print() {
|
||||||
|
throw new UnsupportedOperationException("不支持打印操作");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
package com.heibaiying.structural.composite;
|
||||||
|
|
||||||
|
public class File extends Component {
|
||||||
|
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
public File(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void vim(String content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cat() {
|
||||||
|
System.out.println(content);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void print() {
|
||||||
|
System.out.println(getName());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.heibaiying.structural.composite;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Folder extends Component {
|
||||||
|
|
||||||
|
private List<Component> componentList = new ArrayList<>();
|
||||||
|
|
||||||
|
public Folder(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(Component component) {
|
||||||
|
componentList.add(component);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove(Component component) {
|
||||||
|
componentList.remove(component);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void print() {
|
||||||
|
System.out.println(getName());
|
||||||
|
componentList.forEach(x -> System.out.println(" " + x.getName()));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.heibaiying.structural.composite;
|
||||||
|
|
||||||
|
public class ZTest {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Folder rootDir = new Folder("ROOT目录");
|
||||||
|
Folder nginx = new Folder("Nginx安装目录");
|
||||||
|
Folder tomcat = new Folder("Tomcat安装目录");
|
||||||
|
File startup = new File("startup.bat");
|
||||||
|
rootDir.add(nginx);
|
||||||
|
rootDir.add(tomcat);
|
||||||
|
rootDir.add(startup);
|
||||||
|
rootDir.print();
|
||||||
|
startup.vim("java -jar");
|
||||||
|
startup.cat();
|
||||||
|
nginx.cat();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.heibaiying.structural.decorator;
|
||||||
|
|
||||||
|
public abstract class Decorator extends Phone {
|
||||||
|
|
||||||
|
private Phone phone;
|
||||||
|
|
||||||
|
public Decorator(Phone phone) {
|
||||||
|
this.phone = phone;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getPrice() {
|
||||||
|
return phone.getPrice();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDesc() {
|
||||||
|
return phone.getDesc();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.heibaiying.structural.decorator;
|
||||||
|
|
||||||
|
public class FilmDecorator extends Decorator {
|
||||||
|
|
||||||
|
public FilmDecorator(Phone phone) {
|
||||||
|
super(phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getPrice() {
|
||||||
|
return super.getPrice() + 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDesc() {
|
||||||
|
return super.getDesc() + " + 钢化膜";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.heibaiying.structural.decorator;
|
||||||
|
|
||||||
|
public class MiPhone extends Phone {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getPrice() {
|
||||||
|
return 1999;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDesc() {
|
||||||
|
return "MiPhone";
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.heibaiying.structural.decorator;
|
||||||
|
|
||||||
|
public abstract class Phone {
|
||||||
|
|
||||||
|
public abstract int getPrice();
|
||||||
|
|
||||||
|
public abstract String getDesc();
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.heibaiying.structural.decorator;
|
||||||
|
|
||||||
|
public class ShellDecorator extends Decorator {
|
||||||
|
|
||||||
|
public ShellDecorator(Phone phone) {
|
||||||
|
super(phone);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getPrice() {
|
||||||
|
return super.getPrice() + 200;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDesc() {
|
||||||
|
return super.getDesc() + " + 手机壳";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.heibaiying.structural.decorator;
|
||||||
|
|
||||||
|
public class ZTest {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ShellDecorator decorator = new ShellDecorator(new FilmDecorator(new MiPhone()));
|
||||||
|
System.out.println(decorator.getDesc() + " : " + decorator.getPrice());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.heibaiying.structural.facade;
|
||||||
|
|
||||||
|
public class AccountService {
|
||||||
|
public boolean balanceCheck() {
|
||||||
|
System.out.println("账户余额校验...");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.heibaiying.structural.facade;
|
||||||
|
|
||||||
|
public class EnvInspectionService {
|
||||||
|
public boolean evInspection() {
|
||||||
|
System.out.println("支付环境检查...");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.heibaiying.structural.facade;
|
||||||
|
|
||||||
|
public class LogisticsService {
|
||||||
|
public void ship(Phone phone) {
|
||||||
|
System.out.println(phone.getName() + "已经发货,请注意查收...");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.heibaiying.structural.facade;
|
||||||
|
|
||||||
|
public class OrderService {
|
||||||
|
|
||||||
|
private EnvInspectionService inspectionService = new EnvInspectionService();
|
||||||
|
private AccountService accountService = new AccountService();
|
||||||
|
private LogisticsService logisticsService = new LogisticsService();
|
||||||
|
|
||||||
|
public void order(Phone phone) {
|
||||||
|
if (inspectionService.evInspection()) {
|
||||||
|
if (accountService.balanceCheck()) {
|
||||||
|
System.out.println("支付成功");
|
||||||
|
logisticsService.ship(phone);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.heibaiying.structural.facade;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class Phone {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Phone(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.heibaiying.structural.facade;
|
||||||
|
|
||||||
|
public class ZTest {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Phone phone = new Phone("XXX手机");
|
||||||
|
OrderService orderService = new OrderService();
|
||||||
|
orderService.order(phone);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user