public class Main {
public static void main(String args[])
{
int extrinsicstate=22;
FlyweightFactory f=new FlyweightFactory();
Flyweight fx=f.GetFlyweight(0);
fx.Operation(--extrinsicstate);
Flyweight fy=f.GetFlyweight(1);
fx.Operation(--extrinsicstate);
Flyweight fz=f.GetFlyweight(2);
fx.Operation(--extrinsicstate);
UnshareConcreteFlyweight u=new UnshareConcreteFlyweight();
u.Operation(--extrinsicstate);
}
}
abstract class Flyweight{
public abstract void Operation(int extrinsicstate);
}
class ConcreteFlyweight extends Flyweight{
public void Operation(int extrinsicstate) {
// TODO Auto-generated method stub
System.out.println("具体Flyweight:"+extrinsicstate);
}
}
class UnshareConcreteFlyweight extends Flyweight{
public void Operation(int extrinsicstate) {
// TODO Auto-generated method stub
System.out.println("不共享的具体Flyweight:"+extrinsicstate);
}
}
class FlyweightFactory{
private List<ConcreteFlyweight> l1=new ArrayList<ConcreteFlyweight>();
ConcreteFlyweight X=new ConcreteFlyweight();
ConcreteFlyweight Y=new ConcreteFlyweight();
ConcreteFlyweight Z=new ConcreteFlyweight();
public FlyweightFactory() {
l1.add(X);
l1.add(Y);
l1.add(Z);
}
public Flyweight GetFlyweight(int key) {
return l1.get(key);
}
}
享元模式
最新推荐文章于 2024-08-26 09:32:51 发布