设计模式之禅
This commit is contained in:
commit
1058526492
27
.gitignore
vendored
Normal file
27
.gitignore
vendored
Normal 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
87
README.md
Normal 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
16
abstract_factory/pom.xml
Normal 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>
|
@ -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("黑人会说话,一般人听不懂。");
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -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("白色人种会说话,一般都是但是单字节。");
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -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("黄色人种会说话,一般说的都是双字节。");
|
||||
}
|
||||
}
|
@ -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("黑人女性");
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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("白人女性");
|
||||
}
|
||||
|
||||
}
|
@ -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("黄人女性");
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
@ -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("黑人男性");
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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("白人男性");
|
||||
}
|
||||
|
||||
}
|
@ -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("黄人男性");
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* .....
|
||||
* 后面你可以续了
|
||||
*/
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
@ -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();
|
||||
}
|
@ -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();
|
||||
}
|
@ -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();
|
||||
|
||||
/*
|
||||
* 然后在这里就可以为所欲为了...
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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的实现方法");
|
||||
}
|
||||
|
||||
}
|
@ -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的实现方法");
|
||||
}
|
||||
|
||||
}
|
@ -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的实现方法");
|
||||
}
|
||||
|
||||
}
|
@ -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的实现方法");
|
||||
}
|
||||
|
||||
}
|
16
abstract_factory_vs_builder/pom.xml
Normal file
16
abstract_factory_vs_builder/pom.xml
Normal 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>
|
@ -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();
|
||||
|
||||
}
|
@ -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();
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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
16
adapter/pom.xml
Normal 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>
|
25
adapter/src/main/java/com/company/section1/App.java
Normal file
25
adapter/src/main/java/com/company/section1/App.java
Normal 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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
22
adapter/src/main/java/com/company/section1/IOuterUser.java
Normal file
22
adapter/src/main/java/com/company/section1/IOuterUser.java
Normal 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();
|
||||
|
||||
}
|
27
adapter/src/main/java/com/company/section1/IUserInfo.java
Normal file
27
adapter/src/main/java/com/company/section1/IUserInfo.java
Normal 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();
|
||||
}
|
50
adapter/src/main/java/com/company/section1/OuterUser.java
Normal file
50
adapter/src/main/java/com/company/section1/OuterUser.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
57
adapter/src/main/java/com/company/section1/UserInfo.java
Normal file
57
adapter/src/main/java/com/company/section1/UserInfo.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
14
adapter/src/main/java/com/company/section2/Adaptee.java
Normal file
14
adapter/src/main/java/com/company/section2/Adaptee.java
Normal 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!");
|
||||
}
|
||||
}
|
14
adapter/src/main/java/com/company/section2/Adapter.java
Normal file
14
adapter/src/main/java/com/company/section2/Adapter.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
18
adapter/src/main/java/com/company/section2/Client.java
Normal file
18
adapter/src/main/java/com/company/section2/Client.java
Normal 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();
|
||||
}
|
||||
}
|
@ -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!"); }
|
||||
|
||||
}
|
12
adapter/src/main/java/com/company/section2/Target.java
Normal file
12
adapter/src/main/java/com/company/section2/Target.java
Normal 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();
|
||||
}
|
27
adapter/src/main/java/com/company/section3/Client.java
Normal file
27
adapter/src/main/java/com/company/section3/Client.java
Normal 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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
@ -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();
|
||||
}
|
@ -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();
|
||||
}
|
27
adapter/src/main/java/com/company/section3/IUserInfo.java
Normal file
27
adapter/src/main/java/com/company/section3/IUserInfo.java
Normal 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();
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
57
adapter/src/main/java/com/company/section3/UserInfo.java
Normal file
57
adapter/src/main/java/com/company/section3/UserInfo.java
Normal 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
16
bridge/pom.xml
Normal 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>
|
22
bridge/src/main/java/com/company/section1/Client.java
Normal file
22
bridge/src/main/java/com/company/section1/Client.java
Normal 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();
|
||||
}
|
||||
}
|
||||
|
25
bridge/src/main/java/com/company/section1/ClothesCorp.java
Normal file
25
bridge/src/main/java/com/company/section1/ClothesCorp.java
Normal 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("服装公司赚小钱...");
|
||||
}
|
||||
}
|
||||
|
||||
|
31
bridge/src/main/java/com/company/section1/Corp.java
Normal file
31
bridge/src/main/java/com/company/section1/Corp.java
Normal 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();
|
||||
|
||||
}
|
||||
|
||||
}
|
23
bridge/src/main/java/com/company/section1/HouseCorp.java
Normal file
23
bridge/src/main/java/com/company/section1/HouseCorp.java
Normal 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("房地产公司赚大钱了...");
|
||||
}
|
||||
}
|
23
bridge/src/main/java/com/company/section2/Client.java
Normal file
23
bridge/src/main/java/com/company/section2/Client.java
Normal 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
31
bridge/src/main/java/com/company/section2/Corp.java
Normal file
31
bridge/src/main/java/com/company/section2/Corp.java
Normal 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();
|
||||
|
||||
}
|
||||
|
||||
}
|
23
bridge/src/main/java/com/company/section2/HouseCorp.java
Normal file
23
bridge/src/main/java/com/company/section2/HouseCorp.java
Normal 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("房地产公司赚大钱了...");
|
||||
}
|
||||
}
|
26
bridge/src/main/java/com/company/section2/IPodCorp.java
Normal file
26
bridge/src/main/java/com/company/section2/IPodCorp.java
Normal 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("我赚钱呀...");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
24
bridge/src/main/java/com/company/section3/Client.java
Normal file
24
bridge/src/main/java/com/company/section3/Client.java
Normal 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();
|
||||
}
|
||||
}
|
||||
|
22
bridge/src/main/java/com/company/section3/Corp.java
Normal file
22
bridge/src/main/java/com/company/section3/Corp.java
Normal 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();
|
||||
}
|
||||
}
|
||||
|
17
bridge/src/main/java/com/company/section3/House.java
Normal file
17
bridge/src/main/java/com/company/section3/House.java
Normal 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("生产出的房子卖出去了...");
|
||||
}
|
||||
}
|
||||
|
18
bridge/src/main/java/com/company/section3/HouseCorp.java
Normal file
18
bridge/src/main/java/com/company/section3/HouseCorp.java
Normal 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("房地产公司赚大钱了...");
|
||||
}
|
||||
}
|
||||
|
16
bridge/src/main/java/com/company/section3/IPod.java
Normal file
16
bridge/src/main/java/com/company/section3/IPod.java
Normal 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卖出去了...");
|
||||
}
|
||||
}
|
||||
|
13
bridge/src/main/java/com/company/section3/Product.java
Normal file
13
bridge/src/main/java/com/company/section3/Product.java
Normal 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();
|
||||
}
|
||||
|
18
bridge/src/main/java/com/company/section3/ShanZhaiCorp.java
Normal file
18
bridge/src/main/java/com/company/section3/ShanZhaiCorp.java
Normal 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("我赚钱呀...");
|
||||
}
|
||||
}
|
||||
|
27
bridge/src/main/java/com/company/section4/Client.java
Normal file
27
bridge/src/main/java/com/company/section4/Client.java
Normal 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();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
18
bridge/src/main/java/com/company/section4/Clothes.java
Normal file
18
bridge/src/main/java/com/company/section4/Clothes.java
Normal 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("生产出的衣服卖出去了...");
|
||||
}
|
||||
}
|
||||
|
||||
|
22
bridge/src/main/java/com/company/section4/Corp.java
Normal file
22
bridge/src/main/java/com/company/section4/Corp.java
Normal 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();
|
||||
}
|
||||
}
|
||||
|
17
bridge/src/main/java/com/company/section4/House.java
Normal file
17
bridge/src/main/java/com/company/section4/House.java
Normal 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("生产出的房子卖出去了...");
|
||||
}
|
||||
}
|
||||
|
18
bridge/src/main/java/com/company/section4/HouseCorp.java
Normal file
18
bridge/src/main/java/com/company/section4/HouseCorp.java
Normal 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("房地产公司赚大钱了...");
|
||||
}
|
||||
}
|
||||
|
13
bridge/src/main/java/com/company/section4/Product.java
Normal file
13
bridge/src/main/java/com/company/section4/Product.java
Normal 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();
|
||||
}
|
||||
|
18
bridge/src/main/java/com/company/section4/ShanZhaiCorp.java
Normal file
18
bridge/src/main/java/com/company/section4/ShanZhaiCorp.java
Normal 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("我赚钱呀...");
|
||||
}
|
||||
}
|
||||
|
26
bridge/src/main/java/com/company/section5/Abstraction.java
Normal file
26
bridge/src/main/java/com/company/section5/Abstraction.java
Normal 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;
|
||||
}
|
||||
}
|
18
bridge/src/main/java/com/company/section5/Client.java
Normal file
18
bridge/src/main/java/com/company/section5/Client.java
Normal 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();
|
||||
}
|
||||
}
|
@ -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(){
|
||||
//业务逻辑处理
|
||||
}
|
||||
}
|
@ -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(){
|
||||
//业务逻辑处理
|
||||
}
|
||||
}
|
13
bridge/src/main/java/com/company/section5/Implementor.java
Normal file
13
bridge/src/main/java/com/company/section5/Implementor.java
Normal 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();
|
||||
}
|
@ -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
16
builder/pom.xml
Normal 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>
|
34
builder/src/main/java/com/company/section1/BMWModel.java
Normal file
34
builder/src/main/java/com/company/section1/BMWModel.java
Normal 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
Loading…
Reference in New Issue
Block a user