Java設計模式
設計模式的分類
創建型模式 對象的創建結構型模式 對象的組成
行爲型模式 對象的行爲
簡單工廠方法模式(靜態工廠方法模式)
特點:
構造方法私有化,外界不能直接創建它的對象
提供靜態功能,每一種靜態都會產生所需要的對象...
缺點:不利於後期維護
如果添加新的對象,需要創建它的類還需要在靜態工廠類提供一些功能!
public abstract class Animal {
public abstract void eat();
}
定義兩個動物具體類,繼承動物抽象類,重寫裏面的eat()方法public class Cat extends Animal {
@Override
public void eat() {
System.out.println("貓吃魚....");
}
}
public class Dog extends Animal {
@Override
public void eat() {
System.out.println("狗吃骨頭...");
}
}
再創建一個動物工廠類,提供貓和狗這兩種動物public class AnimalFactory {
private AnimalFactory() {
}
//提供貓和狗這兩種動物
public static Animal createAnimal(String type) {
if("dog".equals(type)) {
return new Dog();
}else if("cat".equals(type)){
return new Cat();
}else {
return null;
}
}
}
測試類:
public class AnimalDemo {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
Cat cat = new Cat();
cat.eat();
System.out.println("---------------");
Animal a = AnimalFactory.createAnimal("cat");
a.eat();
a = AnimalFactory.createAnimal("dog");
a.eat();
a = AnimalFactory.createAnimal("erdan") ;
if(a != null) {
a.eat();
}else {
System.out.println("目前工廠類沒有提供該動物...");
}
}
}
工廠模式
特點:
需要提供一個抽象類,以及每個動物的具體類和接口(工廠接口)
該接口中的抽象方法的返回值是該抽象類
針對每個具體動物都提供一些對應的工廠類--->實現該接口--->返回值就是需要具體的動物對象
弊端:代碼量大.
public abstract class Animal {
public abstract void eat() ;
}
定義兩個動物具體類,繼承動物抽象類,重寫裏面的eat()方法public class Cat extends Animal {
public interface Factory {
public abstract Animal createAnimal();
}
@Overridepublic void eat() {System.out.println("貓吃魚....");}}定義工廠類接口
public interface Factory {
public abstract Animal createAnimal();
}
分別定義cat和dog的工廠類,實現工廠接口
public class CatFactory implements Factory {
@Override
public Animal createAnimal() {
return new Cat();
}
}
public class DogFactory implements Factory {
@Override
public Animal createAnimal() {
return new Dog();
}
}
測試類public class AnimalDemo {
public static void main(String[] args) {
//貓工廠生產貓
DogFactory d = new DogFactory();
Animal a = d.createAnimal();
a.eat();
System.out.println("------------");
//狗工廠生產狗
CatFactory c = new CatFactory();
a = c.createAnimal();
a.eat();
}
}
單例模式
定義:在內存中始終只有一個對象
餓漢式
特點:當前某一個一加載,就會創建一個對象
需要將該類的無參構造私有化
在成員變量創建該類的實例
需要提供公共的訪問方法(實際開發中使用這種方式)
懶漢式
特點:並不是加載某個類就直接創建對象,而是需要的時候在創建對象
1)懶加載(延遲加載)
2)線程的安全問題
線程安全問題檢驗標準:
a:是否是多線程環境
b:是否有共享數據
c:是否有多條語句對共享數據進行操作!
使用同步代碼塊或同步方法(synchronized)進行解決
public class Student {
//無參構造私有化,目的是爲了讓外界不創建對象
private Student() {
}
//在成員變量的位置創建該類的實例
//靜態只能訪問靜態,所以需要加上靜態修飾
//爲了不讓外界修改當前這個實例,所以加入private修飾
private static Student s = new Student();
public static Student getStudent() {
return s;
}
public class StudentDemo {
public static void main(String[] args) {
//利用靜態的公共訪問方法創建對象,防止外界修改當前Student類的中實例
Student s1 = Student.getStudent() ;
Student s2 = Student.getStudent();
System.out.println(s1==s2);
System.out.println(s1);
System.out.println(s2);
}
}
懶漢式:public class Teacher {
private Teacher() {}
//在成員變量位置聲明變量
private static Teacher t = null ; //共享數據
//提供公共的方法訪問
public synchronized static Teacher getTeacher(){ //Teacher.class鎖對象
//判斷 噹噹前該對象沒有更多引用的時候,才創建對象
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);//true
System.out.println(t1);
System.out.println(t2);
}
}
就想問你能否想到懶漢式中多線程的安全問題...
如果是開發中,使用單例模式,那麼使用的 --->餓漢式(不會出現問題的單例模式)
如果是面試中,使用單例模式,----->懶漢式(想問你能否想到懶漢式中多線程的安全問題以及解決辦法)