java幾種常用設計模式簡單樣本

來源:互聯網
上載者:User

1.單例設計模式

       所謂單例設計模式簡單說就是無論程式如何運行,採用單例設計模式的類(Singleton類)永遠只會有一個執行個體化對象產生。具體實現步驟如下:

      (1) 將採用單例設計模式的類的構造方法私人化(採用private修飾)。

      (2) 在其內部產生該類的執行個體化對象,並將其封裝成private static類型。

      (3) 定義一個靜態方法返回該類的執行個體。

         範例程式碼如下:

  Java代碼   class Singleton {        private static Singleton instance = new Singleton();// 在內部產生本類的執行個體化對象          public static Singleton getInstance() { // 通過靜態方法返回instance對象           return instance;        }           private Singleton() { // 將構造方法封裝為私人化       }           public void print() {            System.out.println("Hello World!!!");        }    }       public class SingletonDemo {        public static void main(String args[]) {            Singleton s1 = null; // 聲明對象           Singleton s2 = null; // 聲明對象           Singleton s3 = null; // 聲明對象           s1 = Singleton.getInstance(); // 取得執行個體化對象           s2 = Singleton.getInstance(); // 取得執行個體化對象           s3 = Singleton.getInstance(); // 取得執行個體化對象           s1.print(); // 調用方法            s2.print(); // 調用方法            s3.print(); // 調用方法        }    }  

class Singleton {private static Singleton instance = new Singleton();// 在內部產生本類的執行個體化對象public static Singleton getInstance() { // 通過靜態方法返回instance對象return instance;}private Singleton() { // 將構造方法封裝為私人化}public void print() {System.out.println("Hello World!!!");}}public class SingletonDemo {public static void main(String args[]) {Singleton s1 = null; // 聲明對象Singleton s2 = null; // 聲明對象Singleton s3 = null; // 聲明對象s1 = Singleton.getInstance(); // 取得執行個體化對象s2 = Singleton.getInstance(); // 取得執行個體化對象s3 = Singleton.getInstance(); // 取得執行個體化對象s1.print(); // 調用方法s2.print(); // 調用方法s3.print(); // 調用方法}}

 一、單例模式的介紹
     Singleton是一種建立型模式,指某個類採用Singleton模式,則在這個類被建立後,只可能產生一個執行個體供外部存取,並且提供一個全域的訪問點

二、單例模式的實現

實現的方式有如下四種:
Java代碼   /**    *     * 單例模式的實現:餓漢式,安全執行緒 但效率比較低    */   public class SingletonTest {           private SingletonTest() {        }           private static final SingletonTest instance = new SingletonTest();           public static SingletonTest getInstancei() {            return instance;        }       }  

/** *  * 單例模式的實現:餓漢式,安全執行緒 但效率比較低 */public class SingletonTest {private SingletonTest() {}private static final SingletonTest instance = new SingletonTest();public static SingletonTest getInstancei() {return instance;}}


Java代碼   /**    * 單例模式的實現:飽漢式,非安全執行緒     *     */   public class SingletonTest {        private SingletonTest() {        }           private static SingletonTest instance;           public static SingletonTest getInstance() {            if (instance == null)                instance = new SingletonTest();            return instance;        }    }  

/** * 單例模式的實現:飽漢式,非安全執行緒  *  */public class SingletonTest {private SingletonTest() {}private static SingletonTest instance;public static SingletonTest getInstance() {if (instance == null)instance = new SingletonTest();return instance;}}


Java代碼   /**    * 安全執行緒,但是效率非常低    * @author vanceinfo    *    */   public class SingletonTest {        private SingletonTest() {        }           private static SingletonTest instance;           public static synchronized SingletonTest getInstance() {            if (instance == null)                instance = new SingletonTest();            return instance;        }    }  

/** * 安全執行緒,但是效率非常低 * @author vanceinfo * */public class SingletonTest {private SingletonTest() {}private static SingletonTest instance;public static synchronized SingletonTest getInstance() {if (instance == null)instance = new SingletonTest();return instance;}}


Java代碼   /**    * 安全執行緒  並且效率高    *    */   public class SingletonTest {        private static SingletonTest instance;           private SingletonTest() {        }           public static SingletonTest getIstance() {            if (instance == null) {                synchronized (SingletonTest.class) {                    if (instance == null) {                        instance = new SingletonTest();                    }                }            }            return instance;        }    }  

 2.工廠設計模式

       程式在介面和子類之間加入了一個過渡端,通過此過渡端可以動態取得實現了共同介面的子類執行個體化對象。

      範例程式碼如下: Java代碼   interface Animal { // 定義一個動物的介面       public void say(); // 說話方法   }       class Cat implements Animal { // 定義子類Cat       @Override       public void say() { // 覆寫say()方法           System.out.println("我是貓咪,喵嗚。");        }    }       class Dog implements Animal { // 定義子類Dog          @Override       public void say() { // 覆寫say()方法           System.out.println("我是小狗,汪汪。");        }    }       class Factory { // 定義工廠類       public static Animal getInstance(String className) {            Animal a = null; // 定義介面對象           if ("Cat".equals(className)) { // 判斷是哪個子類的標記               a = new Cat(); // 通過Cat子類執行個體化介面           }            if ("Dog".equals(className)) { // 判斷是哪個子類的標記               a = new Dog(); // 通過Dog子類執行個體化介面           }            return a;        }    }       public class FactoryDemo {           public static void main(String[] args) {            Animal a = null; // 定義介面對象           a = Factory.getInstance(args[0]); // 通過工廠擷取執行個體           if (a != null) { // 判斷對象是否為空白               a.say(); // 調用方法            }        }    }  

interface Animal { // 定義一個動物的介面public void say(); // 說話方法}class Cat implements Animal { // 定義子類Cat@Overridepublic void say() { // 覆寫say()方法System.out.println("我是貓咪,喵嗚。");}}class Dog implements Animal { // 定義子類Dog@Overridepublic void say() { // 覆寫say()方法System.out.println("我是小狗,汪汪。");}}class Factory { // 定義工廠類public static Animal getInstance(String className) {Animal a = null; // 定義介面對象if ("Cat".equals(className)) { // 判斷是哪個子類的標記a = new Cat(); // 通過Cat子類執行個體化介面}if ("Dog".equals(className)) { // 判斷是哪個子類的標記a = new Dog(); // 通過Dog子類執行個體化介面}return a;}}public class FactoryDemo {public static void main(String[] args) {Animal a = null; // 定義介面對象a = Factory.getInstance(args[0]); // 通過工廠擷取執行個體if (a != null) { // 判斷對象是否為空白a.say(); // 調用方法}}}

 

 3.代理設計模式

       指由一個代理主題來操作真實主題,真實主題執行具體的業務操作,而代理主題負責其他相關業務的處理。比如生活中的通過代理訪問網路,客戶通過網路代理程式串連網路(具體業務),由Proxy 伺服器完成使用者權限和訪問限制等與上網相關的其他動作(相關業務)。

      範例程式碼如下: Java代碼   interface Network { // 定義Network介面       public void browse(); // 定義瀏覽的抽象方法   }       class Real implements Network { // 真實的上網操作       public void browse() { // 覆寫抽象方法           System.out.println("上網瀏覽資訊。");        }    }       class Proxy implements Network { // 代理上網       private Network network;           public Proxy(Network network) {// 設定代理的真實操作           this.network = network; // 設定代理的子類       }           public void check() { // 身分識別驗證操作           System.out.println("檢查使用者是否合法。");        }           

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.