论坛首页 Java企业应用论坛

多例模式【创建模式第五篇】

浏览 2115 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-10-23   最后修改:2010-10-20
多例模式(Multiton Pattern)
1、多例模式特点:
1) 多例类可有多个实例
2) 多例类必须自己创建、管理自己的实例,并向外界提供自己的实例。
//多例类(骰子)
		import java.util.Random;
		import java.util.Date;
		
		public class Die{
			private static Die die1 = new Die();
			private static Die die2 = new Die();
			
			private Die(){
			}
			
			//工厂方法
			public static Die getInstance(int whichOne){
				if(whichOne == 1){
					return die1;
				}else{
					return die2;
				}
			}
			
			//掷骰子,返回一个在1~6之间的随机数
			public synchronized int dice(){
				Date d = new Date();
				Random r = new Random(d.getTime());
				int value = r.nextInt();
				value = Math.abs(value);
				value = value % 6;
				value += 1;
				return value;
			}
		}
		
		//客户端
		public class Client{
			private static Die die1, die2;
			public static void main(String args[]){
				die1 = Die.getInstance(1);
				die2 = Die.getInstance(2);
				die1.dice();
				die2.dice();
			}
		}
		
		//多例类是单例类的推广,而单例类是多例类的特殊情况
		多例类对象的状态如果是可以在加载后改变的,那么这种多例对象叫做可边多例对象;如果多例对象的状态在加载后不可以改变
		那么这种多例对象叫做不变多例对象
		
		//怎样使用Locale对象和ResourceBundle对象
		Locale locale = new Locale("fr", "FR");
		ResourceBundle res = ResourceBundle("shortname",locale);
		res会加载一个名为shortname_fr_FR.properties的Resource文件
		
		//多例类LingualResource
		import java.util.HashMap;
		import java.util.Locale;
		import java.util.ResourceBundle;
		
		public class LingualResource{
			private String language = "en";
			private region = "US";	//region(区域)
			private String localeCode = "en_US";
			private static HashMap instances = new HashMap(19);
			private Locale locale = null;
			private ResourceBundle resourceBundle = null;
			private LingualResource lnkLingualResource;
			
			private LingualResource(String language, String region){
				this.localeCode = language;
				this.region = region;
				localeCode = makeLocaleCode(language, region);
				locale = new Locale(language, region);
				resourceBundle = ResourceBundle.getBundle(FILE_NAME, locale);
				instances.put(makeLocaleCode(language, region), resourceBundle);
			}
			
			private LingualResource(){
			}
			
			//工厂方法,返回一个具体的内部状态的实例
			public synchronized static LingualResource getInstance(String language, String region){
				if(makeLocaleCode(language, region)){
					return (LingualResource)makeLocaleCode(language, region);
				}else{
					return new LingualResource(language, region);
				}
			}
			
			public String getLocaleString(String code){
				return resourceBundle.getString(code);
			}
			
			private static String makeLocaleCode(String language, String region){
				return language + "_" + region;
			}
		}
		
		//makeLocaleCode()是一个辅助性的方法,在传入语言代码和地区代码时,此方法可以返回一个Locale代码
		在getInstance()方法被调用时,程序会首先检查传入的Locale代码是否已经在instances集合中存在,如果已经存在
		即直接返回同它所对应的LingualResource对象,否则就会首先创建一个此Locale代码所对应的LingualResource对象,将之存入
		instances集合,并返回这个实例
		
		//客户端
		public class LingualResourceTester{
			public static void main(String agrs[]){
				LingualResource ling = LingualResource.getInstance("en", "US");
				String usDollar = ling.getLocaleString("USD");
				System.out.println("USD = " + usDollar);
				LingualResource lingZh = LingualResource.getInstance("zh", "CH");
				String usDollarZh = lingZh.getLocaleString("USD");
				System.out.println("USD = " + usDollarZh);
			}
		}
		//如果客户是美国客户,那么在jsp网页中可以通过调用getLocalString()方法得到相应的英文说明
		Resource文件res_en_US.properties的内容
		----------------------------------------
				LingualResource ling = LingualResource.getInstance("en", "US");
				String usDollar = ling.getLocaleString("USD");
		----------------------------------------
		返回----US Dollar
		
		中国用户:
		---------------------------------------
				LingualResource lingZh = LingualResource.getInstance("zh", "CH");
				String usDollarZh = lingZh.getLocaleString("USD");
		----------------------------------------
		返回----美元
		
		////////////////////////////////////////////
		为美国英文准备的Resource文件res_en_US.properties的内容如下:
			USD = US Dollar
			JPY = Japanese Yen
		为简体中文准备的Resource文件res_zh_CH.properties的内容如下:
			USD = 美元
			JPY = 日元
			
		=======================问答题=================================
		一个根据语言代码和地区代码将数字格式化的例子
		import java.util.Locale;
		import java.text.NumberFormat;
		
		public class NumberFormatTester{
			public static void displayNumber(Double amount, Locale currentLocale){
				NumberFormat formatter;
				String amountOut;
				formatter = NumberFormat.getNumberInstance(currentLocale);
				amountOut = formatter.format(amount);
				System.out.println(amountOut + "  " + currentLocale.toString());
			}
			
			public static void main(String agrs[]){
				displayNumber(new Double(1234567.89), new Locale("en", "US"));
				displayNumber(new Double(1234567.89), new Locale("de", "DE"));
				displayNumber(new Double(1234567.89), new Locale("fr", "FR"));
			}
		}
		
		一个根据语言代码和地区代码将货币数字格式化的例子
		import java.util.Locale;
		import java.text.NumberFormat;
		
		public class CurrencyFormatTester{
			public static void displayCurrency(Double amount, Locale currentLocale){
				NumberFormat formatter;
				String amountOut;
				formatter = NumberFormat.getCurrencyInstance(currentLocale);
				amountOut = formatter.format(amount);
				System.out.println(amountOut + "  " + currentLocale.toString());
			}
			
			public static void main(String agrs[]){
				displayCurrency(new Double(1234567.89), new Locale("en", "US"));
				displayCurrency(new Double(1234567.89), new Locale("de", "DE"));
				displayCurrency(new Double(1234567.89), new Locale("fr", "FR"));
			}
		} 
		
		一个根据语言代码和地区代码将百分比格式化的例子
		import java.util.Locale;
		import java.text.NumberFormat;
		
		public class PercentFormatTester{
			public static void displayPercent(Double amount, Locale currentLocale){
				NumberFormat formatter;
				String amountOut;
				formatter = NumberFormat.getPercentInstance(currentLocale);
				amountOut = formatter.format(amount);
				System.out.println(amountOut + "  " + currentLocale.toString());
			}
			
			public static void main(String agrs[]){
				displayPercent(new Double(4567.89), new Locale("en", "US"));
				displayPercent(new Double(4567.89), new Locale("de", "DE"));
				displayPercent(new Double(4567.89), new Locale("fr", "FR"));
			}
		}
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics