设计模式

This commit is contained in:
luoxiang 2019-12-08 08:43:00 +08:00
parent a5a3821857
commit 0aeefd7e05
33 changed files with 470 additions and 2 deletions

View File

@ -27,6 +27,11 @@
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,14 @@
package com.heibaiying.behavioral.state;
public class CloseState extends State {
@Override
public void pause() {
System.out.println("操作失败:视频已处于关闭状态,无需暂停");
}
@Override
public void speed() {
System.out.println("操作失败:视频已处于关闭状态,无法加速");
}
}

View File

@ -0,0 +1,9 @@
package com.heibaiying.behavioral.state;
public class PauseState extends State {
@Override
public void speed() {
System.out.print("操作失败:暂停状态下不支持加速");
}
}

View File

@ -0,0 +1,5 @@
package com.heibaiying.behavioral.state;
public class PlayState extends State {
}

View File

@ -0,0 +1,47 @@
package com.heibaiying.behavioral.state;
public class Player {
private State state;
public final static PlayState PLAY_STATE = new PlayState();
public final static PauseState PAUSE_STATE = new PauseState();
public final static CloseState CLOSE_STATE = new CloseState();
public final static SpeedState SPEED_STATE = new SpeedState();
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
this.state.setPlayer(this);
}
Player() {
// 假设播放器初始状态为关闭
this.state = new CloseState();
this.state.setPlayer(this);
}
public void paly() {
System.out.println("播放视频");
state.paly();
}
public void pause() {
System.out.println("暂停视频");
state.pause();
}
public void close() {
System.out.println("关闭视频");
state.close();
}
public void speed() {
System.out.println("视频加速");
state.speed();
}
}

View File

@ -0,0 +1,9 @@
package com.heibaiying.behavioral.state;
public class SpeedState extends State {
@Override
public void paly() {
System.out.println("你当前已处于加速播放状态");
}
}

View File

@ -0,0 +1,26 @@
package com.heibaiying.behavioral.state;
public class State {
private Player player;
public void setPlayer(Player player) {
this.player = player;
}
public void paly() {
player.setState(Player.PLAY_STATE);
}
public void pause() {
player.setState(Player.PAUSE_STATE);
}
public void close() {
player.setState(Player.CLOSE_STATE);
}
public void speed() {
player.setState(Player.SPEED_STATE);
}
}

View File

@ -0,0 +1,13 @@
package com.heibaiying.behavioral.state;
public class ZTest {
public static void main(String[] args) {
Player player = new Player();
player.speed();
player.paly();
player.speed();
player.pause();
player.close();
player.speed();
}
}

View File

@ -0,0 +1,8 @@
package com.heibaiying.behavioral.visitor;
/**
* 档案
*/
public interface Archive {
void accept(Visitor visitor);
}

View File

@ -0,0 +1,24 @@
package com.heibaiying.behavioral.visitor;
import java.util.ArrayList;
import java.util.List;
public class Company {
private List<Archive> archives = new ArrayList<>();
void add(Archive archive) {
archives.add(archive);
}
void remove(Archive archive) {
archives.remove(archive);
}
void accept(Visitor visitor) {
for (Archive archive : archives) {
archive.accept(visitor);
}
}
}

View File

@ -0,0 +1,14 @@
package com.heibaiying.behavioral.visitor;
public class DepartManager implements Visitor {
@Override
public void visit(PublicArchive publicArchive) {
System.out.println("所有公开档案");
}
@Override
public void visit(SecretArchive secretArchive) {
System.out.println("三级以下权限的加密档案");
}
}

View File

@ -0,0 +1,14 @@
package com.heibaiying.behavioral.visitor;
public class President implements Visitor {
@Override
public void visit(PublicArchive publicArchive) {
System.out.println("所有公开档案");
}
@Override
public void visit(SecretArchive secretArchive) {
System.out.println("所有加密档案");
}
}

View File

