Design Patterns - Abstract Factory Pattern

The last time Nuwa created a human being, she forgot to define gender for human beings.
Now the gossip furnace needs to be disassembled, one for men and one for women
write picture description here

/**
 * Created by xpeng on 2018/5/1.
 * 定义一个人类的统称
 */

public interface Human {
    //首先定义什么是人类

    //人是会笑的
    public void laugh();

    //人类还会哭
    public void cry();

    //人类会说话
    public void talk();

    //定义性别
    public void sex();
}

The human interface is defined, and then three abstract classes are created according to the interface, that is, three product levels, and the three methods of laugh(), cry(), and talk() are implemented.

/**
 * Created by xpeng on 2018/5/5.
 * 为什么要修改成抽象类呢?要定义性别啊
 */

public abstract class AbstractYellowHuman implements Human {
    @Override
    public void cry() {
        Log.e("xyz","黄色人类会哭");
    }

    @Override
    public void talk() {

        Log.e("xyz","黄色人类会说话");
    }

    @Override
    public void laugh() {
        Log.e("xyz","黄色人类会笑");
    }

}
public abstract class AbstractWhiteHuman implements Human {
    @Override
    public void cry() {
        Log.e("xyz","白色人类会哭");
    }

    @Override
    public void talk() {
        Log.e("xyz","白色人类会说话");
    }

    @Override
    public void laugh() {
        Log.e("xyz","白色人类会笑");
    }

}
/**
 * Created by xpeng on 2018/5/5.
 */

public abstract class AbstractBlackHuman implements Human {
    @Override
    public void cry() {
        Log.e("xyz","黑色人类会哭");
    }

    @Override
    public void talk() {
        Log.e("xyz","黑色人类会说话");
    }

    @Override
    public void laugh() {
        Log.e("xyz","黑色人类会笑");
    }

}

The three abstract classes have been implemented, and then the concrete implementation class.

/**
 * Created by xpeng on 2018/5/1.
 * 女性黄色人类
 */

public class YellowFemaleHuman extends AbstractYellowHuman {
    @Override
    public void sex() {
        Log.e("xyz"," 女性黄色人 ");
    }
}
/**
 * Created by xpeng on 2018/5/1.
 * 男性黄色人类
 */

public class YellowMaleHuman extends AbstractYellowHuman {
    @Override
    public void sex() {
        Log.e("xyz"," 男性黄色人 ");
    }
}
/**
 * Created by xpeng on 2018/5/1.
 * 女性白色人类
 */

public class WhiteFemaleHuman extends AbstractWhiteHuman {
    @Override
    public void sex() {
        Log.e("xyz"," 女性白色人 ");
    }
}
/**
 * Created by xpeng on 2018/5/1.
 * 男性白色人类
 */

public class WhiteMaleHuman extends AbstractWhiteHuman {
    @Override
    public void sex() {
        Log.e("xyz"," 男性白色人 ");
    }
}
/**
 * Created by xpeng on 2018/5/1.
 * 女性黑色人类
 */

public class BlackFemaleHuman extends AbstractBlackHuman {
    @Override
    public void sex() {
        Log.e("xyz"," 女性黑色人 ");
    }
}
/**lack
 * Created by xpeng on 2018/5/1.
 * 男性黑色人类
 */

public class BlackMaleHuman extends AbstractBlackHuman {
    @Override
    public void sex() {
        Log.e("xyz"," 男性黑色人 ");
    }
}

The product level and product family in the abstract factory mode have been completed, that is, the human and the generated human have been defined. The next step is to wait for the factory to start and create, and then create the factory class

/**
 * Created by xpeng on 2018/5/5.
 * 世界上有哪些类型的人,列出来
 */

public enum HumanEnum {

    YelloMaleHuman("com.example.xpeng.mode.YellowMaleHuman"),
    YelloFemalHuman("com.example.xpeng.mode.YellowFemaleHuman"),
    WhiteFemalHuman("com.example.xpeng.mode.WhiteFemaleHuman"),
    WhiteMaleHuman("com.example.xpeng.mode.WhiteMaleHuman"),
    BlackMaleHuman("com.example.xpeng.mode.BlackMaleHuman"),
    BlackFemalHuman("com.example.xpeng.mode.BlackFemaleHuman");

