設(shè)計(jì)模式
是一種編碼套路
單例模式
一個(gè)類只能創(chuàng)建一個(gè)實(shí)例
餓漢式
直接創(chuàng)建唯一實(shí)例
package com.by.entity;
?
/**
* 單例模式-餓漢式
*/
public class ClassA {
? ?//static: 1. newClassA可以訪問返回 2. 靜態(tài)屬性內(nèi)存中只會(huì)存在一個(gè)
? ?//private: 防止外界直接訪問屬性
? ?private static ClassA ca = new ClassA();
?
? ?//外界獲取唯一實(shí)例的渠道
? ?//static:使外界直接通過類名訪問
? ?public static ClassA newClassA(){
? ? ? ?return ca;
? }
?
? ?//構(gòu)造私有化-防止外界調(diào)用構(gòu)造創(chuàng)建對(duì)象
? ?private ClassA(){
?
? }
}
缺點(diǎn): 有可能浪費(fèi)空間
懶漢式
在獲取實(shí)例是創(chuàng)建唯一對(duì)象
package com.by.entity;
?
/**
* 單例模式-懶漢式
*/
public class ClassB {
? ?//static: 1. newClassA可以訪問返回 2. 靜態(tài)屬性內(nèi)存中只會(huì)存在一個(gè)
? ?//private: 防止外界直接訪問屬性
? ?private static ClassB cb = null;
?
? ?//外界獲取唯一實(shí)例的渠道
? ?//static:使外界直接通過類名訪問
? ?//synchronized: 同步方法 預(yù)防線程安全問題
? ?public static synchronized ClassB newClassB(){
? ? ? ?
? ? ? ?if (cb == null) {//當(dāng)?shù)谝淮潍@取時(shí)再進(jìn)行實(shí)例化
? ? ? ? ? ?cb = new ClassB();
? ? ? }
? ? ? ?return cb;
? }
?
? ?//構(gòu)造私有化-防止外界調(diào)用構(gòu)造創(chuàng)建對(duì)象
? ?private ClassB(){
?
? }
?
}
缺點(diǎn): 線程效率慢
懶漢式-進(jìn)階版
在懶漢式的基礎(chǔ)上,利用同步代碼塊結(jié)合二次校驗(yàn)提高執(zhí)行效率
package com.by.entity;
?
/**
* 單例模式-懶漢式進(jìn)階版
*/
public class ClassC {
? ?//static: 1. newClassA可以訪問返回 2. 靜態(tài)屬性內(nèi)存中只會(huì)存在一個(gè)
? ?//private: 防止外界直接訪問屬性
? ?private static ClassC cc = null;
?
? ?//外界獲取唯一實(shí)例的渠道
? ?//static:使外界直接通過類名訪問
? ?public static ClassC newClassC(){
? ? ? ?if (cc == null) {//二次校驗(yàn): 決定是否需要開啟互斥鎖 t1:true t2:true
? ? ? ? ? ?synchronized (ClassC.class) {//臨界資源對(duì)象: 當(dāng)前類的類對(duì)象
? ? ? ? ? ? ? ?//對(duì)屬性進(jìn)行實(shí)例化
? ? ? ? ? ? ? ?if (cc == null) {//當(dāng)?shù)谝淮潍@取時(shí)再進(jìn)行實(shí)例化
? ? ? ? ? ? ? ? ? ?cc = new ClassC();
? ? ? ? ? ? ? }
? ? ? ? ? }
? ? ? }
? ? ? ?return cc;
? }
?
? ?//構(gòu)造私有化-防止外界調(diào)用構(gòu)造創(chuàng)建對(duì)象
? ?private ClassC(){
?
? }
?
}package com.by.entity;
?
/**
* 單例模式-懶漢式進(jìn)階版
*/
public class ClassC {
? ?//static: 1. newClassA可以訪問返回 2. 靜態(tài)屬性內(nèi)存中只會(huì)存在一個(gè)
? ?//private: 防止外界直接訪問屬性
? ?private static ClassC cc = null;
?
? ?//外界獲取唯一實(shí)例的渠道
? ?//static:使外界直接通過類名訪問
? ?public static ClassC newClassC(){
? ? ? ?if (cc == null) {//二次校驗(yàn): 決定是否需要開啟互斥鎖 t1:true t2:true
? ? ? ? ? ?synchronized (ClassC.class) {//臨界資源對(duì)象: 當(dāng)前類的類對(duì)象
? ? ? ? ? ? ? ?//對(duì)屬性進(jìn)行實(shí)例化
? ? ? ? ? ? ? ?if (cc == null) {//當(dāng)?shù)谝淮潍@取時(shí)再進(jìn)行實(shí)例化
? ? ? ? ? ? ? ? ? ?cc = new ClassC();
? ? ? ? ? ? ? }
? ? ? ? ? }
? ? ? }
? ? ? ?return cc;
? }
?
? ?//構(gòu)造私有化-防止外界調(diào)用構(gòu)造創(chuàng)建對(duì)象
? ?private ClassC(){
?
? }
?
}
工廠模式
-
是一種底層技術(shù),通常用于底層框架的編寫
-
思路: 將數(shù)據(jù)的創(chuàng)建和維護(hù)交由工廠完成
案例
-
需求: 構(gòu)建一個(gè)工廠類,獲取學(xué)生對(duì)象
-
提供學(xué)生類
package com.by.entity; ? public class Student { ? ?private String name; ? ?private int age; ? ?private double score; ? ? ?public String getName() { ? ? ? ?return name; ? } ? ? ?public void setName(String name) { ? ? ? ?this.name = name; ? } ? ? ?public int getAge() { ? ? ? ?return age; ? } ? ? ?public void setAge(int age) { ? ? ? ?this.age = age; ? } ? ? ?public double getScore() { ? ? ? ?return score; ? } ? ? ?public void setScore(double score) { ? ? ? ?this.score = score; ? } ? ? ?@Override ? ?public String toString() { ? ? ? ?return "Student{" + ? ? ? ? ? ? ? ?"name='" + name + '\'' + ? ? ? ? ? ? ? ?", age=" + age + ? ? ? ? ? ? ? ?", score=" + score + ? ? ? ? ? ? ? ?'}'; ? } ? ? ?public Student() { ? } ? ? ?public Student(String name, int age, double score) {//string,int,double ? ? ? ?this.name = name; ? ? ? ?this.age = age; ? ? ? ?this.score = score; ? } }
-
書寫配置文件
-
在項(xiàng)目下創(chuàng)建后綴名為
.properties
的配置文件 -
作用: 存放被管理類的全限定名
-
以
鍵=值
的形式存放數(shù)據(jù) -
鍵值不可添加雙引號(hào)、末尾不可添加分號(hào)、中間不可存在多個(gè)符號(hào)(如空格)
-
一行只能有一個(gè)鍵值對(duì)
StudentClassName=com.by.entity.Student
-
-
書寫工廠類文章來源:http://www.zghlxwxcb.cn/news/detail-801679.html
package com.by.util; ? import com.by.entity.Student; ? import java.io.FileInputStream; import java.util.Properties; ? public class MyFactory { ? ?/** ? ? * 獲取學(xué)生對(duì)象 ? ? */ ? ?public static Student getStudent(){ ? ? ? ?Student student = null; ? ? ? ?try ( ? ? ? ? ? ? ? ?//創(chuàng)建輸入流 ? ? ? ? ? ? ? ?FileInputStream fis=new FileInputStream("factory.properties") ? ? ? ? ? ? ? ) { ? ? ? ? ? ?//將配置文件的內(nèi)容加載到集合中 ? ? ? ? ? ?Properties p = new Properties(); ? ? ? ? ? ?p.load(fis); ? ? ? ? ? ?//獲取全限定名 ? ? ? ? ? ?String className = p.getProperty("StudentClassName"); ? ? ? ? ? ?//獲取類對(duì)象 ? ? ? ? ? ?Class c = Class.forName(className); ? ? ? ? ? ?//利用反射構(gòu)建學(xué)生實(shí)例 ? ? ? ? ? ?student = (Student) c.newInstance(); ? ? ? } catch (Exception e) { ? ? ? ? ? ?System.out.println("未知異常!"); ? ? ? ? ? ?e.printStackTrace(); ? ? ? } ? ? ? ?return student; ? ? } } ?
-
測(cè)試文章來源地址http://www.zghlxwxcb.cn/news/detail-801679.html
package com.by.test; ? import com.by.entity.Student; import com.by.util.MyFactory; ? public class Test5 { ? ?public static void main(String[] args) { ? ? ? ?//利用工廠獲取學(xué)生實(shí)例對(duì)象 ? ? ? ?Student s1 = MyFactory.getStudent(); ? ? ? ?Student s2 = MyFactory.getStudent(); ? ? } } ?
到了這里,關(guān)于java基礎(chǔ)之設(shè)計(jì)模式(單例模式,工廠模式)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!