《大话设计模式》阅读笔记和总结。原书是C#编写的,本人用Java实现了一遍,包括每种设计模式的UML图实现和示例代码实现。
目录:设计模式
Github地址:DesignPattern
说明
定义:享元模式(Flyweight),运用共享技术有效地支持大量细粒度的对象。
UML图:
代码实现:
Flyweight类,它是所有具体享元类的超类或接口,通过这个接口,Flyweight可以接受并作用于外部状态
abstract class Flyweight{
public abstract void Operation(int extrinsicstate);
}
ConcreteFlyweight是继承Flyweight超类或实现Flyweight接口,并为内部状态增加存储空间
class ConcreteFlyweight extends Flyweight{
@Override
public void Operation(int extrinsicstate) {
System.out.println("具体Flyweight:"+extrinsicstate);
}
}
UnsharedConcreteFlyweight是指那些不需要共享的Flyweight子类,因为Flyweight接口共享成为可能,,但它并不强制共享
class UnsharedConcreteFlyweight extends Flyweight{
@Override
public void Operation(int extrinsicstate) {
System.out.println("不共享具体Flyweight:"+extrinsicstate);
}
}
FlyweightFactory,是一个享元工厂,用来创建并管理Flyweight对象。它主要是用来确保合理的共享Flyweight,当用户请求一个Flyweight时,FlyweightFactory对象提供一个已创建的实例或者创建一个(如果不存在的话)。
class FlyweightFactory{
private Map<String,Object> flyweights = new HashMap<>();
public FlyweightFactory(){
flyweights.put("X",new ConcreteFlyweight());
flyweights.put("Y",new ConcreteFlyweight());
flyweights.put("Z",new ConcreteFlyweight());
}
public Flyweight getFlyweight(String key){
return (Flyweight) flyweights.get(key);
}
}
客户端代码
public class FlyweightPattern {
public static void main(String[] args){
int extrinsicstate = 22;
FlyweightFactory f = new FlyweightFactory();
Flyweight fx = f.getFlyweight("X");
fx.Operation(--extrinsicstate);
Flyweight fy = f.getFlyweight("Y");
fy.Operation(--extrinsicstate);
Flyweight fz = f.getFlyweight("Z");
fz.Operation(--extrinsicstate);
UnsharedConcreteFlyweight uf = new UnsharedConcreteFlyweight();
uf.Operation(--extrinsicstate);
}
}
运行结果
具体Flyweight:21
具体Flyweight:20
具体Flyweight:19
不共享具体Flyweight:18
示例
例子:网站分为很多种,有博客类的,有产品展示的。其实每种类型的网站在代码上来说有很大的相似之处,所以公司在做网站的时候往往相同类型的网站都使用一份源代码,这样维护起来十分简单和便捷。用程序模拟这个过程。
UML图:
代码实现:
用户类,用于网站的客户账号,是“网站的外部状态”
public class User {
private String name;
public User(String name){
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
网站抽象类
public abstract class WebSite {
// “使用”方法需要传递“用户”对象
public abstract void Use(User user);
}
具体网站类
public class ConcreteWebSite extends WebSite {
private String name;
public ConcreteWebSite(String name){
this.name = name;
}
@Override
public void Use(User user) {
System.out.println("网站分类:"+name+"----用户:"+user.getName());
}
}
网站工厂类
public class WebSiteFactory {
private Map<String,Object> flyweights = new HashMap<>();
// 获得网站分类
public WebSite getWebSiteCategory(String key){
if (!flyweights.containsKey(key)){
flyweights.put(key,new ConcreteWebSite(key));
}
return (WebSite) flyweights.get(key);
}
public int getWebSiteCount(){
return flyweights.size();
}
}
客户端代码
public class Main {
public static void main(String[] args){
WebSiteFactory f = new WebSiteFactory();
WebSite fx = f.getWebSiteCategory("产品展示");
fx.Use(new User("小菜"));
WebSite fy = f.getWebSiteCategory("产品展示");
fy.Use(new User("大鸟"));
WebSite fz = f.getWebSiteCategory("产品展示");
fz.Use(new User("娇娇"));
WebSite fl = f.getWebSiteCategory("博客");
fl.Use(new User("老顽童"));
WebSite fm = f.getWebSiteCategory("博客");
fm.Use(new User("桃谷六仙"));
WebSite fn = f.getWebSiteCategory("博客");
fn.Use(new User("南海鳄神"));
System.out.println("得到网站分类总数为"+f.getWebSiteCount());
}
}
运行结果
网站分类:产品展示----用户:小菜
网站分类:产品展示----用户:大鸟
网站分类:产品展示----用户:娇娇
网站分类:博客----用户:老顽童
网站分类:博客----用户:桃谷六仙
网站分类:博客----用户:南海鳄神
得到网站分类总数为2