JAVA设计模式之工厂系列(factory)

1、静态工厂

 单例模式:一种特殊的静态工厂,请参考单例模式

2、简单工厂

复制代码
/**

  • 可移动的物体抽象接口
    /
    public interface Moveble { }
    /
    *
  • 食品抽象类
    /
    public abstract class Food{
    }
    /
    *
  • 面包类
    /
    public class bread extends Food {
    public Bike(){
    System.out.println(“T am bread”);
    }
    }
    复制代码
    复制代码
    /
    *
  • bike类
    /
    public class Bike implements Moveble{
    public Bike(){
    System.out.println(“T am bike”);
    }
    }
    复制代码
    复制代码
    /
    *
  • car类
    /
    public class Car implements Moveble{
    public Car(){
    System.out.println(“I am car”);
    }
    }
    复制代码
    复制代码
    /
    *
  • 简单工厂 只要是类都可以生产
    /
    public class SimplerFactory {
    public Moveble createCar(){
    return new Car();
    }
    public Moveble createBike(){
    return new Bike();
    }
       public Food createBread(){
    return new Bread();
    }
    }
    复制代码
    复制代码
    /
    *
  • 测试类
    */
    public class T {
    public static void main(String[] args) {
    //生产car
    Moveble car = new SimplerFactory().createCar();
    //生产bike
    Moveble bike = new SimplerFactory().createBike();
         //生产bike
    Food bread = new SimplerFactory().createBread();
      }
    }
    复制代码
    3、工厂方法

复制代码
/**

  • 可移动的物体抽象接口
    */
    public abstract interface Moveble {
    }

/**

  • 食品抽象类
    */
    public abstract class Food {
    }

/**

  • bike类
    */
    public class Bike implements Moveble{
    public Bike(){
    System.out.println(“T am bike”);
    }
    }

/**

  • car类
    */
    public class Car implements Moveble{
    public Car(){
    System.out.println(“I am car”);
    }
    }

/**

  • bread类
    */
    public class Bread extends Food{
    public Bread(){
    System.out.println(“T am bread”);
    }
    }

/**

  • bike工厂
    */
    public class BikeFactory {
    public Moveble create(){
    return new Bike();
    }
    }

/**

  • car工厂
    */
    public class CarFactory {
    public Moveble create(){
    return new Car();
    }
    }

/**

  • bread工厂
    */
    public class BreadFactory {
    public Food create(){
    return new Bread();
    }
    }

/**

  • 测试类 ,工厂方法 一种类一个工厂
    */
    public class T {
    public static void main(String[] args) {
    //生产car
    Moveble car = new BikeFactory().create();
    //生产bike
    Moveble bike = new CarFactory().create();
    //生产bread
    Food bread = new BreadFactory().create();
    }
    }
    复制代码
    4、抽象工厂

复制代码
/**

  • 食品抽象类
    */
    public abstract class Food {
    }

/**

  • 交通工具抽象类
    */
    public abstract class Transportation {
    }

/**

  • 电子设备抽象类
    */
    public abstract class Electronic {
    }

/**

  • bread类
    */
    public class Bread extends Food {
    public Bread(){
    System.out.println(“T am bread”);
    }
    }

/**

  • plane类
    */
    public class Plane extends Transportation{
    public Plane(){
    System.out.println(“I am plane”);
    }
    }

/**

  • phone类
    */
    public class Phone extends Electronic{
    public Phone(){
    System.out.println(“I am phone”);
    }
    }

/**

  • 具体的工厂 可以生产一组类
    */
    public class HaierFactory extends AbstractFactory{

    Food createFood() {
    return new Bread();
    }

    Electronic createElectronic() {
    return new Phone();
    }

    Transportation createTransportation() {
    return new Plane();
    }
    }

/**

  • cake类
    */
    public class Cake extends Food {
    public Cake(){
    System.out.println(“T am cake”);
    }
    }

/**

  • Tank类
    */
    public class Tank extends Transportation{
    public Tank(){
    System.out.println(“I am tank”);
    }
    }

/**

  • Computer类
    */
    public class Computer extends Electronic{
    public Computer(){
    System.out.println(“T am pc”);
    }
    }

/**

  • 具体的工厂,可以生产一组类
    */
    public class SuningFactory extends AbstractFactory{
    Food createFood() {
    return new Cake();
    }
    Electronic createElectronic() {
    return new Computer();
    }
    Transportation createTransportation() {
    return new Tank();
    }
    }

/**

  • 测试类,抽象工厂可以生产一组类,并且可以很灵活的扩展一组类的创建过程,但是不能做到灵活扩展一种类
    */
    public class T {
    public static void main(String[] args) {
    //海尔工厂可以生产面包 飞机 手机
    AbstractFactory haierFactory = new HaierFactory();
    Food bread = haierFactory.createFood();
    Transportation plane = haierFactory.createTransportation();
    Electronic phone = haierFactory.createElectronic();
    //苏宁工厂可以生产 蛋糕 坦克 pc电脑
    AbstractFactory suningFactory = new SuningFactory();
    Food cate = haierFactory.createFood();
    Transportation tank = haierFactory.createTransportation();
    Electronic computer = haierFactory.createElectronic();
    }
    }
    亚马逊测评www.yisuping.com

你可能感兴趣的:(JAVA设计模式之工厂系列(factory))