设计模式——简单工厂模式

  1. 云栖社区>
  2. 博客>
  3. 正文

设计模式——简单工厂模式

it徐胖子 2012-03-20 09:33:00 浏览401
展开阅读全文

 

package cn.vehicle

 

public interface Moveable
{
 public void run();
}

 

public class Car implements Moveable
{
 public void run()
 {
  System.out.println("开车喽");
 }
}

 

public class Plane implements Moveable
{
 public void run()
 {
  System.out.println("开飞机喽");
 }
}

 

package cn.factory

 

import cn.vehicle.Moveable;

public abstract class VehicleFactory
{
 public abstract Moveable createInstance();
}

 

import cn.vehicle.Car;

public class CarFactory extends VehicleFactory
{
 public Car createInstance()
 {
  return new Car();
 }
}

 

import cn.vehicle.Plane;

public class PlaneFactory extends VehicleFactory
{
 public Plane createInstance()
 {
  return new Plane();
 }
}

 

 

package cn.test

 

import cn.factory.VehicleFactory;
import cn.factory.PlaneFactory;
import cn.vehicle.Moveable;

public class Test
{
 public static void main(String[] args)
 {
  VehicleFactory factory = new PlaneFactory();
  Moveable m = factory.createInstance();
  m.run();
 }
}


 

 

 


 

网友评论

登录后评论
0/500
评论
it徐胖子
+ 关注