设计模式之禅

This commit is contained in:
zhuyijun 2021-07-10 10:14:41 +08:00
commit 1058526492
993 changed files with 106018 additions and 0 deletions

27
.gitignore vendored Normal file
View File

@ -0,0 +1,27 @@
target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
.mvn
mvnw*
.gitee
### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
.nb-gradle/

87
README.md Normal file
View File

@ -0,0 +1,87 @@
# design-pattern 设计模式之禅
#### 项目介绍
##1、单一职责原则
- srp
##2、里氏替换原则
- lsp
##3、依赖倒置原则
- dip
##4、迪米特原则
- lod
##5、接口隔离原则
- isp
##6、开闭原则
- ocp
##7、单例模式
- singleton
##8、工厂方法模式
- simple_factory
##9、抽象工厂模式
- abstract_factory
##10、模板方法模式
- template_method
##11、建造者模式
- builder
##12、代理模式
- dynamic_proxy
- proxy
##13、原型模式
- prototype
##14、中介者模式
- mediator
##15、命令模式
- command
##16、责任链模式
- chain_of_responsibility
##17、装饰模式
- decorator
##18、策略模式
- strategy
##19、适配器模式
- adapter
##20、迭代器模式
- iterator
##21、组合模式
- composite
##22、观察者模式
- observer
##23、门面模式
- facade
##24、备忘录模式
- memento
##25、访问者模式
- visitor
##26、状态模式
- state
##27、解释器模式
- interpreter
##28、享元模式
- flyweight
##29、桥梁模式
- bridge
##30、创建类PK
- abstract_factory_vs_builder
- factory_vs_builder
##31、结构类PK
- decorator_vs_adapter
- proxy_vs_decorator
##32、行为类PK
- command_vs_strategy
- observer_vs_chain
- strategy_vs_state
##33、跨战区PK
- command_vs_strategy
- strategy_vs_bridge
- wrapper
##34、命令模式+责任链模式
- command_chain
##35、工厂方法模式+策略模式
- factory_strategy
##36、观察者模式+中介者模式
- observer_mediator
##37、规格模式
- specification
##38、MVC框架
- mvc

16
abstract_factory/pom.xml Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>design-pattern</artifactId>
<groupId>com.zhq</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhq</groupId>
<artifactId>abstract_factory</artifactId>
</project>

View File

@ -0,0 +1,18 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public abstract class AbstractBlackHuman implements Human {
public void getColor(){
System.out.println("黑色人种的皮肤颜色是黑色的!");
}
public void talk() {
System.out.println("黑人会说话,一般人听不懂。");
}
}

View File

@ -0,0 +1,20 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public abstract class AbstractWhiteHuman implements Human {
//白色人种的颜色是白色的
public void getColor(){
System.out.println("白色人种的皮肤颜色是白色的!");
}
//白色人种讲话
public void talk() {
System.out.println("白色人种会说话,一般都是但是单字节。");
}
}

View File

@ -0,0 +1,15 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public abstract class AbstractYellowHuman implements Human {
public void getColor(){
System.out.println("黄色人种的皮肤颜色是黄色的!");
}
public void talk() {
System.out.println("黄色人种会说话,一般说的都是双字节。");
}
}

View File

@ -0,0 +1,14 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class FemaleBlackHuman extends AbstractBlackHuman {
//女性黑人
public void getSex() {
System.out.println("黑人女性");
}
}

View File

@ -0,0 +1,24 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 女性工厂
*/
public class FemaleFactory implements HumanFactory {
//生产出黑人女性
public Human createBlackHuman() {
return new FemaleBlackHuman();
}
//生产出白人女性
public Human createWhiteHuman() {
return new FemaleWhiteHuman();
}
//生产出黄人女性
public Human createYellowHuman() {
return new FemaleYellowHuman();
}
}

View File

@ -0,0 +1,14 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class FemaleWhiteHuman extends AbstractWhiteHuman {
//白人女性
public void getSex() {
System.out.println("白人女性");
}
}

View File

@ -0,0 +1,14 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class FemaleYellowHuman extends AbstractYellowHuman {
//黄人女性
public void getSex() {
System.out.println("黄人女性");
}
}

View File

@ -0,0 +1,19 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 定义一个人类的统称
*/
public interface Human {
//每个人种都有相应的颜色
public void getColor();
//人类会说话
public void talk();
//每个人都有性别
public void getSex();
}

View File

@ -0,0 +1,19 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 这次定一个接口应该要造不同性别的人需要不同的生产线
* 那这个八卦炉必须可以制造男人和女人
*/
public interface HumanFactory {
//制造一个黄色人种
public Human createYellowHuman();
//制造一个白色人种
public Human createWhiteHuman();
//制造一个黑色人种
public Human createBlackHuman();
}

View File

@ -0,0 +1,14 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class MaleBlackHuman extends AbstractBlackHuman {
//女性黑人
public void getSex() {
System.out.println("黑人男性");
}
}

View File

@ -0,0 +1,24 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 男性工厂
*/
public class MaleFactory implements HumanFactory {
//生产出黑人男性
public Human createBlackHuman() {
return new MaleBlackHuman();
}
//生产出白人男性
public Human createWhiteHuman() {
return new MaleWhiteHuman();
}
//生产出黄人男性
public Human createYellowHuman() {
return new MaleYellowHuman();
}
}