@ -0,0 +1,11 @@
package com.heibaiying.behavioral.visitor;
/**
* 公开档案
*/
public class PublicArchive implements Archive {
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

View File

@ -0,0 +1,11 @@
package com.heibaiying.behavioral.visitor;
/**
* 加密档案
*/
public class SecretArchive implements Archive {
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}

View File

@ -0,0 +1,6 @@
package com.heibaiying.behavioral.visitor;
public interface Visitor {
void visit(PublicArchive publicArchive);
void visit(SecretArchive secretArchive);
}

View File

@ -0,0 +1,11 @@
package com.heibaiying.behavioral.visitor;
public class ZTest {
public static void main(String[] args) {
Company company = new Company();
company.add(new SecretArchive());
company.add(new PublicArchive());
company.accept(new DepartManager());
company.accept(new President());
}
}

View File

@ -0,0 +1,13 @@
package com.heibaiying.structural.flyweight;
public class ArtPPT extends PowerPoint {
public ArtPPT(String copyright) {
super(copyright);
}
@Override
void create() {
System.out.println("艺术类PPT模板");
}
}

View File

@ -0,0 +1,13 @@
package com.heibaiying.structural.flyweight;
public class BusinessPPT extends PowerPoint {
public BusinessPPT(String copyright) {
super(copyright);
}
@Override
void create() {
System.out.println("商务类PPT模板");
}
}

View File

@ -0,0 +1,26 @@
package com.heibaiying.structural.flyweight;
import java.lang.reflect.Constructor;
import java.util.HashMap;
public class PPTFactory {
private HashMap<String, PowerPoint> hashMap = new HashMap<>();
public PowerPoint getPPT(Class<? extends PowerPoint> clazz) {
try {
String name = clazz.getName();
if (hashMap.keySet().contains(name)) {
return hashMap.get(name);
}
Constructor<?> constructor = Class.forName(name).getConstructor(String.class);
PowerPoint powerPoint = (PowerPoint) constructor.newInstance("PPT工厂版本所有");
hashMap.put(name, powerPoint);
return powerPoint;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -0,0 +1,27 @@
package com.heibaiying.structural.flyweight;
import lombok.ToString;
public abstract class PowerPoint {
/*版权*/
private String copyright;
private String title;
public PowerPoint(String copyright) {
this.copyright = copyright;
}
public void setTitle(String title) {
this.title = title;
}
abstract void create();
@Override
public String toString() {
return "编号:" + hashCode() + ": PowerPoint{" +
"copyright='" + copyright + '\'' +
", title='" + title + '\'' +
'}';
}
}

View File

@ -0,0 +1,13 @@
package com.heibaiying.structural.flyweight;
public class SciencePPT extends PowerPoint {
public SciencePPT(String copyright) {
super(copyright);
}
@Override
void create() {
System.out.println("科技类PPT模板");
}
}

View File

@ -0,0 +1,16 @@
package com.heibaiying.structural.flyweight;
public class ZTest {
public static void main(String[] args) {
PPTFactory pptFactory = new PPTFactory();
PowerPoint ppt01 = pptFactory.getPPT(BusinessPPT.class);
ppt01.setTitle("第一季度工作汇报");
System.out.println(ppt01);
PowerPoint ppt02 = pptFactory.getPPT(BusinessPPT.class);
ppt02.setTitle("第二季度工作汇报");
System.out.println(ppt02);
PowerPoint ppt03 = pptFactory.getPPT(SciencePPT.class);
ppt03.setTitle("科技展汇报");
System.out.println(ppt03);
}
}

View File

@ -0,0 +1,7 @@
package com.heibaiying.structural.proxy.cglib;
public class ComputeService {
public void compute() {
System.out.println("业务处理");
}
}

View File

@ -0,0 +1,36 @@
package com.heibaiying.structural.proxy.cglib;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Proxy implements MethodInterceptor {
private Object target;
public Proxy(Object target) {
this.target = target;
}
public Object getProxyInstance() {
// 创建用于生成生成动态子类的工具类
Enhancer enhancer = new Enhancer();
// 指定动态生成类的父类
enhancer.setSuperclass(target.getClass());
// 设置回调
enhancer.setCallback(this);
// 动态生成子类
return enhancer.create();
}
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws InvocationTargetException, IllegalAccessException {
System.out.println("权限校验");
Object invoke = method.invoke(target, args);
System.out.println("资源回收");
return invoke;
}
}

View File

@ -0,0 +1,9 @@
package com.heibaiying.structural.proxy.cglib;
public class ZTest {
public static void main(String[] args) {
Proxy proxy = new Proxy(new ComputeService());
ComputeService service = (ComputeService) proxy.getProxyInstance();
service.compute();
}
}

View File

@ -0,0 +1,8 @@
package com.heibaiying.structural.proxy.dynamic_proxy;
public class ComputeService implements IService {
@Override
public void compute() {
System.out.println("业务处理");
}
}

View File

@ -0,0 +1,5 @@
package com.heibaiying.structural.proxy.dynamic_proxy;
public interface IService {
void compute();
}

View File

@ -0,0 +1,19 @@
package com.heibaiying.structural.proxy.dynamic_proxy;
import java.lang.reflect.Proxy;
public class ZTest {
public static void main(String[] args) {
ComputeService target = new ComputeService();
IService proxyInstance = (IService) Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
(proxy, method, args1) -> {
System.out.println("权限校验");
Object invoke = method.invoke(target, args1);
System.out.println("资源回收");
return invoke;
});
proxyInstance.compute();
}
}

View File

@ -0,0 +1,8 @@
package com.heibaiying.structural.proxy.static_proxy;
public class ComputeService implements IService {
@Override
public void compute() {
System.out.println("业务处理");
}
}

View File

@ -0,0 +1,5 @@
package com.heibaiying.structural.proxy.static_proxy;
public interface IService {
void compute();
}

View File

@ -0,0 +1,17 @@
package com.heibaiying.structural.proxy.static_proxy;
public class ProxyService implements IService {
private IService target;
public ProxyService(IService target) {
this.target = target;
}
@Override
public void compute() {
System.out.println("权限校验");
target.compute();
System.out.println("资源回收");
}
}

View File

@ -0,0 +1,8 @@
package com.heibaiying.structural.proxy.static_proxy;
public class ZTest {
public static void main(String[] args) {
ProxyService proxyService = new ProxyService(new ComputeService());
proxyService.compute();
}
}

View File

@ -450,7 +450,7 @@ public class ZTest {
### 2.3 优缺点
简单工厂的优点在于其向用户屏蔽了对象创建过程,使得用户可以不必关注具体的创建细节,其缺陷在于违背了开闭原则。在简单工厂模式,如果想要增加新的产品,就需要修改简单工厂中的判断逻辑,这就违背了开闭原则,因此其并不属于 GOF 经典的 23 种设计模式。在 Java 语言中,可以通过泛型来尽量规避这一缺陷,此时需要将创建产品的方法修改为如下所示:
简单工厂的优点在于其向用户屏蔽了对象创建过程,使得用户可以不必关注具体的创建细节,其缺陷在于违背了开闭原则。在简单工厂模式,如果想要增加新的产品,就需要修改简单工厂中的判断逻辑,这就违背了开闭原则,因此其并不属于 GOF 经典的 23 种设计模式。在 Java 语言中,可以通过泛型来尽量规避这一缺陷,此时可以将创建产品的方法修改为如下所示:
```java
public Phone getPhone(Class<? extends Phone> phoneClass) {
@ -757,3 +757,14 @@ Phone(processor=高通骁龙处理器, camera=索尼摄像头, screen=OLED)
### 5.3 优点
建造者模式的优点在于将复杂的构建过程拆分为多个独立的单元,在保证拓展性的基础上也保证了良好的封装性,使得客户端不必知道产品的具体创建流程。
## 6. 原型模式
### 6.1 定义
### 6.2 示例
### 6.3