Skip to content

Commit

Permalink
feat:上传已经完成的设计模式的例子
Browse files Browse the repository at this point in the history
subject:上传目前已经完成的6中设计模式,后面会陆续增加新的,具体计划与安排会陆续在readme中更新。
  • Loading branch information
maxy26 committed Feb 27, 2019
1 parent 41c2155 commit b6bc16e
Show file tree
Hide file tree
Showing 100 changed files with 1,831 additions and 169 deletions.
1 change: 1 addition & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,024 changes: 884 additions & 140 deletions .idea/workspace.xml

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# DesignPatterns

1.观察者模式 - 已上传

2.装饰模式- 已上传

3.策略模式-已上传

4.适配器模式- 已上传

5.代理模式- 已上传

6.模板模式- 已上传

7.工厂模式(抽象)- 整理中

8.桥接模式- 整理中

9.门面模式- 整理中

10.组合模式- 整理中

11.状态模式- 整理中

12.命令模式- 整理中

13.解释器模式- 整理中


8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@
<version>1.1.0.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.10</version>
</dependency>


</dependencies>


Expand Down
11 changes: 11 additions & 0 deletions src/main/com/mxy/design/adapter/classes/adapter/Ps2Connector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mxy.design.adapter.classes.adapter;

/**
* ps2插口
*/
public interface Ps2Connector {


void ps2Description();

}
17 changes: 17 additions & 0 deletions src/main/com/mxy/design/adapter/classes/adapter/Ps2UsbAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mxy.design.adapter.classes.adapter;

