工厂类

    1. package miluo.design.patterns.simpleFactory;
    2. /**
    3. * @author Miluo
    4. * @className MachineFactory
    5. * @description 工厂类
    6. * @date 2022/2/16
    7. **/
    8. public class MachineFactory {
    9. public static Machine getMachine(String country) throws Exception {
    10. if ("cn".equals(country.toLowerCase())) {
    11. return new ChinaMachine();
    12. }else if ("usa".equals(country.toLowerCase())){
    13. return new AmericanMachine();
    14. }else {
    15. throw new Exception("请输入正确参数");
    16. }
    17. }
    18. }

    抽象产品类

    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.simpleFactory;
    2. /**
    3. * @author Miluo
    4. * @className ChinaMachine
    5. * @description
    6. * @date 2022/2/16
    7. **/
    8. public class ChinaMachine extends Machine {
    9. @Override
    10. public String welcome() {
    11. return "你好";
    12. }
    13. }
    1. package miluo.design.patterns.simpleFactory;
    2. /**
    3. * @author Miluo
    4. * @className ChinaMachine
    5. * @description
    6. * @date 2022/2/16
    7. **/
    8. public class AmericanMachine extends Machine {
    9. @Override
    10. public String welcome() {
    11. return "Hello";
    12. }
    13. }

    测试

    1. package miluo.design.patterns.simpleFactory;
    2. /**
    3. * @author Miluo
    4. * @className testSimpleFactory
    5. * @description
    6. * @date 2022/2/16
    7. **/
    8. public class TestSimpleFactory {
    9. public static void main(String[] args) {
    10. try {
    11. Machine cn = MachineFactory.getMachine("cn");
    12. System.out.println(cn.welcome());
    13. Machine usa = MachineFactory.getMachine("USA");
    14. System.out.println(usa.welcome());
    15. Machine jp = MachineFactory.getMachine("jp");
    16. System.out.println(jp.welcome());
    17. } catch (Exception e) {
    18. e.printStackTrace();
    19. }
    20. }
    21. }
    22. **********************************
    23. Connected to the target VM, address: '127.0.0.1:7163', transport: 'socket'
    24. 你好
    25. Hello
    26. java.lang.Exception: 请输入正确参数
    27. at miluo.design.patterns.simpleFactory.MachineFactory.getMachine(MachineFactory.java:16)
    28. at miluo.design.patterns.simpleFactory.TestSimpleFactory.main(TestSimpleFactory.java:16)
    29. Disconnected from the target VM, address: '127.0.0.1:7163', transport: 'socket'
    30. Process finished with exit code 0
    31. **********************************

    测试

    1. package miluo.design.patterns.factoryMethod;
    2. import miluo.design.patterns.simpleFactory.Machine;
    3. /**
    4. * @author Miluo
    5. * @className TestFactoryMethod
    6. * @description
    7. * @date 2022/2/16
    8. **/
    9. public class TestFactoryMethod {
    10. public static void main(String[] args) {
    11. CnFactory cnFactory = new CnFactory();
    12. Machine cnFactoryMachine = cnFactory.getMachine();
    13. System.out.println(cnFactoryMachine.welcome());
    14. UsaFactory usaFactory = new UsaFactory();
    15. Machine usaFactoryMachine = usaFactory.getMachine();
    16. System.out.println(usaFactoryMachine.welcome());
    17. }
    18. }
    19. ***********************************
    20. Connected to the target VM, address: '127.0.0.1:7725', transport: 'socket'
    21. 你好
    22. Hello
    23. Disconnected from the target VM, address: '127.0.0.1:7725', transport: 'socket'
    24. ***********************************