Java-36 设计模式
设计模式(理解)
(1)面试对象的常见设计原则
单一职责原则
开闭原则
里氏替换原则
依赖注入原则
接口分离原则
迪米特原则
(2)设计模式概述和分类
A:经验的总结
B:三类:
创建型
结构型
行为型
(3)改进的设计模式
A:简单工厂模式

public class Dog extends Animal { @Override public void eat() { System.out.println("狗吃肉"); } }

public class Dog extends Animal { @Override public void eat() { System.out.println("狗吃肉"); } }

public class Cat 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("Cat".equals(type)){ return new Cat(); }else if("Dog".equals(type)){ return new Dog(); }else { return null; } } }

/* 简单工厂模式(静态工厂方法模式) 优点 客户端不需要在负责对象的创建,从而明确了各个类的职责 缺点 这个静态工厂类负责所有对象的创建,如果有新的对象增加, 或者某些对象的创建方式不同,就需要不断的修改工厂类,不利于后期的维护 */ public class AnimalDemo { public static void main(String[] args) { //学习工厂模式之前 // Dog dog = new Dog(); // dog.eat(); // Cat cat = new Cat(); // cat.eat(); //现在有一个动物工厂去造一些动物 // Dog dog = AnimalFactory.createDog(); // Cat cat = AnimalFactory.createCat(); // dog.eat(); // cat.eat(); //改进版 Animal dog = AnimalFactory.createAnimal("Dog"); dog.eat(); Animal cat = AnimalFactory.createAnimal("Cat"); cat.eat(); Animal pig = AnimalFactory.createAnimal("Pig"); if(pig!=null){ pig.eat(); }else { System.out.println("该工厂目前还不提供制造该对象的功能!"); } } }
B:工厂方法模式

public interface Factory { public abstract Animal creatCreateAnimal(); }

public abstract class Animal { public abstract void eat(); }

public class Cat extends Animal { @Override public void eat() { System.out.println("猫吃鱼"); } }

public class CatFactory implements Factory{ @Override public Animal creatCreateAnimal() { return new Cat(); } }

public class Dog extends Animal { @Override public void eat() { System.out.println("狗吃肉"); } }

public class DogFactory implements Factory { @Override public Animal creatCreateAnimal() { return new Dog(); } }

/* 工厂方法模式 优点 客户端不需要在负责对象的创建,从而明确了各个类的职责, 如果有新的对象增加,只需要增加一个具体的类和具体的工厂类即可, 不影响已有的代码,后期维护容易,增强了系统的扩展性 缺点 需要额外的编写代码,增加了工作量 */ public class AnimalDemo { public static void main(String[] args) { //我想要只狗 DogFactory dogFactory = new DogFactory(); Animal animal = dogFactory.creatCreateAnimal(); animal.eat(); //我想要只猫 CatFactory catFactory = new CatFactory(); Animal animal1 = catFactory.creatCreateAnimal(); animal1.eat(); } }
C:单例模式(掌握)
a:饿汉式:类一加载,就创建对象

public class Student { //构造方法私有 private Student(){ } //自己创建一个对象 //静态方法只能访问静态的成员变量,加static //为了不让外界随意改动这个变量,我们将他私有化 private static Student s = new Student(); //提供一个方法给外界进行访问这个对象 public static Student getStudent(){ return s; } }

/* 单例模式:保证在类中只存在一个对象 如何保证类在内存中只有一个对象? 1、构造方法私有 2、在类的成员变量的位置上创建一个对象 3、提供一个公共的方法给外界获取到该对象 通过观察发现,我们随着类一加载,对象就创建好了 ,这样的方法是,单例模式中的饿汉式。 */ public class StudengDemo { public static void main(String[] args) { Student s1 = Student.getStudent(); Student s2 = Student.getStudent(); System.out.println(s1==s2);//true System.out.println(s1); System.out.println(s2); } }
b:懒汉式:用的时候,就去创建对象
Teacher类:

public class Teacher { private Teacher(){ } private static Teacher t = null; //假设现在有3个线程,t1,t2,t3 //静态同步方法 public synchronized static Teacher getTeacher(){ //t1,t2,t3 if(t == null){ t = new Teacher(); } return t; } }

/* 如何保证类在内存中只有一个对象? 1、构造方法私有 2、在类的成员变量的位置上创建一个对象 3、提供一个公共的方法给外界获取到该对象 饿汉式:类一加载,就创建对象 懒汉式:用的时候,再去创建对象。 1、懒加载(延迟加载) 2、容易产生线程安全问题 1)是否存在多线程环境 是 2)是否存在共享变量 是 3)是否存在多条语句操作共享变量 */ public class TeacherDemo { public static void main(String[] args) { Teacher t1 = Teacher.getTeacher(); Teacher t2 = Teacher.getTeacher(); System.out.println(t1==t2);//true System.out.println(t1); System.out.println(t2); } }
(注意:面试的时候写懒汉式,开发的时候写饿汉式,因为饿汉式不存在线程安全问题)
(4)Runtime
JDK提供的一个单例模式应用的类
还可以通过dos命令。
A:概述
每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。
可以通过 getRuntime 方法获取当前运行时。 应用程序不能创建自己的 Runtime 类实例
import java.io.IOException; public class RuntimeDemo1 { public static void main(String[] args) throws IOException { Runtime r = Runtime.getRuntime(); r.exec("calc"); //调出计算器 // r.exec("shutdown -s -t 10000"); // r.exec("shutdown -a"); } } //源码 /* public class Runtime { private Runtime() {} private static Runtime currentRuntime = new Runtime(); public static Runtime getRuntime() { return currentRuntime; } } */