/**
* 1.类适配器
* PS2 to Usb 转换口
*/
public class Ps2UsbAdapter extends UsbConnectorImpl implements Ps2Connector {

/**
* 重点:继承你需要适配的类
*/
@Override
public void ps2Description() {
System.out.println("正在进行 PS/2适配USB中....");
usbDescription();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mxy.design.adapter.classes.adapter;


public class PsConnectorImpl implements Ps2Connector {

@Override
public void ps2Description() {

System.out.println("使用PS/2插口");
}
}
11 changes: 11 additions & 0 deletions src/main/com/mxy/design/adapter/classes/adapter/UsbConnector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mxy.design.adapter.classes.adapter;

/**
* USB插口
*/
public interface UsbConnector {


void usbDescription();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mxy.design.adapter.classes.adapter;

/**
* 使用USB插槽
*/
public class UsbConnectorImpl implements UsbConnector {

@Override
public void usbDescription() {
System.out.println("使用USB插口");
}
}
23 changes: 23 additions & 0 deletions src/main/com/mxy/design/adapter/interfaces/adapter/Adapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mxy.design.adapter.interfaces.adapter;

/**
* 接口适配器:重在共享接口功能,并为继承该适配器的类提供各自所需方法
*/
public abstract class Adapter implements Port {

@Override
public void SSH() {

}

@Override
public void NET() {

}

@Override
public void FTP() {

}

}
17 changes: 17 additions & 0 deletions src/main/com/mxy/design/adapter/interfaces/adapter/Chat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mxy.design.adapter.interfaces.adapter;
/**
* 该类只需要 NET FTP 就够了 所以继承父类并重写
*/
public class Chat extends Adapter {


@Override
public void NET() {
System.out.println("Chat 开启80端口");
}

@Override
public void FTP() {
System.out.println("Chat 开启21端口");
}
}
22 changes: 22 additions & 0 deletions src/main/com/mxy/design/adapter/interfaces/adapter/Linux.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.mxy.design.adapter.interfaces.adapter;

/**
* 该系统需要全部方法
*/
public class Linux implements Port {

@Override
public void SSH() {
System.out.println("Linux 开启22端口");
}

@Override
public void NET() {
System.out.println("Linux 开启80端口");
}

@Override
public void FTP() {
System.out.println("Linux 开启21端口");
}
}
17 changes: 17 additions & 0 deletions src/main/com/mxy/design/adapter/interfaces/adapter/Port.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mxy.design.adapter.interfaces.adapter;

/**
* 各种协议端口
*/
public interface Port {

// 远程SSH端口22
public void SSH();

// 网络端口80
public void NET();

// 文件传输FTP端口21
public void FTP();
}

13 changes: 13 additions & 0 deletions src/main/com/mxy/design/adapter/interfaces/adapter/Tomcat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mxy.design.adapter.interfaces.adapter;

/**
* 该类只需要 NET 就够了 所以继承父类并重写
*/
public class Tomcat extends Adapter {


@Override
public void NET() {
System.out.println("Tomcat 开启80端口");
}
}
11 changes: 11 additions & 0 deletions src/main/com/mxy/design/adapter/object/adapater/Ps2Connector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mxy.design.adapter.object.adapater;

/**
* ps2插口
*/
public interface Ps2Connector {


void ps2Description();

}
25 changes: 25 additions & 0 deletions src/main/com/mxy/design/adapter/object/adapater/Ps2UsbAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mxy.design.adapter.object.adapater;

/**
* 1.对象适配器
* PS2 to Usb 转换口
*/
public class Ps2UsbAdapter implements Ps2Connector {


/**
* 重点:在适配器里面持有需要适配的对象
*/
private UsbConnector usbConnector;


public Ps2UsbAdapter(UsbConnector usbConnector) {
this.usbConnector = usbConnector;
}

@Override
public void ps2Description() {
System.out.println("正在进行 PS/2适配USB中....");
usbConnector.usbDescription();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mxy.design.adapter.object.adapater;


public class PsConnectorImpl implements Ps2Connector {

@Override
public void ps2Description() {

System.out.println("使用PS/2插口");
}
}
11 changes: 11 additions & 0 deletions src/main/com/mxy/design/adapter/object/adapater/UsbConnector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.mxy.design.adapter.object.adapater;

/**
* USB插口
*/
public interface UsbConnector {


void usbDescription();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mxy.design.adapter.object.adapater;

/**
* 使用USB插槽
*/
public class UsbConnectorImpl implements UsbConnector {

@Override
public void usbDescription() {
System.out.println("使用USB插口");
}
}
15 changes: 15 additions & 0 deletions src/main/com/mxy/design/decorator/Cake.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mxy.design.decorator;

public interface Cake {

/**
* 描述
*/
String description();

/**
* 花费金额
*/
double cost();

}
12 changes: 12 additions & 0 deletions src/main/com/mxy/design/decorator/CakeDecorator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mxy.design.decorator;

public abstract class CakeDecorator implements Cake {


protected Cake cake;

public CakeDecorator(Cake cake) {
this.cake = cake;
}

}
15 changes: 15 additions & 0 deletions src/main/com/mxy/design/decorator/CakeImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mxy.design.decorator;

public class CakeImpl implements Cake {


@Override
public String description() {
return "蛋糕";
}

@Override
public double cost() {
return 10;
}
}
29 changes: 29 additions & 0 deletions src/main/com/mxy/design/decorator/Candle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.mxy.design.decorator;


public class Candle extends CakeDecorator {

double candleCost = 30;

public Candle(Cake cake) {
super(cake);
}

@Override
public String description() {
return "蜡烛";
}

@Override
public double cost() {
return (candleCost) + (cake.cost());
}

/**
* 涨价
* @param price
*/
public void risePrice(double price) {
this.candleCost += price;
}
}
25 changes: 25 additions & 0 deletions src/main/com/mxy/design/decorator/Fruits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mxy.design.decorator;

public class Fruits extends CakeDecorator {

double fruitsCost = 20;

/**
* 构造为了 调用父类方法
*
* @param cake
*/
public Fruits(Cake cake) {
super(cake);
}

@Override
public String description() {
return "水果";
}

@Override
public double cost() {
return (fruitsCost) + (cake.cost());
}
}
Loading

0 comments on commit b6bc16e

Please sign in to comment.