View File

@ -0,0 +1,14 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class MaleWhiteHuman extends AbstractWhiteHuman {
//白人女性
public void getSex() {
System.out.println("白人男性");
}
}

View File

@ -0,0 +1,14 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class MaleYellowHuman extends AbstractYellowHuman {
//黄人男性
public void getSex() {
System.out.println("黄人男性");
}
}

View File

@ -0,0 +1,44 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 女娲建立起了两条生产线分别是
* 男性生产线
* 女性生产线
*/
public class NvWa {
public static void main(String[] args) {
//第一条生产线男性生产线
HumanFactory maleHumanFactory = new MaleFactory();
//第二条生产线女性生产线
HumanFactory femaleHumanFactory = new FemaleFactory();
//生产线建立完毕开始生产人了:
Human maleYellowHuman = maleHumanFactory.createYellowHuman();
Human femaleYellowHuman = femaleHumanFactory.createYellowHuman();
System.out.println("---生产一个黄色女性---");
femaleYellowHuman.getColor();
femaleYellowHuman.talk();
femaleYellowHuman.getSex();
System.out.println("\n---生产一个黄色男性---");
maleYellowHuman.getColor();
maleYellowHuman.talk();
maleYellowHuman.getSex();
/*
* .....
* 后面你可以续了
*/
}
}

View File

@ -0,0 +1,15 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 抽象的产品类
*/
public abstract class AbstractCreator {
//创建A产品家族
public abstract AbstractProductA createProductA();
//创建B产品家族
public abstract AbstractProductB createProductB();
}

View File

@ -0,0 +1,17 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 产品A
*/
public abstract class AbstractProductA {
//每个产品共有的方法
public void shareMethod(){
}
//每个产品相同方法不同实现
public abstract void doSomething();
}

View File

@ -0,0 +1,17 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 产品B
*/
public abstract class AbstractProductB {
//每个产品共有的方法
public void shareMethod(){
}
//每个产品相同方法不同实现
public abstract void doSomething();
}

View File

@ -0,0 +1,29 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 场景类
*/
public class Client {
public static void main(String[] args) {
//定义出两个工厂
AbstractCreator creator1 = new Creator1();
AbstractCreator creator2 = new Creator2();
//产生A1对象
AbstractProductA a1 = creator1.createProductA();
//产生A2对象
AbstractProductA a2 = creator2.createProductA();
//产生B1对象
AbstractProductB b1 = creator1.createProductB();
//产生B2对象
AbstractProductB b2 = creator2.createProductB();
/*
* 然后在这里就可以为所欲为了...
*/
}
}

View File

@ -0,0 +1,20 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 工厂1只生产跳线为1的产品
*/
public class Creator1 extends AbstractCreator {
//只生产产品等级为1的A产品
public AbstractProductA createProductA() {
return new ProductA1();
}
//只生产铲平等级为1的B产品
public AbstractProductB createProductB() {
return new ProductB1();
}
}

View File

@ -0,0 +1,20 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 工厂2只生产跳线为2的产品
*/
public class Creator2 extends AbstractCreator {
//只生产产品等级为2的A产品
public AbstractProductA createProductA() {
return new ProductA2();
}
//只生产铲平等级为2的B产品
public AbstractProductB createProductB() {
return new ProductB2();
}
}

View File

@ -0,0 +1,15 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 产品A的实现类
*/
public class ProductA1 extends AbstractProductA {
@Override
public void doSomething() {
System.out.println("产品A1的实现方法");
}
}

View File

@ -0,0 +1,15 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 产品A的实现类
*/
public class ProductA2 extends AbstractProductA {
@Override
public void doSomething() {
System.out.println("产品A2的实现方法");
}
}

View File

@ -0,0 +1,15 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 产品A的实现类
*/
public class ProductB1 extends AbstractProductB {
@Override
public void doSomething() {
System.out.println("产品B1的实现方法");
}
}

View File

@ -0,0 +1,15 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 产品A的实现类
*/
public class ProductB2 extends AbstractProductB {
@Override
public void doSomething() {
System.out.println("产品B2的实现方法");
}
}

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>design-pattern</artifactId>
<groupId>com.zhq</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhq</groupId>
<artifactId>abstract_factory_vs_builder</artifactId>
</project>

View File

@ -0,0 +1,18 @@
package com.company.abstract_factory;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public abstract class AbsBMW implements ICar {
private final static String BMW_BAND = "宝马汽车";
//宝马车
public String getBand() {
return BMW_BAND;
}
//型号由具体的实现类实现
public abstract String getModel();
}

View File

@ -0,0 +1,18 @@
package com.company.abstract_factory;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public abstract class AbsBenz implements ICar {
private final static String BENZ_BAND = "奔驰汽车";
public String getBand() {
return BENZ_BAND;
}
//具体型号由实现类完成
public abstract String getModel();
}

View File

@ -0,0 +1,19 @@
package com.company.abstract_factory;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class BMWFactory implements CarFactory {
//生产SUV
public ICar createSuv() {
return new BMWSuv();
}
//生产商务车
public ICar createVan(){
return new BMWVan();
}
}

View File

