• 设计模式


    抽象工厂模式:

    1 public abstract class Animal {
    2     public abstract void eat();
    3 }
    1 public class Cat extends Animal {
    2 
    3     @Override
    4     public void eat() {
    5         System.out.println("猫吃鱼");
    6     }
    7 
    8 }
    1 public class Dog extends Animal {
    2 
    3     @Override
    4     public void eat() {
    5         System.out.println("狗吃肉");
    6     }
    7 
    8 }
    1 public interface Factory {
    2     public abstract Animal createAnimal();
    3 }
    1 public class CatFactory implements Factory {
    2 
    3     @Override
    4     public Animal createAnimal() {
    5         return new Cat();
    6     }
    7 
    8 }
    1 public class DogFactory implements Factory {
    2 
    3     @Override
    4     public Animal createAnimal() {
    5         return new Dog();
    6     }
    7 
    8 }
     1 public class AnimalDemo {
     2     public static void main(String[] args) {
     3         // 需求:我要买只狗
     4         Factory f = new DogFactory();
     5         Animal a = f.createAnimal();
     6         a.eat();
     7         System.out.println("-------");
     8         
     9         //需求:我要买只猫
    10         f = new CatFactory();
    11         a = f.createAnimal();
    12         a.eat();
    13     }
    14 }

    单例模式:

    饿汉式:

     1 public class Student {
     2     // 构造私有
     3     private Student() {
     4     }
     5 
     6     // 自己造一个
     7     // 静态方法只能访问静态成员变量,加静态
     8     // 为了不让外界直接访问修改这个值,加private
     9     private static Student s = new Student();
    10 
    11     // 提供公共的访问方式
    12     // 为了保证外界能够直接使用该方法,加静态
    13     public static Student getStudent() {
    14         return s;
    15     }
    16 }

    懒汉式:

     1 public class Teacher {
     2     private Teacher() {
     3     }
     4 
     5     private static Teacher t = null;
     6 
     7     public synchronized static Teacher getTeacher() {
     8         // t1,t2,t3
     9         if (t == null) {
    10             //t1,t2,t3
    11             t = new Teacher();
    12         }
    13         return t;
    14     }
    15 }

    * 单例模式:
    * 饿汉式:类一加载就创建对象
    * 懒汉式:用的时候,才去创建对象
    *
    * 面试题:单例模式的思想是什么?请写一个代码体现。
    *
    * 开发:饿汉式(是不会出问题的单例模式)
    * 面试:懒汉式(可能会出问题的单例模式)
    * A:懒加载(延迟加载)
    * B:线程安全问题
    * a:是否多线程环境 是
    * b:是否有共享数据 是
    * c:是否有多条语句操作共享数据 是

    * Runtime:每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。
    * exec(String command)

    /*
    * class Runtime {
    * private Runtime() {}
    * private static Runtime currentRuntime = new Runtime();
    * public static Runtime getRuntime() {
    * return currentRuntime;
    * }
    * }
    */

     1 public class RuntimeDemo {
     2     public static void main(String[] args) throws IOException {
     3         Runtime r = Runtime.getRuntime();
     4 //        r.exec("winmine");
     5         // r.exec("notepad");
     6         // r.exec("calc");
     7 //        r.exec("shutdown -s -t 10000");
     8         r.exec("shutdown -a");
     9     }
    10 }

    模板设计模式:

     1 public abstract class GetTime {
     2     // 需求:请给我计算出一段代码的运行时间
     3     public long getTime() {
     4         long start = System.currentTimeMillis();
     5 
     6         // 再给我测试一个代码:集合操作的,多线程操作,常用API操作的等等...
     7         code();
     8 
     9         long end = System.currentTimeMillis();
    10 
    11         return end - start;
    12     }
    13 
    14     public abstract void code();
    15 }
    1 public class ForDemo extends GetTime {
    2 
    3     @Override
    4     public void code() {
    5         for (int x = 0; x < 100000; x++) {
    6             System.out.println(x);
    7         }
    8     }
    9 }

    装饰设计模式:

    1 public interface Phone {
    2     public abstract void call();
    3 }
    1 public class IPhone implements Phone {
    2 
    3     @Override
    4     public void call() {
    5         System.out.println("手机可以打电话了");
    6     }
    7 
    8 }
     1 public abstract class PhoneDecorate implements Phone {
     2 
     3     private Phone p;
     4 
     5     public PhoneDecorate(Phone p) {
     6         this.p = p;
     7     }
     8 
     9     @Override
    10     public void call() {
    11         this.p.call();
    12     }
    13 }
     1 public class RingPhoneDecorate extends PhoneDecorate {
     2 
     3     public RingPhoneDecorate(Phone p) {
     4         super(p);
     5     }
     6 
     7     @Override
     8     public void call() {
     9         System.out.println("手机可以听彩铃");
    10         super.call();
    11     }
    12 }
     1 public class MusicPhoneDecorate extends PhoneDecorate {
     2 
     3     public MusicPhoneDecorate(Phone p) {
     4         super(p);
     5     }
     6 
     7     @Override
     8     public void call() {
     9         super.call();
    10         System.out.println("手机可以听音乐");
    11     }
    12 }
     1 public class PhoneDemo {
     2     public static void main(String[] args) {
     3         Phone p = new IPhone();
     4         p.call();
     5         System.out.println("------------");
     6 
     7         // 需求:我想在接电话前,听彩铃
     8         PhoneDecorate pd = new RingPhoneDecorate(p);
     9         pd.call();
    10         System.out.println("------------");
    11 
    12         // 需求:我想在接电话后,听音乐
    13         pd = new MusicPhoneDecorate(p);
    14         pd.call();
    15         System.out.println("------------");
    16 
    17         // 需求:我要想手机在接前听彩铃,接后听音乐
    18         // 自己提供装饰类,在打电话前听彩铃,打电话后听音乐
    19         pd = new RingPhoneDecorate(new MusicPhoneDecorate(p));
    20         pd.call();
    21         System.out.println("----------");
    22         // 想想我们在IO流中的使用
    23         // InputStream is = System.in;
    24         // InputStreamReader isr = new InputStreamReader(is);
    25         // BufferedReader br = new BufferedReader(isr);
    26         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    27         BufferedWriter bw = new BufferedWriter((new OutputStreamWriter(
    28                 System.out)));
    29 
    30         Scanner sc = new Scanner(System.in);
    31     }
    32 }
  • 相关阅读:
    好记性不如烂笔杆android学习笔记<十四> EditText 画行,解决光标压线问题
    好记性不如烂笔杆android学习笔记<十五> GridView简单用法
    分享个好玩的算法游戏
    ubuntu环境下lnmp环境搭建(3)之Php
    数据可视化之美之疾病潜在关联
    ubuntu环境下lnmp环境搭建(2)之Nginx
    乐观锁和悲观锁
    表单无刷新上传图片
    ubuntu环境下lnmp环境搭建(1)之Mysql
    祭旗篇关于提高技术团队技术氛围的一些尝试
  • 原文地址:https://www.cnblogs.com/samuraihuang/p/10052768.html
Copyright © 2020-2023  润新知