设计模式

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

@ -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();
}
}