java web Series: factory pattern (code)

Keywords: shell Attribute

Theory of factory model: https://mp.csdn.net/postedit/96699960

Code to simulate the plant production process of plant vs. zombie

Route

1. Create Plant interface Plant

public interface Plant {
    public String getName();
    public void fight();

}

2. Create basic plant features (weapons, hair, etc.)

public class Arms {
    private String bulletType;

    public String getBulletType() {
        return bulletType;
    }

    public void setBulletType(String bulletType) {
        this.bulletType = bulletType;
    }
}
public class Hair {
    private String color;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
package org.imooc.component;

public class Shell {
    private Integer hardness;

    public Integer getHardness() {
        return hardness;
    }

    public void setHardness(Integer hardness) {
        this.hardness = hardness;
    }
}

3. Plant inheritance interface and creation attribute

public class Ice implements Plant{
    private Hair hair;
    private Arms arms;
    public String getName(){return "Blue ice";}
    public void fight(){
        System.out.println("Launch an ice bean");
    }

    public Hair getHair() {
        return hair;
    }

    public void setHair(Hair hair) {
        this.hair = hair;
    }

    public Arms getArms() {
        return arms;
    }

    public void setArms(Arms arms) {
        this.arms = arms;
    }
}

Other three similar

4. Define class path to facilitate direct call and modification through reflection

public class PlantNameConstant {
    /**
     * Mung bean
     */
    public static final String BEAN_NAME="org.imooc.factory.BeanFactory";
    /**
     * Ice bean
     */
    public static final String ICE_NAME="org.imooc.factory.IceFactory";
    /**
     * Fruit wall
     */
    public static final String WALL_NAME="org.imooc.factory.WallFactory";
}

5. Create a factory to produce iced beans

public class IceFactory implements Factory{
    public Plant createPlant(){
        Ice ice=new Ice();
        Hair hair=new Hair();
        hair.setColor("blue");
        ice.setHair(hair);

        Arms arms=new Arms();
        arms.setBulletType("Common beans");
        ice.setArms(arms);

        return ice;
    }
}

6. A factory will be designated by reflection

public class FactoryBuilder {
//Specify factory by path name
//    public  static Factory build(String name){
//        Factory factory=null;
//        if (PlantNameConstant.BEAN_NAME.equals(name)) {
//factory=new BeanFactory();
//
//        }else if (PlantNameConstant.ICE_NAME.equals(name)){
//            factory=new IceFactory();
//
//        }else if (PlantNameConstant.WALL_NAME.equals(name)){
//            factory=new WallFactory();
//        }
//        return factory;
//    }

    //Specify a factory by reflection
    public static Factory buildByClassName(String name) throws Exception{
        return (Factory)Class.forName(name).newInstance();
    }
}

7. Extract and call factory methods for decoupling, and the same code can be reused.

public class Function {
    public void put(String name, int number) throws Exception {
        //Specify factory, such as BeanFactory, etc.
        Factory factory= FactoryBuilder.buildByClassName(name);
        //Using factories to produce plants
        Plant plant= factory.createPlant();

        System.out.println("take"+plant.getName()+"Put on"+number+"On the grass");
        plant.fight();
    }
}

8. test

public class MyMain {
    public static void main(String[] args) throws Exception {
        Function function=new Function();
        function.put(PlantNameConstant.BEAN_NAME, 8);
        function.put(PlantNameConstant.ICE_NAME, 15);
        function.put(PlantNameConstant.WALL_NAME, 9);

    }
}

9. results

Put the mungbean on the grass No.8
Launch a bean
Put blue ice on grass 15
Launch an ice bean
Put the fruit wall on grass 9
Standing steadily on the grass
 

Posted by Scorptique on Sat, 19 Oct 2019 13:31:09 -0700