    private String value = "";

    //定义构造函数,目的是Data(value)类型的相匹配
    private HumanEnum(String value){
        this.value = value;
    }

    public String getValue(){
        return this.value;
    }

}

Factory class, first look at the interface:

/**
 * Created by xpeng on 2018/5/1.
 * 女娲八卦炉造人
 */

public interface HumanFactory {

   //制造黄色人类
    public Human createYellowHuman();

    //制造一个白色人类
    public Human createWhiteHuman();

    //制造一个黑色人类
    public Human createBlackHuman();

}

Then look at the abstract class:

/**
 * Created by xpeng on 2018/5/5.
 * 编写一个抽象类,根据enum创建一个类出来
 */

public abstract class AbstractHumanFactory implements HumanFactory {
    //给定一个性别人类,创建一个人类出来,专业术语是产生产品等级
    protected Human createHuman(HumanEnum humanEnum){
        Human human = null;

        //如果传递进来不是一个Enum中具体的一个Element的话,则不处理
        if (!humanEnum.getValue().equals("")){
            try {
                //直接生产一个实例
                human = (Human) Class.forName(humanEnum.getValue()).newInstance();
            }catch (Exception e){
                //因为使用了enum,这个异常情况不会产生了,除非你的enum有问题;
                e.printStackTrace();
            }
        }
        return human;
    }
}

This is the advantage of introducing enum. The createHuman(HumanEnum humanEnum) method defines that the input parameter must be of HumanEnum type, and then directly use the humanEnum.getValue() method to get the specific value passed in. The purpose of the abstract class is to reduce the amount of code that implements the following classes.
Next , implement the class
Male Factory, which only creates males:

/**
 * Created by xpeng on 2018/5/5.
 * 男性创建工厂
 */

public class MaleHumanFactory extends AbstractHumanFactory {

    //创建一个男性黑种人
    @Override
    public Human createYellowHuman() {
        return super.createHuman(HumanEnum.BlackMaleHuman);
    }

    //创建一个男性白种人
    @Override
    public Human createWhiteHuman() {
        return super.createHuman(HumanEnum.WhiteMaleHuman);
    }

    //创建一个男性黄种人
    @Override
    public Human createBlackHuman() {
        return super.createHuman(HumanEnum.YelloMaleHuman);
    }
}

Women's factory, only women are created;

/**
 * Created by xpeng on 2018/5/5.
 * 男性创建工厂
 */

public class FemaleHumanFactory extends AbstractHumanFactory {

    //创建一个男性黑种人
    @Override
    public Human createYellowHuman() {
        return super.createHuman(HumanEnum.YelloFemalHuman);
    }

    //创建一个男性白种人
    @Override
    public Human createWhiteHuman() {
        return super.createHuman(HumanEnum.WhiteFemalHuman);
    }

    //创建一个男性黄种人
    @Override
    public Human createBlackHuman() {
        return super.createHuman(HumanEnum.BlackFemalHuman);
    }
}

Okay, everything is defined, the next step is to create people.

  //第一条生产线,男性生产线
        HumanFactory maleHumanFactory = new MaleHumanFactory();

        //第二条生产线,女性生产线
        HumanFactory femalHumanFactory = new FemaleHumanFactory();

        //生产线建立完毕,开始生产人了
        Human maleYellowHuman = maleHumanFactory.createYellowHuman();

        Human femalYellowHuman = femalHumanFactory.createYellowHuman();

        maleYellowHuman.cry();
        Log.e("xyz"," maleYellow   ... cry ");
        maleYellowHuman.laugh();
        maleYellowHuman.talk();
        femalYellowHuman.sex();
        Log.e("xyz","  femalYellowHuman ... sex ");
        maleYellowHuman.sex();
        //......还可以创建很多,只要你愿意
    }

After the abstract factory pattern is finished, what are the advantages of the factory pattern?
1. Comply with OCP principle, open and closed principle. Scalable
2. High cohesion, low coupling

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325661275&siteId=291194637