@ -0,0 +1,15 @@
package com.company.abstract_factory;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class BMWSuv extends AbsBMW {
private final static String X_SEARIES = "X系列车型SUV";
@Override
public String getModel() {
return X_SEARIES;
}
}

View File

@ -0,0 +1,15 @@
package com.company.abstract_factory;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class BMWVan extends AbsBMW {
private final static String SEVENT_SEARIES = "7系列车型商务车";
@Override
public String getModel() {
return SEVENT_SEARIES;
}
}

View File

@ -0,0 +1,18 @@
package com.company.abstract_factory;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class BenzFactory implements CarFactory {
//生产SUV
public ICar createSuv() {
return new BenzSuv();
}
//生产商务车
public ICar createVan(){
return new BenzVan();
}
}

View File

@ -0,0 +1,15 @@
package com.company.abstract_factory;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class BenzSuv extends AbsBenz {
private final static String G_SERIES = "G系列SUV";
@Override
public String getModel() {
return G_SERIES;
}
}

View File

@ -0,0 +1,15 @@
package com.company.abstract_factory;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class BenzVan extends AbsBenz {
private final static String R_SERIES = "R系列商务车";
@Override
public String getModel() {
return R_SERIES;
}
}

View File

@ -0,0 +1,14 @@
package com.company.abstract_factory;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public interface CarFactory {
//生产SUV
public ICar createSuv();
//生产商务车
public ICar createVan();
}

View File

@ -0,0 +1,23 @@
package com.company.abstract_factory;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class Client {
public static void main(String[] args) {
//给我生产一辆奔驰SUV
System.out.println("===要求生产一辆奔驰SUV===");
//首先找到生产奔驰车的工厂
System.out.println("A、找到奔驰车工厂");
CarFactory carFactory= new BenzFactory();
//开始生产奔驰SUV
System.out.println("B、开始生产奔驰SUV");
ICar benzSuv = carFactory.createSuv();
//生产完毕展示一下车辆信息
System.out.println("C、生产出的汽车如下");
System.out.println("汽车品牌:"+benzSuv.getBand());
System.out.println("汽车型号:" + benzSuv.getModel());
}
}

View File

@ -0,0 +1,16 @@
package com.company.abstract_factory;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public interface ICar {
//汽车的生产商也就是牌子
public String getBand();
//汽车的型号
public String getModel();
}

View File

@ -0,0 +1,19 @@
package com.company.builder;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class BMWBuilder extends CarBuilder {
@Override
public String buildEngine() {
return super.getBlueprint().getEngine();
}
@Override
public String buildWheel() {
return super.getBlueprint().getWheel();
}
}

View File

@ -0,0 +1,19 @@
package com.company.builder;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class BenzBuilder extends CarBuilder {
@Override
public String buildEngine() {
return super.getBlueprint().getEngine();
}
@Override
public String buildWheel() {
return super.getBlueprint().getWheel();
}
}

View File

@ -0,0 +1,25 @@
package com.company.builder;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class Blueprint {
//车轮的要求
private String wheel;
//引擎的要求
private String engine;
public String getWheel() {
return wheel;
}
public void setWheel(String wheel) {
this.wheel = wheel;
}
public String getEngine() {
return engine;
}
public void setEngine(String engine) {
this.engine = engine;
}
}

View File

@ -0,0 +1,33 @@
package com.company.builder;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class Car implements ICar {
//汽车引擎
private String engine;
//汽车轮子
private String wheel;
//一次性传递汽车需要的信息
public Car(String _engine,String _wheel){
this.engine = _engine;
this.wheel = _wheel;
}
public String getEngine() {
return engine;
}
public String getWheel() {
return wheel;
}
@Override
public String toString(){
return "车的轮子是:" + wheel + "\n车的引擎是" + engine;
}
}

View File

@ -0,0 +1,33 @@
package com.company.builder;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public abstract class CarBuilder {
//待建造的汽车
private ICar car;
//设计蓝图
private Blueprint bp;
public Car buildCar(){
//按照顺序生产一辆车
return new Car(buildEngine(),buildWheel());
}
//接收一份设计蓝图
public void receiveBlueprint(Blueprint _bp){
this.bp = _bp;
}
//查看蓝图只有真正的建造者才可以查看蓝图
protected Blueprint getBlueprint(){
return bp;
}
//建造轮子
protected abstract String buildWheel();
//建造引擎
protected abstract String buildEngine();
}

View File

@ -0,0 +1,26 @@
package com.company.builder;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class Client {
public static void main(String[] args) {
//定义出导演类
Director director =new Director();
//给我一辆奔驰车SUV
System.out.println("===制造一辆奔驰的SUV===");
ICar benzSuv = director.createBenzSuv();
System.out.println(benzSuv);
//给我一辆宝马的商务车
System.out.println("\n===制造一辆宝马的商务车===");
ICar bmwVan = director.createBMWVan();
System.out.println(bmwVan);
//给我一辆混合车型
System.out.println("\n===制造一辆混合车===");
ICar complexCar = director.createComplexCar();
System.out.println(complexCar);
}
}

View File

