Java笔记(25):设计模式概述
生活随笔
收集整理的這篇文章主要介紹了
Java笔记(25):设计模式概述
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、設計模式的概述和分類
設計模式:
經驗的總結。
A:創建型 創建對象
B:結構型 對象的組成
C:行為型 對象的功能
創建型模式:
1)簡單工廠模式
2)工廠方法模式
3)設計模式
2、簡單工廠模式概述和使用
1 package cn.itcast_01; 2 3 public abstract class Animal { 4 public abstract void eat(); 5 }
1 package cn.itcast_01; 2 3 public class Cat extends Animal { 4 @Override 5 public void eat() { 6 System.out.println("貓吃魚"); 7 } 8 }
1 package cn.itcast_01; 2 3 public class Dog extends Animal { 4 @Override 5 public void eat() { 6 System.out.println("狗吃肉"); 7 } 8 }
1 package cn.itcast_01; 2 3 public class AnimalFactory { 4 5 private AnimalFactory() { 6 } 7 8 // public static Dog createDog() { 9 // return new Dog(); 10 // } 11 // 12 // public static Cat createCat() { 13 // return new Cat(); 14 // } 15 16 public static Animal createAnimal(String type) { 17 if ("dog".equals(type)) { 18 return new Dog(); 19 } else if ("cat".equals(type)) { 20 return new Cat(); 21 } else { 22 return null; 23 } 24 } 25 }
1 package cn.itcast_01; 2 3 public class AnimalDemo { 4 public static void main(String[] args) { 5 // 具體類調用 6 Dog d = new Dog(); 7 d.eat(); 8 Cat c = new Cat(); 9 c.eat(); 10 System.out.println("------------"); 11 12 // 工廠有了后,通過工廠給造 13 // Dog dd = AnimalFactory.createDog(); 14 // Cat cc = AnimalFactory.createCat(); 15 // dd.eat(); 16 // cc.eat(); 17 // System.out.println("------------"); 18 19 // 工廠改進后 20 Animal a = AnimalFactory.createAnimal("dog"); 21 a.eat(); 22 a = AnimalFactory.createAnimal("cat"); 23 a.eat(); 24 25 // NullPointerException 26 a = AnimalFactory.createAnimal("pig"); 27 if (a != null) { 28 a.eat(); 29 } else { 30 System.out.println("對不起,暫時不提供這種動物"); 31 } 32 } 33 }
3、工廠方法模式的概述和使用
1 package cn.itcast_02; 2 3 public abstract class Animal { 4 public abstract void eat(); 5 }
1 package cn.itcast_02; 2 3 public interface Factory { 4 public abstract Animal createAnimal(); 5 }
1 package cn.itcast_02; 2 3 public class AnimalDemo { 4 public static void main(String[] args) { 5 // 需求:我要買只狗 6 Factory f = new DogFactory(); 7 Animal a = f.createAnimal(); 8 a.eat(); 9 System.out.println("-------"); 10 11 //需求:我要買只貓 12 f = new CatFactory(); 13 a = f.createAnimal(); 14 a.eat(); 15 } 16 }
1 package cn.itcast_02; 2 3 public class Cat extends Animal { 4 5 @Override 6 public void eat() { 7 System.out.println("貓吃魚"); 8 } 9 10 }
1 package cn.itcast_02; 2 3 public class CatFactory implements Factory { 4 5 @Override 6 public Animal createAnimal() { 7 return new Cat(); 8 } 9 10 }
1 package cn.itcast_02; 2 3 public class Dog extends Animal { 4 5 @Override 6 public void eat() { 7 System.out.println("狗吃肉"); 8 } 9 10 }
1 package cn.itcast_02; 2 3 public class DogFactory implements Factory { 4 5 @Override 6 public Animal createAnimal() { 7 return new Dog(); 8 } 9 10 }
4、單例模式之餓漢式
1 package cn.itcast_03; 2 3 public class Student { 4 // 構造私有 5 private Student() { 6 } 7 8 // 自己造一個 9 // 靜態方法只能訪問靜態成員變量,加靜態 10 // 為了不讓外界直接訪問修改這個值,加private 11 private static Student s = new Student(); 12 13 // 提供公共的訪問方式 14 // 為了保證外界能夠直接使用該方法,加靜態 15 public static Student getStudent() { 16 return s; 17 } 18 }
1 package cn.itcast_03; 2 3 /* 4 * 單例模式:保證類在內存中只有一個對象。 5 * 6 * 如何保證類在內存中只有一個對象呢? 7 * A:把構造方法私有 8 * B:在成員位置自己創建一個對象 9 * C:通過一個公共的方法提供訪問 10 */ 11 public class StudentDemo { 12 public static void main(String[] args) { 13 // Student s1 = new Student(); 14 // Student s2 = new Student(); 15 // System.out.println(s1 == s2); // false 16 17 // 通過單例如何得到對象呢? 18 19 // Student.s = null; 20 21 Student s1 = Student.getStudent(); 22 Student s2 = Student.getStudent(); 23 System.out.println(s1 == s2); 24 25 System.out.println(s1); // null,cn.itcast_03.Student@175078b 26 System.out.println(s2);// null,cn.itcast_03.Student@175078b 27 } 28 }
5、單例模式之懶漢式
1 package cn.itcast_03; 2 3 /* 4 * 單例模式: 5 * 餓漢式:類一加載就創建對象 6 * 懶漢式:用的時候,才去創建對象 7 * 8 * 面試題:單例模式的思想是什么?請寫一個代碼體現。 9 * 10 * 開發:餓漢式(是不會出問題的單例模式) 11 * 面試:懶漢式(可能會出問題的單例模式) 12 * A:懶加載(延遲加載) 13 * B:線程安全問題 14 * a:是否多線程環境 是 15 * b:是否有共享數據 是 16 * c:是否有多條語句操作共享數據 是 17 */ 18 public class Teacher { 19 private Teacher() { 20 } 21 22 private static Teacher t = null; 23 24 public synchronized static Teacher getTeacher() { 25 // t1,t2,t3 26 if (t == null) { 27 //t1,t2,t3 28 t = new Teacher(); 29 } 30 return t; 31 } 32 }
1 package cn.itcast_03; 2 3 public class TeacherDemo { 4 public static void main(String[] args) { 5 Teacher t1 = Teacher.getTeacher(); 6 Teacher t2 = Teacher.getTeacher(); 7 System.out.println(t1 == t2); 8 System.out.println(t1); // cn.itcast_03.Teacher@175078b 9 System.out.println(t2);// cn.itcast_03.Teacher@175078b 10 } 11 }
6、單例模式的Java代碼體現Runtime類
1 package cn.itcast_03; 2 3 import java.io.IOException; 4 5 /* 6 * Runtime:每個 Java 應用程序都有一個 Runtime 類實例,使應用程序能夠與其運行的環境相連接。 7 * exec(String command) 8 */ 9 public class RuntimeDemo { 10 public static void main(String[] args) throws IOException { 11 Runtime r = Runtime.getRuntime(); 12 // r.exec("winmine"); 13 // r.exec("notepad"); 14 // r.exec("calc"); 15 // r.exec("shutdown -s -t 10000"); 16 r.exec("shutdown -a"); 17 } 18 } 19 20 /* 21 * class Runtime { 22 * private Runtime() {} 23 * private static Runtime currentRuntime = new Runtime(); 24 * public static Runtime getRuntime() { 25 * return currentRuntime; 26 * } 27 * } 28 */
?
轉載于:https://www.cnblogs.com/lz2lhy/p/7021100.html
總結
以上是生活随笔為你收集整理的Java笔记(25):设计模式概述的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017年6月16号课堂笔记
- 下一篇: 求一个含义深刻的个性签名!