日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

Java笔记(25):设计模式概述

發(fā)布時(shí)間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java笔记(25):设计模式概述 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、設(shè)計(jì)模式的概述和分類

設(shè)計(jì)模式:
經(jīng)驗(yàn)的總結(jié)。

A:創(chuàng)建型 創(chuàng)建對(duì)象
B:結(jié)構(gòu)型 對(duì)象的組成
C:行為型 對(duì)象的功能

創(chuàng)建型模式:
1)簡(jiǎn)單工廠模式
2)工廠方法模式
3)設(shè)計(jì)模式

2、簡(jiǎn)單工廠模式概述和使用

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         // 具體類調(diào)用
 6         Dog d = new Dog();
 7         d.eat();
 8         Cat c = new Cat();
 9         c.eat();
10         System.out.println("------------");
11 
12         // 工廠有了后,通過(guò)工廠給造
13         // Dog dd = AnimalFactory.createDog();
14         // Cat cc = AnimalFactory.createCat();
15         // dd.eat();
16         // cc.eat();
17         // System.out.println("------------");
18 
19         // 工廠改進(jìn)后
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("對(duì)不起,暫時(shí)不提供這種動(dòng)物");
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     // 構(gòu)造私有
 5     private Student() {
 6     }
 7 
 8     // 自己造一個(gè)
 9     // 靜態(tài)方法只能訪問(wèn)靜態(tài)成員變量,加靜態(tài)
10     // 為了不讓外界直接訪問(wèn)修改這個(gè)值,加private
11     private static Student s = new Student();
12 
13     // 提供公共的訪問(wèn)方式
14     // 為了保證外界能夠直接使用該方法,加靜態(tài)
15     public static Student getStudent() {
16         return s;
17     }
18 }
 1 package cn.itcast_03;
 2 
 3 /*
 4  * 單例模式:保證類在內(nèi)存中只有一個(gè)對(duì)象。
 5  * 
 6  * 如何保證類在內(nèi)存中只有一個(gè)對(duì)象呢?
 7  *         A:把構(gòu)造方法私有
 8  *         B:在成員位置自己創(chuàng)建一個(gè)對(duì)象
 9  *         C:通過(guò)一個(gè)公共的方法提供訪問(wèn)
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         // 通過(guò)單例如何得到對(duì)象呢?
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  *         餓漢式:類一加載就創(chuàng)建對(duì)象
 6  *         懶漢式:用的時(shí)候,才去創(chuàng)建對(duì)象
 7  * 
 8  * 面試題:單例模式的思想是什么?請(qǐng)寫一個(gè)代碼體現(xiàn)。
 9  * 
10  *         開發(fā):餓漢式(是不會(huì)出問(wèn)題的單例模式)
11  *         面試:懶漢式(可能會(huì)出問(wèn)題的單例模式)
12  *             A:懶加載(延遲加載)    
13  *             B:線程安全問(wèn)題
14  *                 a:是否多線程環(huán)境    是
15  *                 b:是否有共享數(shù)據(jù)    是
16  *                 c:是否有多條語(yǔ)句操作共享數(shù)據(jù)     是
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代碼體現(xiàn)Runtime類

 1 package cn.itcast_03;
 2 
 3 import java.io.IOException;
 4 
 5 /*
 6  * Runtime:每個(gè) Java 應(yīng)用程序都有一個(gè) Runtime 類實(shí)例,使應(yīng)用程序能夠與其運(yùn)行的環(huán)境相連接。
 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  */

?

轉(zhuǎn)載于:https://www.cnblogs.com/lz2lhy/p/7021100.html

總結(jié)

以上是生活随笔為你收集整理的Java笔记(25):设计模式概述的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。