@ -0,0 +1,41 @@
package com.company.builder;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class Director {
//声明对建造者的引用
private CarBuilder benzBuilder = new BenzBuilder();
private CarBuilder bmwBuilder = new BMWBuilder();
//生产奔驰SUV车型
public ICar createBenzSuv(){
//制造出汽车
return createCar(benzBuilder, "benz的引擎", "benz的轮胎");
}
//生产出一辆宝马的商务车
public ICar createBMWVan(){
return createCar(benzBuilder, "BMW的引擎", "BMW的轮胎");
}
//生产出一个混合车型
public ICar createComplexCar(){
return createCar(bmwBuilder, "BMW的引擎", "benz的轮胎");
}
//生产车辆
private ICar createCar(CarBuilder _carBuilder,String engine,String wheel){
//导演怀揣蓝图
Blueprint bp = new Blueprint();
bp.setEngine(engine);
bp.setWheel(wheel);
System.out.println("获得生产蓝图");
_carBuilder.receiveBlueprint(bp);
return _carBuilder.buildCar();
}
}

View File

@ -0,0 +1,14 @@
package com.company.builder;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
//汽车接口
public interface ICar {
//汽车的车轮
public String getWheel();
//汽车引擎
public String getEngine();
}

16
adapter/pom.xml Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>design-pattern</artifactId>
<groupId>com.zhq</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhq</groupId>
<artifactId>adapter</artifactId>
</project>

View File

@ -0,0 +1,25 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 这就是我们具体的应用了比如老板要查所有的20-30的女性信息
*/
public class App {
public static void main(String[] args) {
//没有与外系统连接的时候是这样写的
//IUserInfo youngGirl = new UserInfo();
//老板一想不对呀兔子不吃窝边草还是找人力资源的员工好点
IUserInfo youngGirl = new OuterUserInfo(); //我们只修改了这一句好
//从数据库中查到101个
for(int i=0;i<101;i++){
youngGirl.getMobileNumber();
}
}
}

View File

@ -0,0 +1,22 @@
package com.company.section1;
import java.util.Map;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 外系统的人员信息
*/
@SuppressWarnings("all")
public interface IOuterUser {
//基本信息比如名称性别手机号码了等
public Map getUserBaseInfo();
//工作区域信息
public Map getUserOfficeInfo();
//用户的家庭信息
public Map getUserHomeInfo();
}

View File

@ -0,0 +1,27 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 用户信息对象
*/
public interface IUserInfo {
//获得用户姓名
public String getUserName();
//获得家庭地址
public String getHomeAddress();
//手机号码这个太重要手机泛滥呀
public String getMobileNumber();
//办公电话一般式座机
public String getOfficeTelNumber();
//这个人的职位是啥
public String getJobPosition();
//获得家庭电话这个有点缺德我是不喜欢打家庭电话讨论工作
public String getHomeTelNumber();
}

View File

@ -0,0 +1,50 @@
package com.company.section1;
import java.util.HashMap;
import java.util.Map;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 外系统的用户信息的实现类
*/
@SuppressWarnings("all")
public class OuterUser implements IOuterUser {
/*
* 用户的基本信息
*/
public Map getUserBaseInfo() {
HashMap baseInfoMap = new HashMap();
baseInfoMap.put("userName", "这个员工叫混世魔王....");
baseInfoMap.put("mobileNumber", "这个员工电话是....");
return baseInfoMap;
}
/*
* 员工的家庭信息
*/
public Map getUserHomeInfo() {
HashMap homeInfo = new HashMap();
homeInfo.put("homeTelNumbner", "员工的家庭电话是....");
homeInfo.put("homeAddress", "员工的家庭地址是....");
return homeInfo;
}
/*
* 员工的工作信息比如职位了等
*/
public Map getUserOfficeInfo() {
HashMap officeInfo = new HashMap();
officeInfo.put("jobPosition","这个人的职位是BOSS...");
officeInfo.put("officeTelNumber", "员工的办公电话是....");
return officeInfo;
}
}

View File

@ -0,0 +1,71 @@
package com.company.section1;
import java.util.Map;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 把OuterUser包装成UserInfo
*/
@SuppressWarnings("all")
public class OuterUserInfo extends OuterUser implements IUserInfo {
private Map baseInfo = super.getUserBaseInfo(); //员工的基本信息
private Map homeInfo = super.getUserHomeInfo(); //员工的家庭 信息
private Map officeInfo = super.getUserOfficeInfo(); //工作信息
/*
* 家庭地址
*/
public String getHomeAddress() {
String homeAddress = (String)this.homeInfo.get("homeAddress");
System.out.println(homeAddress);
return homeAddress;
}
/*
* 家庭电话号码
*/
public String getHomeTelNumber() {
String homeTelNumber = (String)this.homeInfo.get("homeTelNumber");
System.out.println(homeTelNumber);
return homeTelNumber;
}
/*
*职位信息
*/
public String getJobPosition() {
String jobPosition = (String)this.officeInfo.get("jobPosition");
System.out.println(jobPosition);
return jobPosition;
}
/*
* 手机号码
*/
public String getMobileNumber() {
String mobileNumber = (String)this.baseInfo.get("mobileNumber");
System.out.println(mobileNumber);
return mobileNumber;
}
/*
* 办公电话
*/
public String getOfficeTelNumber() {
String officeTelNumber = (String)this.officeInfo.get("officeTelNumber");
System.out.println(officeTelNumber);
return officeTelNumber;
}
/*
* 员工的名称
*/
public String getUserName() {
String userName = (String)this.baseInfo.get("userName");
System.out.println(userName);
return userName;
}
}

