工厂设计模式

工厂代码:

package org.lxh.demo ;
import java.io.* ;
import java.util.* ;
interface Fruit
{
 // 生长
 public void grow() ;
 // 采摘
 public void pick() ;
}
class Apple implements Fruit
{
 public void grow()
 {
  System.out.println("苹果在生长。。。 ") ;
 }
 public void pick()
 {
  System.out.println("摘苹果。。。") ;
 }
};
class Orange implements Fruit
{
 public void grow()
 {
  System.out.println("橘子在生长。。。 ") ;
 }
 public void pick()
 {
  System.out.println("摘橘子。。。") ;
 }
};
class Cherry implements Fruit
{
 public void grow()
 {
  System.out.println("樱桃在生长。。。 ") ;
 }
 public void pick()
 {
  System.out.println("摘樱桃。。。") ;
 }
};

class WaterMelon implements Fruit
{
 public void grow()
 {
  System.out.println("西瓜在生长。。。") ;
 }
 public void pick()
 {
  System.out.println("摘西瓜。。。") ;
 }
};

class Banana implements Fruit
{
 public void grow()
 {
  System.out.println("香蕉在生长。。。 ") ;
 }
 public void pick()
 {
  System.out.println("摘香蕉。。。") ;
 }
};

class DD implements Fruit
{
 public void grow()
 {
  System.out.println("DD。。。 ") ;
 }
 public void pick()
 {
  System.out.println("摘DD。。。") ;
 }
};

class Factory
{
 public static Fruit getFruitInstance(String type)
 {
  Fruit f = null ;
  // 通过Class类完成
  try
  {
   f = (Fruit)Class.forName(type).newInstance() ;
  }
  catch (Exception e)
  {
   System.out.println(e) ;
  }
  return f ;
 }
};

// 定义一个新类,此类可以从键盘输入数据
class InputData
{
 BufferedReader buf = null ;
 public InputData()
 {
  buf = new BufferedReader(new InputStreamReader(System.in)) ;
 }
 public String getString()
 {
  String str = null ;
  try
  {
   str = buf.readLine() ;
  }
  catch (Exception e)
  {
  }
  return str ;
 }
};

class Init
{
 public static Properties getPropertiesInit()
 {
  Properties p = new Properties() ;
  try
  {
   p.loadFromXML(new FileInputStream("lxh.xml")) ;
  }
  catch (Exception e)
  {
  }
  return p ;
 }
};

public class FactoryDemo06
{
 public static void main(String args[])
 {
  Properties p = new Properties() ;
  p = Init.getPropertiesInit() ;
  System.out.println(p) ;
  
  // 加入一个可以从键盘输入数据的类
  System.out.print("输入要使用的子类代码:") ;
  String code = new InputData().getString() ;
  System.out.println(p.getProperty(code)) ;
  Fruit f = Factory.getFruitInstance(p.getProperty(code)) ;
  if(f!=null)
  {
   f.grow() ;
   f.pick() ;
  }
  else
  {
   System.out.println("没有发现这个类型。") ;
  }
 }
};


//XML文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>MLDN --&gt; LXH</comment>
<entry key="b">org.lxh.demo.Banana</entry>
<entry key="a">org.lxh.demo.Apple</entry>
<entry key="o">org.lxh.demo.Orange</entry>
<entry key="c">org.lxh.demo.Cherry</entry>
<entry key="d">org.lxh.demo.DD</entry>
</properties>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值