抽象工厂类

    1. package miluo.design.patterns.factoryMethod;
    2. import miluo.design.patterns.simpleFactory.Machine;
    3. /**
    4. * @author Miluo
    5. * @className AbstractFactory
    6. * @description
    7. * @date 2022/2/16
    8. **/
    9. public abstract class AbstractFactory {
    10. /**
    11. * getMachine
    12. * @return Machine
    13. */
    14. abstract Machine getMachine();
    15. }

    具体工厂类

    1. package miluo.design.patterns.factoryMethod;
    2. import miluo.design.patterns.simpleFactory.ChinaMachine;
    3. import miluo.design.patterns.simpleFactory.Machine;
    4. /**
    5. * @author Miluo
    6. * @className CnFactory
    7. * @description
    8. * @date 2022/2/16
    9. **/
    10. public class CnFactory extends AbstractFactory {
    11. @Override
    12. Machine getMachine() {
    13. return new ChinaMachine();
    14. }
    15. }
    1. package miluo.design.patterns.factoryMethod;
    2. import miluo.design.patterns.simpleFactory.AmericanMachine;
    3. import miluo.design.patterns.simpleFactory.Machine;
    4. /**
    5. * @author Miluo
    6. * @className CnFactory
    7. * @description
    8. * @date 2022/2/16
    9. **/
    10. public class UsaFactory extends AbstractFactory {
    11. @Override
    12. Machine getMachine() {
    13. return new AmericanMachine();
    14. }
    15. }

    抽象产品类

    1. package miluo.design.patterns.simpleFactory;
    2. /**
    3. * @author Miluo
    4. * @className Machine
    5. * @description 抽象产品类
    6. * @date 2022/2/16
    7. **/
    8. public abstract class Machine {
    9. /**
    10. * sayHello
    11. * @return String
    12. */
    13. public abstract String welcome();
    14. }

    具体产品类

    1. package miluo.design.patterns.factoryMethod;
    2. import miluo.design.patterns.simpleFactory.ChinaMachine;
    3. import miluo.design.patterns.simpleFactory.Machine;
    4. /**
    5. * @author Miluo
    6. * @className CnFactory
    7. * @description
    8. * @date 2022/2/16
    9. **/
    10. public class CnFactory extends AbstractFactory {
    11. @Override
    12. Machine getMachine() {
    13. return new ChinaMachine();
    14. }
    15. }
    1. package miluo.design.patterns.factoryMethod;
    2. import miluo.design.patterns.simpleFactory.ChinaMachine;
    3. import miluo.design.patterns.simpleFactory.Machine;
    4. /**
    5. * @author Miluo
    6. * @className CnFactory
    7. * @description
    8. * @date 2022/2/16
    9. **/
    10. public class CnFactory extends AbstractFactory {
    11. @Override
    12. Machine getMachine() {
    13. return new ChinaMachine();
    14. }
    15. }