View File

@ -0,0 +1,57 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class UserInfo implements IUserInfo {
/*
* 获得家庭地址下属送礼也可以找到地方
*/
public String getHomeAddress() {
System.out.println("这里是员工的家庭地址....");
return null;
}
/*
* 获得家庭电话号码
*/
public String getHomeTelNumber() {
System.out.println("员工的家庭电话是....");
return null;
}
/*
* 员工的职位是部门经理还是小兵
*/
public String getJobPosition() {
System.out.println("这个人的职位是BOSS....");
return null;
}
/*
* 手机号码
*/
public String getMobileNumber() {
System.out.println("这个人的手机号码是0000....");
return null;
}
/*
* 办公室电话烦躁的时候最好不小心把电话线踢掉我经常这么干对己对人都有好处
*/
public String getOfficeTelNumber() {
System.out.println("办公室电话是....");
return null;
}
/*
* 姓名了这个老重要了
*/
public String getUserName() {
System.out.println("姓名叫做...");
return null;
}
}

View File

@ -0,0 +1,14 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 源角色
*/
public class Adaptee {
//原有的业务逻辑
public void doSomething(){
System.out.println("I'm kind of busy,leave me alone,pls!");
}
}

View File

@ -0,0 +1,14 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 适配器
*/
public class Adapter extends Adaptee implements Target {
public void request() {
super.doSomething();
}
}

View File

@ -0,0 +1,18 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class Client {
public static void main(String[] args) {
//原有的业务逻辑
Target target = new ConcreteTarget();
target.request();
//现在增加了适配器角色后的业务逻辑
Target target2 = new Adapter();
target2.request();
}
}

View File

@ -0,0 +1,12 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class ConcreteTarget implements Target {
public void request() {
System.out.println("I have nothing to do. if you need any help,pls call me!"); }
}

View File

@ -0,0 +1,12 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 目标角色
*/
public interface Target {
//目标角色有自己的方法
public void request();
}

View File

@ -0,0 +1,27 @@
package com.company.section3;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 这就是我们具体的应用了比如老板要查所有的20-30的女性信息
*/
public class Client {
public static void main(String[] args) {
//外系统的人员信息
IOuterUserBaseInfo baseInfo = new OuterUserBaseInfo();
IOuterUserHomeInfo homeInfo = new OuterUserHomeInfo();
IOuterUserOfficeInfo officeInfo = new OuterUserOfficeInfo();
//传递三个对象
IUserInfo youngGirl = new OuterUserInfo(baseInfo,homeInfo,officeInfo);
//从数据库中查到101个
for(int i=0;i<101;i++){
youngGirl.getMobileNumber();
}
}
}

View File

@ -0,0 +1,12 @@
package com.company.section3;
import java.util.Map;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public interface IOuterUserBaseInfo {
//基本信息比如名称性别手机号码了等
public Map getUserBaseInfo();
}

View File

@ -0,0 +1,12 @@
package com.company.section3;
import java.util.Map;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public interface IOuterUserHomeInfo {
//用户的家庭信息
public Map getUserHomeInfo();
}

View File

@ -0,0 +1,12 @@
package com.company.section3;
import java.util.Map;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public interface IOuterUserOfficeInfo {
//工作区域信息
public Map getUserOfficeInfo();
}

View File

@ -0,0 +1,27 @@
package com.company.section3;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 用户信息对象
*/
public interface IUserInfo {
//获得用户姓名
public String getUserName();
//获得家庭地址
public String getHomeAddress();
//手机号码这个太重要手机泛滥呀
public String getMobileNumber();
//办公电话一般式座机
public String getOfficeTelNumber();
//这个人的职位是啥
public String getJobPosition();
//获得家庭电话这个有点缺德我是不喜欢打家庭电话讨论工作
public String getHomeTelNumber();
}

View File

@ -0,0 +1,24 @@
package com.company.section3;
import java.util.HashMap;
import java.util.Map;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class OuterUserBaseInfo implements IOuterUserBaseInfo {
/*
* 用户的基本信息
*/
public Map getUserBaseInfo() {
HashMap baseInfoMap = new HashMap();
baseInfoMap.put("userName", "这个员工叫混世魔王....");
baseInfoMap.put("mobileNumber", "这个员工电话是....");
return baseInfoMap;
}
}

View File

@ -0,0 +1,23 @@
package com.company.section3;
import java.util.HashMap;
import java.util.Map;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class OuterUserHomeInfo implements IOuterUserHomeInfo {
/*
* 员工的家庭信息
*/
public Map getUserHomeInfo() {
HashMap homeInfo = new HashMap();
homeInfo.put("homeTelNumbner", "员工的家庭电话是....");
homeInfo.put("homeAddress", "员工的家庭地址是....");
return homeInfo;
}
}

View File

