Java常用设计模式

设计模式

装饰器模式

  1. 定义接口

    public interface Component {
        void doSomeThing();
    }
    
  2. 具体构建角色

    public class ConcreateCompnent implements Component {
        @Override
        public void doSomeThing() {
            System.out.println("A功能");
        }
    }
    
  3. 声明装饰器

    public class Decorator implements Component {
        private Component component;
    
        Decorator(Component component) { 
            this.component = component;
        }
        @Override
        public void doSomeThing() {
            component.doSomeThing();
        }
    }
    
    
  4. 装饰类的具体实现

    public class ConcratorDecorator1 extends Decorator {
    
        ConcratorDecorator1(Component component) { //必须传入具体构建角色才能够装饰
            super(component);
    
        }
        @Override
        public void doSomeThing() { //在保留父类方法的同时拓展新的方法
            super.doSomeThing();
            this.doAnotherThing();
        }
        public void doAnotherThing() { //拓展的新方法
            System.out.println("功能B");
        }
    }
    
    public class ConcratorDecorator2 extends Decorator {
        ConcratorDecorator2(Component component) {
            super(component);
        }
        @Override
        public void doSomeThing() {  
            super.doSomeThing();
            this.doAnotherThing();
    
        }
        public void doAnotherThing() {
            System.out.println("功能C");
        }
    }
    
    
  5. Test

    Component component = new ConcratorDecorator2(new ConcratorDecorator1(new ConcreateCompnent())); //具体构建对象new ConreateComponent()同时具有了装饰器1和在装饰器2所提供的拓展方法
    component.doSomeThing(); 
    

单例模式

  1. 双重检查模式(线程安全)

    class Singleton {
        private volatile static Singleton singleton;
    
        //不允许直接被外部调用构造方法
        private Singleton() {
    
        }
        public static Singleton getSingleton(){
            if (singleton == null) {
                synchronized (Singleton.class) {
                    if (singleton == null) {
                        singleton = new Singleton();
                    }
                }
            }
            return singleton;
        }
    
    }
    
  2. 静态内部类单例模式

    class Singleton {
        private Singleton() {
    
        }
    
        public static Singleton getInstance() {
            return SingletonHolder.instance;
        }
    
        private static class SingletonHolder {
            private static final Singleton instance = new Singleton();
        }
    }
    
  1. 枚举单例

    enum Sigleton{
        Instance
    }
    

持续补充中

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 设计模式分类 总体来说设计模式分为三大类:创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原...
    lifeline丿毅阅读 5,023评论 0 2
  • JAVA面试题 1、作用域public,private,protected,以及不写时的区别答:区别如下:作用域 ...
    JA尐白阅读 4,894评论 1 0
  • JAVA面试题相关基础知识 1、面向对象的特征有哪些方面 ①抽象: 抽象是忽略一个主题中与当前目标无关的那些方面,...
    小宇java阅读 4,413评论 0 6
  • 怎样可以让自己变得自信? 有人说锻炼自信,可以每天早上起来对着镜子说自己是最棒的,让自己相信自己,自我催眠法。 应...
    刘伟星阅读 1,508评论 0 0
  • 1、概述 质量损失是指企业在生产、经营过程和活动中,由于产品的质量问题而导致的损失,即由于质量低劣而产生的内、外部...
    商未央阅读 8,930评论 0 0

友情链接更多精彩内容