factory of JAVA design pattern

Any method or class that can generate an object can be called a factory. The single example is the so-called static factory.

Why do we need factories when we have new in jdk?

a. flexible control of production process

b. decorate the object, or give access to the object, or add some log information in the object production process, or add some actual business processing according to the application scenario, etc.

1. Static factory

Singleton mode: a special static factory, please refer to Singleton mode

2. Simple factory

/**
 * Mobile object abstract interface
*/ 
public interface Moveble { }
/**
 * Abstract food
 */
public abstract class Food{
}
/**
 * Bread
 */
public class bread extends Food {
    public Bike(){
        System.out.println("T am bread");
    }
}
/**
 * bike class
 */
public class Bike implements Moveble{
    public Bike(){
        System.out.println("T am bike");
    }
}
/**
 * car class
 */
public class Car implements Moveble{
    public Car(){
        System.out.println("I am car");
    }
}
/**
 * A simple factory can produce as long as it is a class
 */
public class SimplerFactory {
    public Moveble createCar(){
        return new Car();
    }
    public Moveble createBike(){
        return new Bike();
    }
   public Food createBread(){
        return new Bread();
    }
}
/**
 * Test class
 */
public class T {
    public static void main(String[] args) {
        //production car
        Moveble car = new SimplerFactory().createCar();
        //production bike
        Moveble bike = new SimplerFactory().createBike();
     //Make bike
        Food bread = new SimplerFactory().createBread();
  }
}

3. Factory method

/**
 * Mobile object abstract interface
 */
public abstract interface Moveble {
}

/**
 * Abstract food
 */
public abstract  class Food {
}

/**
 * bike class
 */
public class Bike implements Moveble{
    public Bike(){
        System.out.println("T am bike");
    }
}

/**
 * car class
 */
public class Car implements Moveble{
    public Car(){
        System.out.println("I am car");
    }
}

/**
 * bread class
 */
public class Bread extends Food{
    public Bread(){
        System.out.println("T am bread");
    }
}

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

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


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

/**
 * Test class, factory method one kind one factory
 */
public class T {
    public static void main(String[] args) {
        //production car
        Moveble car = new BikeFactory().create();
        //production bike
        Moveble bike = new CarFactory().create();
        //production bread
        Food bread = new BreadFactory().create();
    }
}

4. Abstract factory

/**
 * Abstract food
 */
public abstract  class Food {
}

/**
 * Vehicle abstract class
 */
public abstract class Transportation {
}

/**
 *  Abstract class of electronic equipment
 */
public abstract class Electronic {
}

/**
 * bread class
 */
public class Bread extends Food {
    public Bread(){
        System.out.println("T am bread");
    }
}

/**
 * plane class
 */
public class Plane extends Transportation{
    public Plane(){
        System.out.println("I am plane");
    }
}

/**
 * phone class
 */
public class Phone extends Electronic{
    public Phone(){
        System.out.println("I am phone");
    }
}

/**
 * Specific factories can produce a group of categories
 */
public class HaierFactory extends AbstractFactory{

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

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

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

/**
 * cake class
 */
public class Cake extends Food {
    public Cake(){
        System.out.println("T am cake");
    }
}

/**
 * Tank class
 */
public class Tank extends Transportation{
    public Tank(){
        System.out.println("I am tank");
    }
}

/**
 * Computer class
 */
public class Computer extends Electronic{
    public Computer(){
        System.out.println("T am pc");
    }
}

/**
 * Specific factories can produce a group of classes
 */
public class SuningFactory extends AbstractFactory{
    Food createFood() {
        return new Cake();
    }
    Electronic createElectronic() {
        return new Computer();
    }
    Transportation createTransportation() {
        return new Tank();
    }
}

/**
 * Test classes, abstract factories can produce a group of classes, and can flexibly extend the creation process of a group of classes, but they cannot flexibly extend a group of classes
 */
public class T {
    public static void main(String[] args) {
        //Haier factory can produce bread plane mobile phones
        AbstractFactory haierFactory = new HaierFactory();
        Food bread = haierFactory.createFood();
        Transportation plane = haierFactory.createTransportation();
        Electronic phone = haierFactory.createElectronic();
        //Suning factory can produce cake tank pc Computer
        AbstractFactory suningFactory = new SuningFactory();
        Food cate = haierFactory.createFood();
        Transportation tank = haierFactory.createTransportation();
        Electronic computer = haierFactory.createElectronic();
    }
}

5. Spring's bean factory

You can flexibly control the creation process of a group of classes or a kind of creation process. For details, please read BeanFactory in Spring source code.

Tags: Java Mobile Spring JDK

Posted on Wed, 29 Apr 2020 10:12:43 -0700 by mingo