@ -0,0 +1,76 @@
package com.company.section3;
import java.util.Map;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 把OuterUser包装成UserInfo
*/
@SuppressWarnings("all")
public class OuterUserInfo implements IUserInfo {
//源目标对象
private IOuterUserBaseInfo baseInfo = null; //员工的基本信息
private IOuterUserHomeInfo homeInfo = null; //员工的家庭 信息
private IOuterUserOfficeInfo officeInfo = null; //工作信息
//数据处理
private Map baseMap = null;
private Map homeMap = null;
private Map officeMap = null;
//构造函数传递对象
public OuterUserInfo(IOuterUserBaseInfo _baseInfo,IOuterUserHomeInfo _homeInfo,IOuterUserOfficeInfo _officeInfo){
this.baseInfo = _baseInfo;
this.homeInfo = _homeInfo;
this.officeInfo = _officeInfo;
//数据处理
this.baseMap = this.baseInfo.getUserBaseInfo();
this.homeMap = this.homeInfo.getUserHomeInfo();
this.officeMap = this.officeInfo.getUserOfficeInfo();
}
//家庭地址
public String getHomeAddress() {
String homeAddress = (String)this.homeMap.get("homeAddress");
System.out.println(homeAddress);
return homeAddress;
}
//家庭电话号码
public String getHomeTelNumber() {
String homeTelNumber = (String)this.homeMap.get("homeTelNumber");
System.out.println(homeTelNumber);
return homeTelNumber;
}
//职位信息
public String getJobPosition() {
String jobPosition = (String)this.officeMap.get("jobPosition");
System.out.println(jobPosition);
return jobPosition;
}
//手机号码
public String getMobileNumber() {
String mobileNumber = (String)this.baseMap.get("mobileNumber");
System.out.println(mobileNumber);
return mobileNumber;
}
//办公电话
public String getOfficeTelNumber() {
String officeTelNumber = (String)this.officeMap.get("officeTelNumber");
System.out.println(officeTelNumber);
return officeTelNumber;
}
// 员工的名称
public String getUserName() {
String userName = (String)this.baseMap.get("userName");
System.out.println(userName);
return userName;
}
}

View File

@ -0,0 +1,24 @@
package com.company.section3;
import java.util.HashMap;
import java.util.Map;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class OuterUserOfficeInfo implements IOuterUserOfficeInfo {
/*
* 员工的工作信息比如职位了等
*/
public Map getUserOfficeInfo() {
HashMap officeInfo = new HashMap();
officeInfo.put("jobPosition","这个人的职位是BOSS...");
officeInfo.put("officeTelNumber", "员工的办公电话是....");
return officeInfo;
}
}

View File

@ -0,0 +1,57 @@
package com.company.section3;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class UserInfo implements IUserInfo {
/*
* 获得家庭地址下属送礼也可以找到地方
*/
public String getHomeAddress() {
System.out.println("这里是员工的家庭地址....");
return null;
}
/*
* 获得家庭电话号码
*/
public String getHomeTelNumber() {
System.out.println("员工的家庭电话是....");
return null;
}
/*
* 员工的职位是部门经理还是小兵
*/
public String getJobPosition() {
System.out.println("这个人的职位是BOSS....");
return null;
}
/*
* 手机号码
*/
public String getMobileNumber() {
System.out.println("这个人的手机号码是0000....");
return null;
}
/*
* 办公室电话烦躁的时候最好不小心把电话线踢掉我经常这么干对己对人都有好处
*/
public String getOfficeTelNumber() {
System.out.println("办公室电话是....");
return null;
}
/*
* 姓名了这个老重要了
*/
public String getUserName() {
System.out.println("姓名叫做...");
return null;
}
}

16
bridge/pom.xml Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>design-pattern</artifactId>
<groupId>com.zhq</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhq</groupId>
<artifactId>bridge</artifactId>
</project>

View File

@ -0,0 +1,22 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 我要关心我自己的公司了
*/
public class Client {
public static void main(String[] args) {
System.out.println("-------房地产公司是这个样子运行的-------");
//先找到我的公司
HouseCorp houseCorp =new HouseCorp();
//看我怎么挣钱
houseCorp.makeMoney();
System.out.println("\n");
System.out.println("-------服装公司是这样运行的-------");
ClothesCorp clothesCorp = new ClothesCorp();
clothesCorp.makeMoney();
}
}

View File

@ -0,0 +1,25 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*
*/
public class ClothesCorp extends Corp {
//服装公司生产的就是衣服了
protected void produce() {
System.out.println("服装公司生产衣服...");
}
//服装公司卖服装可只卖服装不卖穿衣服的模特
protected void sell() {
System.out.println("服装公司出售衣服...");
}
//服装公司不景气但怎么说也是赚钱行业
public void makeMoney(){
super.makeMoney();
System.out.println("服装公司赚小钱...");
}
}

View File

@ -0,0 +1,31 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public abstract class Corp {
/*
* 是公司就应该有生产把甭管是什么软件公司还是制造业公司
* 那每个公司的生产的东西都不一样所以由实现类来完成
*/
protected abstract void produce();
/*
* 有产品了那肯定要销售呀不销售你公司怎么生存
*/
protected abstract void sell();
//公司是干什么的赚钱的呀不赚钱傻子才干
public void makeMoney(){
//每个公司都是一样先生产
this.produce();
//然后销售
this.sell();
}
}

View File

