抽象工厂模式:
public abstract class Animal {
public abstract void 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 interface Factory { public abstract Animal createAnimal(); }
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) { // 需求:我要买只狗 Factory f = new DogFactory(); Animal a = f.createAnimal(); a.eat(); System.out.println("-------"); //需求:我要买只猫 f = new CatFactory(); a = f.createAnimal(); a.eat(); } }
单例模式:
饿汉式:
public class Student { // 构造私有 private Student() { } // 自己造一个 // 静态方法只能访问静态成员变量,加静态 // 为了不让外界直接访问修改这个值,加private private static Student s = new Student(); // 提供公共的访问方式 // 为了保证外界能够直接使用该方法,加静态 public static Student getStudent() { return s; } }
懒汉式:
public class Teacher { private Teacher() { } private static Teacher t = null; public synchronized static Teacher getTeacher() { // t1,t2,t3 if (t == null) { //t1,t2,t3 t = new Teacher(); } return t; } }
* 单例模式:
* 饿汉式:类一加载就创建对象
* 懒汉式:用的时候,才去创建对象
*
* 面试题:单例模式的思想是什么?请写一个代码体现。
*
* 开发:饿汉式(是不会出问题的单例模式)
* 面试:懒汉式(可能会出问题的单例模式)
* 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; } }
public class RuntimeDemo { public static void main(String[] args) throws IOException { Runtime r = Runtime.getRuntime(); // r.exec("winmine"); // r.exec("notepad"); // r.exec("calc"); // r.exec("shutdown -s -t 10000"); r.exec("shutdown -a"); } }
模板设计模式:
public abstract class GetTime { // 需求:请给我计算出一段代码的运行时间 public long getTime() { long start = System.currentTimeMillis(); // 再给我测试一个代码:集合操作的,多线程操作,常用API操作的等等... code(); long end = System.currentTimeMillis(); return end - start; } public abstract void code(); }
public class ForDemo extends GetTime { @Override public void code() { for (int x = 0; x < 100000; x++) { System.out.println(x); } } }
装饰设计模式:
public interface Phone { public abstract void call(); }
public class IPhone implements Phone { @Override public void call() { System.out.println("手机可以打电话了"); } }
public abstract class PhoneDecorate implements Phone { private Phone p; public PhoneDecorate(Phone p) { this.p = p; } @Override public void call() { this.p.call(); } }
public class RingPhoneDecorate extends PhoneDecorate { public RingPhoneDecorate(Phone p) { super(p); } @Override public void call() { System.out.println("手机可以听彩铃"); super.call(); } }
public class MusicPhoneDecorate extends PhoneDecorate { public MusicPhoneDecorate(Phone p) { super(p); } @Override public void call() { super.call(); System.out.println("手机可以听音乐"); } }
public class PhoneDemo { public static void main(String[] args) { Phone p = new IPhone(); p.call(); System.out.println("------------"); // 需求:我想在接电话前,听彩铃 PhoneDecorate pd = new RingPhoneDecorate(p); pd.call(); System.out.println("------------"); // 需求:我想在接电话后,听音乐 pd = new MusicPhoneDecorate(p); pd.call(); System.out.println("------------"); // 需求:我要想手机在接前听彩铃,接后听音乐 // 自己提供装饰类,在打电话前听彩铃,打电话后听音乐 pd = new RingPhoneDecorate(new MusicPhoneDecorate(p)); pd.call(); System.out.println("----------"); // 想想我们在IO流中的使用 // InputStream is = System.in; // InputStreamReader isr = new InputStreamReader(is); // BufferedReader br = new BufferedReader(isr); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter((new OutputStreamWriter( System.out))); Scanner sc = new Scanner(System.in); } }