国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

設計模式-(單例模式)

2019-11-06 07:18:15
字體:
來源:轉載
供稿:網友
單例設計模式概述?單例模式就是要確保類在內存中只有一個對象,該實例必須自動創建,并且對外提供。優點?在系統內存中只存在一個對象,因此可以節約系統資源,對于一些需要頻繁創建和銷毀的對象單例模式無疑可以提高系統的性能。缺點?沒有抽象層,因此擴展很難。?職責過重,在一定程序上違背了單一職責1.餓漢式代碼://學生類public class Student {    // 構造私有    PRivate Student() {    }    private static Student s = new Student();    public static Student getStudent() {        return s;    }}//測試類public class StudentDemo {    public static void main(String[] args) {        Student s1 = Student.getStudent();        Student s2 = Student.getStudent();        System.out.println(s1 == s2);        System.out.println(s1); // null,cn.itcast_03.Student@175078b        System.out.println(s2);// null,cn.itcast_03.Student@175078b    }}2.懶漢式代碼//老師類public class Teacher {    private Teacher() {    }    private static Teacher t = null;    public synchronized static Teacher getTeacher() {        if (t == null) {            t = new Teacher();        }        return t;    }}//測試類public class TeacherDemo {    public static void main(String[] args) {        Teacher t1 = Teacher.getTeacher();        Teacher t2 = Teacher.getTeacher();        System.out.println(t1 == t2);        System.out.println(t1); // cn.itcast_03.Teacher@175078b        System.out.println(t2);// cn.itcast_03.Teacher@175078b    }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丰县| 灌南县| 商水县| 岳阳市| 彩票| 内丘县| 咸宁市| 浪卡子县| 海安县| 连南| 龙岩市| 清河县| 永川市| 宁夏| 湖南省| 石景山区| 娱乐| 广灵县| 江口县| 和田市| 镇坪县| 宁陕县| 福州市| 长子县| 获嘉县| 读书| 大姚县| 青浦区| 工布江达县| 合作市| 民县| 纳雍县| 江油市| 定襄县| 绥中县| 沁水县| 濮阳县| 田阳县| 稷山县| 龙江县| 隆回县|