美文网首页JAVA基础
创建相关的设计模式

创建相关的设计模式

作者: heheworld | 来源:发表于2020-03-12 16:07 被阅读0次
    • 简单工厂模式

    //产品接口
    public interface Product {
        void consume();
    }
    
    //工厂接口
    public interface SimpleFactory {
        Product createProduct(String type);
    }
    
    //工厂实现类
    public class SimpleFactoryImpl implements SimpleFactory {
    
        @Override
        public Product createProduct(String type) {
            Product p = null;
            switch (type) {
            case "A":
                p = new ProductA();
                break;
            case "B":
                p = new ProductB();
                break;
            case "C":
                p = new ProductC();
                break;
            }
            return p;
        }
    }
    
    • 工厂模式

    工厂生产行为交由子类去实现

    //工厂接口,
    public interface SimpleFactory {
        Product createProduct();
    }
    
    //工厂实现类A
    public class SimpleFactoryImplA implements SimpleFactory {
        @Override
        public Product createProduct() {
            return new ProductA();
        }
    }
    
    //工厂实现类B
    public class SimpleFactoryImplB implements SimpleFactory {
        @Override
        public Product createProduct() {
            return new ProductB();
        }
    }
    
    //测试类
    public class Test4Factory {
        public static void main(String[] args) {
            Factory factory = new Factory();
            factory.getFactory("A").createProduct();
        }
    }
    
    • 抽象工厂模式

    //工厂接口,
    public interface SimpleFactory {
        ProductA createProductA();
        ProductB createProductB();
    }
    
    //产品A接口
    public interface ProductA {
        void consumeA();
    }
    
    //产品B接口
    public interface ProductB {
        void consumeB();
    }
    
    //测试
        public static void main(String[] args) {
            SimpleFactory factory = new FactoryA();
            ProductA productA = factory.createProductA();
            productA.consume();
            
            
            
            SimpleFactory factory1 = new FactoryB();
            ProductA productA1 = factory1.createProductA();
            productA1.consume();
        }
    

    三种工厂模式的缺点:
    1.简单工厂:工厂类需要实现很多个case,随着业务的增长,工厂类逻辑,代码量越来越多
    2.工厂模式:一个工厂对应一个产品,产品变多的时候,要写的java文件也会越来越多
    3.抽象工厂模式:产品类是固定的,如果需要增加,则各个子类都要跟着增加,不是很友好

    • 单例模式

    public class SingleInstance {
    
        private static SingleInstance instance = null;
    
        private SingleInstance() {
        };
    
        public static SingleInstance getInstance() {
            if (instance == null) {
                synchronized (SingleInstance.class) {
                    instance = new SingleInstance();
                }
            }
            return instance;
        }
    }
    

    单例的另一种写法,基于jvm 初始化类的 原理

    public class SingleInstance {
    
        private SingleInstance() {
            
        }
        
        private static class SingleInstanceHolder{
            final static SingleInstance instance = new SingleInstance();
        }
        
        
        public static SingleInstance getInstance() {
            return SingleInstanceHolder.instance;
        }
    }
    
    

    单例模式需要注意的点:

    1. 主要是高并发引起的问题,如果加上同步,又可能引起效率问题,细抠的话上述代码还是有问题的(机器码执行顺序引起)
    • 构建者模式

    //需要构建的类
    public class Person {
        String name;
        int age;
        String idcard;
        boolean gender;
        public static Builder build() {
            return new Builder();
        }
    }
    
    //构建工具类
    public class Builder {
        private Person p;
    
        public Builder() {
            p = new Person();
        }
    
        Builder name(String name) {
            p.name = name;
            return this;
        }
    
        Builder age(int age) {
            p.age = age;
            return this;
        }
    
        Builder idcard(String idcard) {
            p.idcard = idcard;
            return this;
        }
    
        Builder gender(boolean gender) {
            p.gender = gender;
            return this;
        }
    
        Person build() {
            return p;
        }
    }
    
    //测试
        public static void main(String[] args) {
            Person.build().age(34).gender(false).idcard("1213442342432").name("test").build();
        }
    

    相关文章

      网友评论

        本文标题:创建相关的设计模式

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