美文网首页
工厂模式

工厂模式

作者: virsus | 来源:发表于2020-04-26 10:49 被阅读0次

简单工厂

public class FactoryMode {
  interface Product {
        void hello();
    }

    static class Product1 implements Product {

        @Override
        public void hello() {
            System.out.println("hello,Product1");
        }
    }
    static class Product2 implements Product {

        @Override
        public void hello() {
            System.out.println("hello,Product2");
        }
    }

    static class SimpleFactory {
        public static Product getProduct(String type) {
            if ("1".equals(type)) {
                return new Product1();
            }
            if ("2".equals(type)) {
                return new Product2();
            } else {
                return null;
            }
        }
    }
    public static void main(String[] args) {
        Product product = SimpleFactory.getProduct("1");
        product.hello();
    }
}

工厂方法

public class FactoryMode {
    interface Factory {
        Product create();
    }
    interface Product {
        void hello();
    }

    static class Factory1 implements Factory {

        @Override
        public Product create() {
            return new Product1();
        }
    }
    static class Factory2 implements Factory {

        @Override
        public Product create() {
            return new Product2();
        }
    }

    static class Product1 implements Product {

        @Override
        public void hello() {
            System.out.println("hello,Product1");
        }
    }
    static class Product2 implements Product {

        @Override
        public void hello() {
            System.out.println("hello,Product2");
        }
    }

    public static void main(String[] args) {
        Factory factory = new Factory1();
        Product product = factory.create();
        product.hello();
    }
}

工厂方法跟简单工厂的组合

static class FactoryMap {
        private static final Map<String,Factory> map = new HashMap<>();
        static {
            map.put("1",new Factory1());
            map.put("2",new Factory2());
        }
        public static Factory getFactory(String type) {
            if (StringUtils.isEmpty(type)) {
                return null;
            }
            return map.get(type);
        }
    }

    public static void main(String[] args) {
        Factory factory = FactoryMap.getFactory("1");
        Product product = factory.create();
        product.hello();
    }

总结

  • 简单工厂把对象的创建过程抽离出来
  • 把独立的代码块抽离出来让代码逻辑更清晰,可读性更好
  • 利用多态去实现工厂方法模式,可以去掉if判断,新加类型时的改动,更加符合开闭原则

相关文章

  • 常用设计模式

    设计模式 工厂模式 工厂模式思路上分:简单工厂模式,工厂模式, 抽象工厂模式// 抽象工厂模式可以代替工厂模式,做...

  • 工厂模式

    工厂模式细分三种:简单工厂模式、工厂模式、抽象工厂模式。 工厂模式相当于抽象了简单工厂模式的工厂类,而抽象工厂模式...

  • 工厂模式

    工厂模式 就是工厂---生产-->产品 在设计模式中,分为 简单工厂模式, 工厂方法模式,抽象工厂模式. 工厂模式...

  • 找女朋友之简单工厂模式,工厂模式,抽象工厂模式

    找女朋友之简单工厂模式,工厂模式,抽象工厂模式 找女朋友之简单工厂模式,工厂模式,抽象工厂模式

  • 【设计模式】- 工厂模式

    工厂模式分为三种:简单工厂模式、工厂方法模式和抽象工厂模式。 工厂模式:靠工厂生产对象 简单工厂模式中只有一个工厂...

  • 工厂模式

    工厂模式包含三种模式:简单工厂模式、工厂方法模式和抽象工厂模式。 简单工厂模式 定义简单工厂模式:由一个工厂类根据...

  • Java设计模式——工厂模式

    工厂模式简单工厂模式工厂方法模式抽象工厂模式 1.简单工厂模式 1.基本介绍1)简单工厂模式也叫静态工厂模式,是属...

  • 设计模式-3种工厂模式

    工厂模式包括:简单工厂模式,工厂方法模式,抽象工厂模式 简单工厂模式 工厂方法根据参数直接创建实例:工厂->产品 ...

  • 设计模式-工厂模式

    工厂模式概念 实例化对象,用工厂方法代替new操作。工厂模式包括工厂方法模式和抽象工厂模式。抽象工厂模式是工厂模式...

  • 第一章2.0工厂- 基础类准备

    2.1工厂-简单工厂模式2.2工厂-工厂方法模式2.3工厂-抽象工厂模式

网友评论

      本文标题:工厂模式

      本文链接:https://www.haomeiwen.com/subject/xxcdwhtx.html