当前位置:首页 > 设计模式 > 正文内容

享元模式

canca19年前 (2007-03-26)设计模式529

//享元模式
//CopyRight(C) CAnca software Office.2006
//Created by CAnca.

import java.util.*;

public class ShareMode{
 public static void main(String[] args){
  int number = 23;
  FlyweightFactory flyweight = new FlyweightFactory();
  Component com1 = flyweight.getShare("X");
  com1.operation(--number);
  Component com2 = flyweight.getShare("Y");
  com2.operation(--number);
  Component com3 = flyweight.getShare("W");
  com3.operation(--number);
  Component com4 = flyweight.getShare("U");
  com4.operation(--number);
  
  UnShare us = new UnShare();
  us.operation(--number);
 }
}

class FlyweightFactory{
 private Hashtable flyweights = new Hashtable();
 public FlyweightFactory(){
  flyweights.put("X",new ShareA()); 
  flyweights.put("Y",new ShareB());
  flyweights.put("W",new ShareC());
  flyweights.put("U",new ShareD());
 }
 public Component getShare(String key){
  if(flyweights.containsKey(key)){
   return ((Component)flyweights.get(key));
  }
  return null;
 }
}

abstract class Component{
 abstract void operation(int key);
}

class ShareA extends Component{
 public void operation(int key){
  System.out.println("ShareA.operation()--" + key);
 }
}

class ShareB extends Component{
 public void operation(int key){
  System.out.println("ShareB.operation()--" + key);
 }
}
class ShareC extends Component{
 public void operation(int key){
  System.out.println("ShareC.operation()--" + key);
 }
}
class ShareD extends Component{
 public void operation(int key){
  System.out.println("ShareD.operation()--" + key);
 }
}

class UnShare extends Component{
 public void operation(int key){
  System.out.println("UnShare.operation()--" + key);
 }
}

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/660.html

标签: 设计模式
分享给朋友:
返回列表

上一篇:外观模式

下一篇:装饰模式

“享元模式” 的相关文章

单例模式

//CopyRight CAnca Software Office. 2006//Created by CAnca. import java.util.*; public class singletonMode{ public static void main(String[] args)...

代理模式

//代理模式--真实例子//CopyRight(C)CAnca Software Office.2006//Created by CAnca.   public class ProxyMode_RealWorld{     public static void...

桥接模式

//桥接模式//CopyRight(C) CAnca Software Office 2006.//Created by CAnca. import java.util.*; public class BridgeMode{ public static void main(String[]...

外观模式

//外观模式//CopyRight(C)CAnca Software Office. 2006//Created by CAnca. public class FacadeMode{ public static void main(String[] args){  Fa...

组合模式

//组合模式//CopyRight(C) CAnca software Office.2006//Created by CAnca. import java.util.*; public class ComponantMode{ public static void main(String...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。