抽象工厂类

    1. package miluo.design.patterns.abstractFactory;
    2. /**
    3. * @author Miluo
    4. * @className AbstractFactory
    5. * @description 抽象工厂模式
    6. * @date 2022/2/24
    7. **/
    8. public abstract class AbstractFactory {
    9. /**
    10. * 创建手机
    11. * @return 手机抽象类
    12. */
    13. abstract AbstractPhoneProduct createPhone();
    14. /**
    15. * 创建智能手表
    16. * @return 手表抽象类
    17. */
    18. abstract AbstractWatchProduct createSmartWatch();
    19. }

    具体工厂类

    1. package miluo.design.patterns.abstractFactory;
    2. /**
    3. * @author Miluo
    4. * @className AppleFactory
    5. * @description
    6. * @date 2022/2/24
    7. **/
    8. public class AppleFactory extends AbstractFactory {
    9. @Override
    10. AbstractPhoneProduct createPhone() {
    11. return new ApplePhone();
    12. }
    13. @Override
    14. AbstractWatchProduct createSmartWatch() {
    15. return new AppleWatch();
    16. }
    17. }
    1. package miluo.design.patterns.abstractFactory;
    2. /**
    3. * @author Miluo
    4. * @className AppleFactory
    5. * @description
    6. * @date 2022/2/24
    7. **/
    8. public class HuaWeiFactory extends AbstractFactory {
    9. @Override
    10. AbstractPhoneProduct createPhone() {
    11. return new HuaWeiPhone();
    12. }
    13. @Override
    14. AbstractWatchProduct createSmartWatch() {
    15. return new HuaWeiWatch();
    16. }
    17. }

    抽象产品类

    1. package miluo.design.patterns.abstractFactory;
    2. /**
    3. * @author Miluo
    4. * @className AbstractProduct
    5. * @description 手机抽象类
    6. * @date 2022/2/24
    7. **/
    8. public abstract class AbstractPhoneProduct {
    9. /**
    10. * 打开手机
    11. */
    12. abstract void open();
    13. /**
    14. * 关闭手机
    15. */
    16. abstract void close();
    17. /**
    18. * 打电话
    19. */
    20. abstract void call();
    21. }
    1. package miluo.design.patterns.abstractFactory;
    2. /**
    3. * @author Miluo
    4. * @className AbstractProduct
    5. * @description 手表抽象类
    6. * @date 2022/2/24
    7. **/
    8. public abstract class AbstractWatchProduct {
    9. /**
    10. * 打开手表
    11. */
    12. abstract void open();
    13. /**
    14. * 关闭手表
    15. */
    16. abstract void close();
    17. /**
    18. * 查看手表
    19. */
    20. abstract void look();
    21. }

    具体产品类

    1. package miluo.design.patterns.abstractFactory;
    2. /**
    3. * @author Miluo
    4. * @className ApplePhone
    5. * @description
    6. * @date 2022/2/24
    7. **/
    8. public class ApplePhone extends AbstractPhoneProduct {
    9. @Override
    10. void open() {
    11. System.out.println("打开苹果手机");
    12. }
    13. @Override
    14. void close() {
    15. System.out.println("关闭苹果手机");
    16. }
    17. @Override
    18. void call() {
    19. System.out.println("苹果手机打电话");
    20. }
    21. }
    1. package miluo.design.patterns.abstractFactory;
    2. /**
    3. * @author Miluo
    4. * @className HuaWeiWatch
    5. * @description
    6. * @date 2022/2/24
    7. **/
    8. public class AppleWatch extends AbstractWatchProduct {
    9. @Override
    10. void open() {
    11. System.out.println("打开苹果手表");
    12. }
    13. @Override
    14. void close() {
    15. System.out.println("关闭苹果手表");
    16. }
    17. @Override
    18. void look() {
    19. System.out.println("查看苹果手表信息");
    20. }
    21. }
    1. package miluo.design.patterns.abstractFactory;
    2. /**
    3. * @author Miluo
    4. * @className ApplePhone
    5. * @description
    6. * @date 2022/2/24
    7. **/
    8. public class HuaWeiPhone extends AbstractPhoneProduct {
    9. @Override
    10. void open() {
    11. System.out.println("打开华为手机");
    12. }
    13. @Override
    14. void close() {
    15. System.out.println("关闭华为手机");
    16. }
    17. @Override
    18. void call() {
    19. System.out.println("华为手机打电话");
    20. }
    21. }
    1. package miluo.design.patterns.abstractFactory;
    2. /**
    3. * @author Miluo
    4. * @className HuaWeiWatch
    5. * @description
    6. * @date 2022/2/24
    7. **/
    8. public class HuaWeiWatch extends AbstractWatchProduct {
    9. @Override
    10. void open() {
    11. System.out.println("打开华为手表");
    12. }
    13. @Override
    14. void close() {
    15. System.out.println("关闭华为手表");
    16. }
    17. @Override
    18. void look() {
    19. System.out.println("查看华为手表信息");
    20. }
    21. }

    测试

    1. package miluo.design.patterns.abstractFactory;
    2. /**
    3. * @author Miluo
    4. * @className TestAbstractFactory
    5. * @description
    6. * @date 2022/2/24
    7. **/
    8. public class TestAbstractFactory {
    9. public static void main(String[] args) {
    10. AppleFactory appleFactory = new AppleFactory();
    11. AbstractPhoneProduct applePhone = appleFactory.createPhone();
    12. applePhone.open();
    13. applePhone.call();
    14. applePhone.close();
    15. HuaWeiFactory huaWeiFactory = new HuaWeiFactory();
    16. AbstractWatchProduct huaWeiWatch = huaWeiFactory.createSmartWatch();
    17. huaWeiWatch.open();
    18. huaWeiWatch.look();
    19. huaWeiWatch.close();
    20. }
    21. }
    22. ****************************************
    23. Connected to the target VM, address: '127.0.0.1:7888', transport: 'socket'
    24. 打开苹果手机
    25. 苹果手机打电话
    26. 关闭苹果手机
    27. 打开华为手表
    28. 查看华为手表信息
    29. 关闭华为手表
    30. Disconnected from the target VM, address: '127.0.0.1:7888', transport: 'socket'
    31. ****************************************