天天看點

設計模式——簡單工廠模式

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

  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;

public class test

 public static void main(string[] args)

  vehiclefactory factory = new planefactory();

  moveable m = factory.createinstance();

  m.run();

設計模式——簡單工廠模式

繼續閱讀