zoukankan      html  css  js  c++  java
  • 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  */
    如欢如殇 授以青春鲜活肢体奔忙 如思如忘 驱以老朽深沉灵魂冥想 始自情热激荡 从未敢终于世事炎凉 无能执手相望 无法去尝试结发同床 无力至心死身僵 一息坚强 ------ 我一直没有放弃,如果你也能看到 修身 修禅
  • 相关阅读:
    关于Python对文件字节流数据的处理
    python中的random模块
    软件开发项目验收标准
    pdf文档转图片
    批量处理图片转base64编码
    批量处理图片转为透明色
    python2.7实现本地启一个http服务作为数据转发接收器或模拟接口响应数据源
    系统正常运行指标参考
    Jenkins创建一个自由风格的项目
    KatalonRecorder系列(一):基本使用+XPath元素定位
  • 原文地址:https://www.cnblogs.com/lz2lhy/p/7021100.html
Copyright © 2011-2022 走看看