Java常用設(shè)計模式

設(shè)計模式

裝飾器模式

  1. 定義接口

    public interface Component {
        void doSomeThing();
    }
    
  2. 具體構(gòu)建角色

    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. 裝飾類的具體實現(xiàn)

    public class ConcratorDecorator1 extends Decorator {
    
        ConcratorDecorator1(Component component) { //必須傳入具體構(gòu)建角色才能夠裝飾
            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())); //具體構(gòu)建對象new ConreateComponent()同時具有了裝飾器1和在裝飾器2所提供的拓展方法
    component.doSomeThing(); 
    

單例模式

  1. 雙重檢查模式(線程安全)

    class Singleton {
        private volatile static Singleton singleton;
    
        //不允許直接被外部調(diào)用構(gòu)造方法
        private Singleton() {
    
        }
        public static Singleton getSingleton(){
            if (singleton == null) {
                synchronized (Singleton.class) {
                    if (singleton == null) {
                        singleton = new Singleton();
                    }
                }
            }
            return singleton;
        }
    
    }
    
  2. 靜態(tài)內(nèi)部類單例模式

    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
    }
    

持續(xù)補充中

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 設(shè)計模式分類 總體來說設(shè)計模式分為三大類:創(chuàng)建型模式,共五種:工廠方法模式、抽象工廠模式、單例模式、建造者模式、原...
    lifeline丿毅閱讀 1,362評論 0 2
  • JAVA面試題 1、作用域public,private,protected,以及不寫時的區(qū)別答:區(qū)別如下:作用域 ...
    JA尐白閱讀 1,272評論 1 0
  • JAVA面試題相關(guān)基礎(chǔ)知識 1、面向?qū)ο蟮奶卣饔心男┓矫?①抽象: 抽象是忽略一個主題中與當(dāng)前目標(biāo)無關(guān)的那些方面,...
    小宇java閱讀 954評論 0 6
  • 怎樣可以讓自己變得自信? 有人說鍛煉自信,可以每天早上起來對著鏡子說自己是最棒的,讓自己相信自己,自我催眠法。 應(yīng)...
    劉偉星閱讀 224評論 0 0
  • 1、概述 質(zhì)量損失是指企業(yè)在生產(chǎn)、經(jīng)營過程和活動中,由于產(chǎn)品的質(zhì)量問題而導(dǎo)致的損失,即由于質(zhì)量低劣而產(chǎn)生的內(nèi)、外部...
    商未央閱讀 3,646評論 0 0

友情鏈接更多精彩內(nèi)容