工厂

import java.util.Random;


abstract class UiData
{
 public void DoLogic()
 {
  Random pRandom = new Random();
  m_iData = pRandom.nextInt(100);
  pRandom = null;
 }
 public abstract void DoView();
 protected int m_iData;
}

class UiView_480_272 extends UiData
{
 @Override
 public void DoView()
 {
  System.out.println("View 480 * 272: "+m_iData);
 }
}

class UiView_320_240 extends UiData
{
 @Override
 public void DoView()
 {
  System.out.println("View 320 * 240: "+m_iData);
 }
}

class UiView_NUM extends UiData
{
 @Override
 public void DoView()
 {
  System.out.println("Dos: "+m_iData);
 }
}

abstract class UiFactory
{
 public abstract <T extends UiData> T GetUi(Class<T> c);
}

class CE600_UiFactory extends UiFactory
{
 @SuppressWarnings({ "finally", "unchecked" })
 @Override
 public <T extends UiData> T GetUi(Class<T> c)
 {
  // TODO Auto-generated method stub
  UiData pUiData = null;
  try
  {
   pUiData = (UiData)Class.forName(c.getName()).newInstance();
  }
  catch( Exception e)
  {
   throw e;
  }
        finally
        {
            return (T)pUiData;
        }
 }
}

class DOS_UiFactory extends UiFactory
{
 @SuppressWarnings({ "finally", "unchecked" })
 @Override
 public <T extends UiData> T GetUi(Class<T> c)
 {
  // TODO Auto-generated method stub
  UiData pUiData = null;
  try
  {
   pUiData = new UiView_NUM();
  }
  catch( Exception e)
  {
   throw e;
  }
        finally
        {
            return (T)pUiData;
        }
 }
 
}

//---------------------------------------------------

class FactoryClient
{
 public static void Client()
 {
  UiFactory pUiFactoryView = new CE600_UiFactory();
  UiData pUiData1 = pUiFactoryView.GetUi(UiView_480_272.class);
  pUiData1.DoLogic();
  pUiData1.DoView();
  pUiData1 = null;
  UiData pUiData2 = pUiFactoryView.GetUi(UiView_320_240.class);
  pUiData2.DoLogic();
  pUiData2.DoView();
  pUiData2 = null;
  pUiFactoryView = null;
  
  UiFactory pUiFactoryDos = new DOS_UiFactory();
  UiData pUiDataDos = pUiFactoryDos.GetUi(UiView_320_240.class);
  pUiDataDos.DoLogic();
  pUiDataDos.DoView();
  pUiDataDos = null;
  pUiFactoryView = null;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值