文章目录
- 享元模式
- 1.引出享元模式
-
- 2.享元模式
- 1.基本介绍
- 2.原理类图
- 3.外部状态和内部状态
- 4.类图
- 5.代码实现
- 1.AbsWebSite.java 抽象的网站
- 2.ConcreteWebSite.java 具体的网站,type属性是内部状态
- 3.WebSiteFactory.java 网站工厂,根据类型,获取对应类型的网站
- 4.User.java 使用网站的用户,是外部状态
- 5.Client.java
- 6.结果
- 6.小结
- 3.享元模式在Integer的应用
- 代理模式
- 1.基本介绍
-
- 2.静态代理
- 1.基本介绍
- 2.类图
- 3.代码实现
- 1.目录结构
- 2.ITeacherDao.java 被代理的接口
- 3.TeacherDao.java 被代理的实现类
- 4.TeacherDaoProxy.java 静态代理类
- 5.Client.java
- 6.结果
- 2.动态代理(JDK代理)
- 1.基本介绍
- 2.类图
- 3.代码实现
- 1.ITeacherDao.java 被代理的接口
- 2.TeacherDao.java 被代理的类
- 3.ProxyFactory.java 代理工厂,返回动态代理对象
- 4.Client.java
- 5.结果
- 3.Cglib代理
- 1.基本介绍
- 2.注意事项
- 3.类图
- 4.引入四个jar包
- 5.代码实现
- 1.目录结构
- 2.TeacherDao.java 被代理的类
- 3.ProxyFactory.java 返回代理对象的工厂
- 4.Client.java
- 5.结果
- 6.几种变体
享元模式
1.引出享元模式
1.展示网站项目需求
2.传统方案解决
3.问题分析
2.享元模式
1.基本介绍
2.原理类图
3.外部状态和内部状态
4.类图
5.代码实现
1.AbsWebSite.java 抽象的网站
package com.sun;
public abstract class AbsWebSite {public abstract void use(User user);
}
2.ConcreteWebSite.java 具体的网站,type属性是内部状态
package com.sun;
public class ConcreteWebSite extends AbsWebSite {private String type = "";public ConcreteWebSite(String type) {this.type = type;}@Overridepublic void use(User user) {System.out.println("网站的发布类型为:" + type + user.getName() + "在使用中");}
}
3.WebSiteFactory.java 网站工厂,根据类型,获取对应类型的网站
package com.sun;import java.util.HashMap;
public class WebSiteFactory {private HashMap<String, ConcreteWebSite> pool = new HashMap<>();public AbsWebSite getWebsiteCategory(String type) {if (!pool.containsKey(type)) {pool.put(type, new ConcreteWebSite(type));}return (AbsWebSite) pool.get(type);}public int getWebSiteCount() {return pool.size();}
}
4.User.java 使用网站的用户,是外部状态
package com.sun;
public class User {private String name;public User(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}
5.Client.java
package com.sun;
public class Client {public static void main(String[] args) {WebSiteFactory webSiteFactory = new WebSiteFactory();AbsWebSite webSite = webSiteFactory.getWebsiteCategory("新闻");webSite.use(new User("tom"));AbsWebSite webSite2 = webSiteFactory.getWebsiteCategory("博客");webSite2.use(new User("jack"));AbsWebSite webSite3 = webSiteFactory.getWebsiteCategory("博客");webSite3.use(new User("lisa"));AbsWebSite webSite4 = webSiteFactory.getWebsiteCategory("博客");webSite4.use(new User("milan"));int webSiteCount = webSiteFactory.getWebSiteCount();System.out.println("webSiteCount = " + webSiteCount);}
}
6.结果
6.小结
3.享元模式在Integer的应用
代理模式
1.基本介绍
1.介绍
2.简单类图
2.静态代理
1.基本介绍
2.类图
3.代码实现
1.目录结构
2.ITeacherDao.java 被代理的接口
package com.sun.staticproxy;
public interface ITeacherDao {void teach();
}
3.TeacherDao.java 被代理的实现类
package com.sun.staticproxy;
public class TeacherDao implements ITeacherDao{@Overridepublic void teach() {System.out.println("老师正在授课中");}
}
4.TeacherDaoProxy.java 静态代理类
package com.sun.staticproxy;
public class TeacherDaoProxy implements ITeacherDao{private ITeacherDao teacherDao;public TeacherDaoProxy(ITeacherDao teacherDao) {this.teacherDao = teacherDao;}@Overridepublic void teach() {System.out.println("代理开始");teacherDao.teach();System.out.println("代理结束");}
}
5.Client.java
package com.sun.staticproxy;
public class Client {public static void main(String[] args) {TeacherDaoProxy teacherDaoProxy = new TeacherDaoProxy(new TeacherDao());teacherDaoProxy.teach();}
}
6.结果
2.动态代理(JDK代理)
1.基本介绍
2.类图
3.代码实现
1.ITeacherDao.java 被代理的接口
package com.sun.dynamic;
public interface ITeacherDao {void teach(String name);}
2.TeacherDao.java 被代理的类
package com.sun.dynamic;
public class TeacherDao implements ITeacherDao {@Overridepublic void teach(String name) {System.out.println(name + "老师正在授课中");}}
3.ProxyFactory.java 代理工厂,返回动态代理对象
package com.sun.dynamic;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
public class ProxyFactory {private Object target;public ProxyFactory(Object target) {this.target = target;}public Object getProxyInstance() {return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println("method = " + method + ", args = " + Arrays.toString(args));System.out.println("动态代理开始");Object result = method.invoke(target, args);System.out.println("动态代理结束");return result;}});}
}
4.Client.java
package com.sun.dynamic;
public class Client {public static void main(String[] args) {ITeacherDao target = new TeacherDao();ITeacherDao proxy = (ITeacherDao) new ProxyFactory(target).getProxyInstance();proxy.teach("李华");}
}
5.结果
3.Cglib代理
1.基本介绍
2.注意事项
3.类图
4.引入四个jar包
5.代码实现
1.目录结构
2.TeacherDao.java 被代理的类
package com.sun.cglib;
public class TeacherDao {public void teach(String name) {System.out.println(name + "老师授课中,使用的是cglib代理,不需要实现接口");}
}
3.ProxyFactory.java 返回代理对象的工厂
package com.sun.cglib;import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;import java.lang.reflect.Method;
public class ProxyFactory implements MethodInterceptor {private Object target;public ProxyFactory(Object target) {this.target = target;}public Object getProxyInstance() {Enhancer enhancer = new Enhancer();enhancer.setSuperclass(target.getClass());enhancer.setCallback(this);return enhancer.create();}@Overridepublic Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {System.out.println("cglib代理模式开始");System.out.println("method = " + method);System.out.println("args = " + args);Object returnVal = method.invoke(target, args);System.out.println("cglib代理结束");return returnVal;}
}
4.Client.java
package com.sun.cglib;
public class Client {public static void main(String[] args) {TeacherDao teacherDao = new TeacherDao();ProxyFactory proxyFactory = new ProxyFactory(teacherDao);TeacherDao proxyInstance = (TeacherDao) proxyFactory.getProxyInstance();proxyInstance.teach("小明");}
}
5.结果
6.几种变体