@ -0,0 +1,23 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 房地产公司按照翻译来说应该叫realty corp这个是比较准确的翻译
* 但是我问你说个房地产公司要你翻译成英文你第一反映什么对嘛还是house corp!
*/
public class HouseCorp extends Corp {
//房地产公司就是盖房子
protected void produce() {
System.out.println("房地产公司盖房子...");
}
//房地产卖房子自己住那可不赚钱
protected void sell() {
System.out.println("房地产公司出售房子...");
}
//房地产公司很High了赚钱计算利润
public void makeMoney(){
super.makeMoney();
System.out.println("房地产公司赚大钱了...");
}
}

View File

@ -0,0 +1,23 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 我要关心我自己的公司了
*/
public class Client {
public static void main(String[] args) {
System.out.println("-------房地产公司是这个样子运行的-------");
//先找到我的公司
HouseCorp houseCorp =new HouseCorp();
//看我怎么挣钱
houseCorp.makeMoney();
System.out.println("\n");
System.out.println("-------山寨公司是这样运行的-------");
IPodCorp iPodCorp = new IPodCorp();
iPodCorp.makeMoney();
}
}

View File

@ -0,0 +1,31 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public abstract class Corp {
/*
* 是公司就应该有生产把甭管是什么软件公司还是制造业公司
* 那每个公司的生产的东西都不一样所以由实现类来完成
*/
protected abstract void produce();
/*
* 有产品了那肯定要销售呀不销售你公司怎么生存
*/
protected abstract void sell();
//公司是干什么的赚钱的呀不赚钱傻子才干
public void makeMoney(){
//每个公司都是一样先生产
this.produce();
//然后销售
this.sell();
}
}

View File

@ -0,0 +1,23 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 房地产公司按照翻译来说应该叫realty corp这个是比较准确的翻译
* 但是我问你说个房地产公司要你翻译成英文你第一反映什么对嘛还是house corp!
*/
public class HouseCorp extends Corp {
//房地产公司就是盖房子
protected void produce() {
System.out.println("房地产公司盖房子...");
}
//房地产卖房子自己住那可不赚钱
protected void sell() {
System.out.println("房地产公司出售房子...");
}
//房地产公司很High了赚钱计算利润
public void makeMoney(){
super.makeMoney();
System.out.println("房地产公司赚大钱了...");
}
}

View File

@ -0,0 +1,26 @@
package com.company.section2;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 我是山寨老大你流行啥我就生茶啥
*/
public class IPodCorp extends Corp {
//我开始生产iPod了
protected void produce() {
System.out.println("我生产iPod...");
}
//山寨的iPod很畅销便宜呀
protected void sell() {
System.out.println("iPod畅销...");
}
//狂赚钱
public void makeMoney(){
super.makeMoney();
System.out.println("我赚钱呀...");
}
}

View File

@ -0,0 +1,24 @@
package com.company.section3;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class Client {
public static void main(String[] args) {
House house = new House();
System.out.println("-------房地产公司是这个样子运行的-------");
//先找到房地产公司
HouseCorp houseCorp =new HouseCorp(house);
//看我怎么挣钱
houseCorp.makeMoney();
System.out.println("\n");
//山寨公司生产的产品很多不过我只要指定产品就成了
System.out.println("-------山寨公司是这样运行的-------");
ShanZhaiCorp shanZhaiCorp = new ShanZhaiCorp(new IPod());
shanZhaiCorp.makeMoney();
}
}

View File

@ -0,0 +1,22 @@
package com.company.section3;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public abstract class Corp {
//定义一个产品对象抽象的了不知道具体是什么产品
private Product product;
//构造函数由子类定义传递具体的产品进来
public Corp(Product product){
this.product = product;
}
//公司是干什么的赚钱的呀不赚钱傻子才干
public void makeMoney(){
//每个公司都是一样先生产
this.product.beProducted();
//然后销售
this.product.beSelled();
}
}

View File

@ -0,0 +1,17 @@
package com.company.section3;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class House extends Product {
//豆腐渣就豆腐渣呗好歹也是个房子
public void beProducted() {
System.out.println("生产出的房子是这个样子的...");
}
//虽然是豆腐渣也是能够销售出去的
public void beSelled() {
System.out.println("生产出的房子卖出去了...");
}
}

View File

@ -0,0 +1,18 @@
package com.company.section3;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class HouseCorp extends Corp {
//定义传递一个House产品进来
public HouseCorp(House house){
super(house);
}
//房地产公司很High了赚钱计算利润
public void makeMoney(){
super.makeMoney();
System.out.println("房地产公司赚大钱了...");
}
}

View File

@ -0,0 +1,16 @@
package com.company.section3;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class IPod extends Product {
public void beProducted() {
System.out.println("生产出的iPod是这个样子的...");
}
public void beSelled() {
System.out.println("生产出的iPod卖出去了...");
}
}

View File

@ -0,0 +1,13 @@
package com.company.section3;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public abstract class Product {
//甭管是什么产品它总要是能被生产出来
public abstract void beProducted();
//生产出来的东西一定要销售出去否则亏本呀
public abstract void beSelled();
}

View File

@ -0,0 +1,18 @@
package com.company.section3;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class ShanZhaiCorp extends Corp {
//产什么产品不知道等被调用的才知道
public ShanZhaiCorp(Product product){
super(product);
}
//狂赚钱
public void makeMoney(){
super.makeMoney();
System.out.println("我赚钱呀...");
}
}

