设计模式
l 设计模式概述
• 什么是设计模式
l 设计模式分类
• 创建型模式
• 行为型模式
• 结构型模式
单例设计模式
l 单例设计思想
• 保证类在内存中只有一个对象
l 如何实现类在内存中只有一个对象呢?
• 构造私有
• 本身提供一个对象
• 通过公共的方法让外界访问
单例设计模式分类
l 饿汉式(开发)
l 懒汉式(面试)
• 线程安全问题
• 懒加载思想(延迟加载)
Runtime类的概述和使用
l Runtime类概述
• 每个Java 应用程序都有一个Runtime类实例,使应用程序能够与其运行的环境相连接。可以通过getRuntime 方法获取当前运行时。
• 应用程序不能创建自己的Runtime 类实例。
l Runtime类使用
• public Process exec(String command)
(1)面试对象的常见设计原则
单一
开闭
里氏
依赖注入
接口
迪米特
(2)设计模式概述和分类
A:经验的总结
B:三类
创建型
结构型
行为型
A:创建型 创建对象
B:结构型 对象的组成
C:行为型 对象的功能
创建型模式:
1:简单工厂模式
2:工厂方法模式
3:设计模式
(3)改进的设计模式
A:简单工厂模式
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 class AnimalFactory {
private AnimalFactory() {
}
// public static Dog createDog() {
// return new Dog();
// }
//
// public static Cat createCat() {
// return new Cat();
// }
public static Animal createAnimal(String type) {
if ("dog".equals(type)) {
return new Dog();
} else if ("cat".equals(type)) {
return new Cat();
} else {
return null;
}
}
}
public class AnimalDemo {
public static void main(String[] args) {
// 具体类调用
Dog d = new Dog();
d.eat();
Cat c = new Cat();
c.eat();
System.out.println("------------");
// 工厂有了后,通过工厂给造
// Dog dd = AnimalFactory.createDog();
// Cat cc = AnimalFactory.createCat();
// dd.eat();
// cc.eat();
// System.out.println("------------");
// 工厂改进后
Animal a = AnimalFactory.createAnimal("dog");
a.eat();
a = AnimalFactory.createAnimal("cat");
a.eat();
// NullPointerException
a = AnimalFactory.createAnimal("pig");
if (a != null) {
a.eat();
} else {
System.out.println("对不起,暂时不提供这种动物");
}
}
}
B:工厂方法模式
public abstract class Animal {
public abstract void eat();
}
public interface Factory {
public abstract Animal createAnimal();
}
public class Cat extends Animal {
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
public class CatFactory implements Factory {
@Override
public Animal createAnimal() {
return new Cat();
}
}
public class Dog extends Animal {
@Override
public void eat() {
System.out.println("狗吃肉");
}
}
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();
}
}
C:单例模式(掌握)
a:饿汉式
b:懒汉式
public class Student {
// 构造私有
private Student() {
}
// 自己造一个
// 静态方法只能访问静态成员变量,加静态
// 为了不让外界直接访问修改这个值,加private
private static Student s = new Student();
// 提供公共的访问方式
// 为了保证外界能够直接使用该方法,加静态
public static Student getStudent() {
return s;
}
}
/*
* 单例模式:保证类在内存中只有一个对象。
*
* 如何保证类在内存中只有一个对象呢?
* A:把构造方法私有
* B:在成员位置自己创建一个对象
* C:通过一个公共的方法提供访问
*/
public class StudentDemo {
public static void main(String[] args) {
// Student s1 = new Student();
// Student s2 = new Student();
// System.out.println(s1 == s2); // false
// 通过单例如何得到对象呢?
// Student.s = null;
Student s1 = Student.getStudent();
Student s2 = Student.getStudent();
System.out.println(s1 == s2);
System.out.println(s1); // null,cn.itcast_03.Student@175078b
System.out.println(s2);// null,cn.itcast_03.Student@175078b
}
}
/*
* 单例模式:
* 饿汉式:类一加载就创建对象
* 懒汉式:用的时候,才去创建对象
*
* 面试题:单例模式的思想是什么?请写一个代码体现。
*
* 开发:饿汉式(是不会出问题的单例模式)
* 面试:懒汉式(可能会出问题的单例模式)
* A:懒加载(延迟加载)
* B:线程安全问题
* a:是否多线程环境 是
* b:是否有共享数据 是
* c:是否有多条语句操作共享数据 是
*/
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;
}
}
public class TeacherDemo {
public static void main(String[] args) {
Teacher t1 = Teacher.getTeacher();
Teacher t2 = Teacher.getTeacher();
System.out.println(t1 == t2);
System.out.println(t1); // cn.itcast_03.Teacher@175078b
System.out.println(t2);// cn.itcast_03.Teacher@175078b
}
}
(4)Runtime
JDK提供的一个单例模式应用的类。
还可以调用dos命令。
import java.io.IOException;
/*
* Runtime:每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。
* exec(String command)
*/
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");
}
}
/*
* class Runtime {
* private Runtime() {}
* private static Runtime currentRuntime = new Runtime();
* public static Runtime getRuntime() {
* return currentRuntime;
* }
* }
*/