工厂模式
package Pattern;
public class AnimalDemo01 {
public static void main(String[] args) {
// 具体类调用
Dog01 d = new Dog01();
d.eat();
Cat01 c = new Cat01();
c.eat();
System.out.println("------------");
// 工厂有了后,通过工厂给造
// Dog dd = AnimalFactory.createDog();
// Cat cc = AnimalFactory.createCat();
// dd.eat();
// cc.eat();
// System.out.println("------------");
// 工厂改进后
Animal01 a = AnimalFactory01.createAnimal("dog");
a.eat();
a = AnimalFactory01.createAnimal("cat");
a.eat();
// NullPointerException
a = AnimalFactory01.createAnimal("pig");
if (a != null) {
a.eat();
} else {
System.out.println("对不起,暂时不提供这种动物");
}
}
}
package Pattern;
public abstract class Animal01 {
public abstract void eat();
}
package Pattern;
public class Dog01 extends Animal01 {
@Override
public void eat() {
System.out.println("狗吃肉");
}
}
package Pattern;
public class Cat01 extends Animal01{
@Override
public void eat() {
System.out.println("猫吃老鼠");
}
}
package Pattern;
public class AnimalFactory01 {
private AnimalFactory01() {
}
// public static Dog createDog() {
// return new Dog();
// }
//
// public static Cat createCat() {
// return new Cat();
// }
public static Animal01 createAnimal(String type) {
if ("dog".equals(type)) {
return new Dog01();
} else if ("cat".equals(type)) {
return new Cat01();
} else {
return null;
}
}
}
单例模式
* 饿汉式:类一加载就创建对象* 懒汉式:用的时候,才去创建对象
*
* 面试题:单例模式的思想是什么?请写一个代码体现。
*
* 开发:饿汉式(是不会出问题的单例模式)
* 面试:懒汉式(可能会出问题的单例模式)
* A:懒加载(延迟加载)
* B:线程安全问题
* a:是否多线程环境 是
* b:是否有共享数据 是
* c:是否有多条语句操作共享数据 是
饿汉试
package Pattern;
public class Student {
// 构造私有
private Student() {
}
// 自己造一个
// 静态方法只能访问静态成员变量,加静态
// 为了不让外界直接访问修改这个值,加private
private static Student s = new Student();
// 提供公共的访问方式
// 为了保证外界能够直接使用该方法,加静态
public static Student getStudent() {
return s;
}
}
package Pattern;
/*
* 单例模式:保证类在内存中只有一个对象。
*
* 如何保证类在内存中只有一个对象呢?
* 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
}
}
懒汉试
package Pattern;
/*
* 单例模式:
* 饿汉式:类一加载就创建对象
* 懒汉式:用的时候,才去创建对象
*
* 面试题:单例模式的思想是什么?请写一个代码体现。
*
* 开发:饿汉式(是不会出问题的单例模式)
* 面试:懒汉式(可能会出问题的单例模式)
* 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;
}
}
package Pattern;
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
}
}