View File

@ -0,0 +1,27 @@
package com.company.section4;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class Client {
public static void main(String[] args) {
House house = new House();
System.out.println("-------房地产公司是这个样子运行的-------");
//先找到房地产公司
HouseCorp houseCorp =new HouseCorp(house);
//看我怎么挣钱
houseCorp.makeMoney();
System.out.println("\n");
//山寨公司生产的产品很多不过我只要指定产品就成了
System.out.println("-------山寨公司是这样运行的-------");
ShanZhaiCorp shanZhaiCorp = new ShanZhaiCorp(new Clothes());
shanZhaiCorp.makeMoney();
}
}

View File

@ -0,0 +1,18 @@
package com.company.section4;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class Clothes extends Product {
public void beProducted() {
System.out.println("生产出的衣服是这个样子的...");
}
public void beSelled() {
System.out.println("生产出的衣服卖出去了...");
}
}

View File

@ -0,0 +1,22 @@
package com.company.section4;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public abstract class Corp {
//定义一个产品对象抽象的了不知道具体是什么产品
private Product product;
//构造函数由子类定义传递具体的产品进来
public Corp(Product product){
this.product = product;
}
//公司是干什么的赚钱的呀不赚钱傻子才干
public void makeMoney(){
//每个公司都是一样先生产
this.product.beProducted();
//然后销售
this.product.beSelled();
}
}

View File

@ -0,0 +1,17 @@
package com.company.section4;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class House extends Product {
//豆腐渣就豆腐渣呗好歹也是个房子
public void beProducted() {
System.out.println("生产出的房子是这个样子的...");
}
//虽然是豆腐渣也是能够销售出去的
public void beSelled() {
System.out.println("生产出的房子卖出去了...");
}
}

View File

@ -0,0 +1,18 @@
package com.company.section4;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class HouseCorp extends Corp {
//定义传递一个House产品进来
public HouseCorp(House house){
super(house);
}
//房地产公司很High了赚钱计算利润
public void makeMoney(){
super.makeMoney();
System.out.println("房地产公司赚大钱了...");
}
}

View File

@ -0,0 +1,13 @@
package com.company.section4;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public abstract class Product {
//甭管是什么产品它总要是能被生产出来
public abstract void beProducted();
//生产出来的东西一定要销售出去否则亏本呀
public abstract void beSelled();
}

View File

@ -0,0 +1,18 @@
package com.company.section4;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class ShanZhaiCorp extends Corp {
//产什么产品不知道等被调用的才知道
public ShanZhaiCorp(Product product){
super(product);
}
//狂赚钱
public void makeMoney(){
super.makeMoney();
System.out.println("我赚钱呀...");
}
}

View File

@ -0,0 +1,26 @@
package com.company.section5;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public abstract class Abstraction {
//定义对实现化角色的引用
private Implementor imp;
//约束子类必须实现该构造函数
public Abstraction(Implementor _imp){
this.imp = _imp;
}
//自身的行为和属性
public void request(){
this.imp.doSomething();
}
//获得实现化角色
public Implementor getImp(){
return imp;
}
}

View File

@ -0,0 +1,18 @@
package com.company.section5;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class Client {
public static void main(String[] args) {
//定义一个实现化角色
Implementor imp = new ConcreteImplementor1();
//定义一个抽象化角色
Abstraction abs = new RefinedAbstraction(imp);
//执行行文
abs.request();
}
}

View File

@ -0,0 +1,16 @@
package com.company.section5;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class ConcreteImplementor1 implements Implementor{
public void doSomething(){
//业务逻辑处理
}
public void doAnything(){
//业务逻辑处理
}
}

View File

@ -0,0 +1,16 @@
package com.company.section5;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class ConcreteImplementor2 implements Implementor{
public void doSomething(){
//业务逻辑处理
}
public void doAnything(){
//业务逻辑处理
}
}

View File

@ -0,0 +1,13 @@
package com.company.section5;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public interface Implementor {
//基本方法
public void doSomething();
public void doAnything();
}

View File

@ -0,0 +1,24 @@
package com.company.section5;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
*/
public class RefinedAbstraction extends Abstraction {
//覆写构造函数
public RefinedAbstraction(Implementor _imp){
super(_imp);
}
//修正父类的行文
@Override
public void request(){
/*
* 业务处理....
*/
super.request();
super.getImp().doAnything();
}
}

16
builder/pom.xml Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>design-pattern</artifactId>
<groupId>com.zhq</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhq</groupId>
<artifactId>builder</artifactId>
</project>

View File

@ -0,0 +1,34 @@
package com.company.section1;
/**
* @author zhuyijunm
* I'm glad to share my knowledge with you all.
* 宝马车模型
*/
public class BMWModel extends CarModel {
@Override
protected void alarm() {
System.out.println("宝马车的喇叭声音是这个样子的...");
}
@Override
protected void engineBoom() {
System.out.println("宝马车的引擎室这个声音的...");
}
@Override
protected void start() {
System.out.println("宝马车跑起来是这个样子的...");
}
@Override
protected void stop() {
System.out.println("宝马车应该这样停车...");
}
}

Some files were not shown because too many files have changed in this diff Show More