1.单例设计模式
1.1饿汉式
不管有没有人获取对象,自己创建了唯一的对象
public class Student {
//系统默认提供空参构造
//需求:这个类只允许创建一个对象
//1.不能让外界使用构造方法
private Student(){}
//2.类中自己创建唯一的一个对象
//final修饰之后把它叫做是常量
private static final Student s = new Student();
//3.通过方法让别人获取唯一的对象
public static Student getStudent(){
return s;
}
}
1.2 懒汉式
- 当有人第一次获取对象时,才真正的创建对象
- 可以存在线程安全问题,所有加同步方法
public class Student {
//懒汉式写法
//1.把构造方法私有化
private Student(){}
//2.内部创建对象
private static Student s;
//3.通过获取方法给别人返回唯一的对象
public static synchronized Student getStudent(){
//什么时候获取什么时候就创建
if(s == null){
s = new Student();
}
return s;
}
}
2.多例设计模式
- 某个类只允许创建固定个数的对象
public class Student {
//只允许创建3个对象
//把构造方法私有化
private Student(){}
//创建集合 保存3个学生对象
private static ArrayList<Student> list = new ArrayList<>();
//创建学生对象
static{
//创建三个对象放在集合中
for (int i = 0; i < 3; i++) {
list.add(new Student());
}
}
//公共方法
public static Student getStudent(){
//随机返回一个对象
Random r = new Random();
int i = r.nextInt(list.size());
Student s = list.get(i);
return s;
}
}
3.动态代理
- 动态代理的作用是使用【代理对象】对某个类原来的方法功能进行改变
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
public class MyCollections {
//参数代表的是被代理对象[list]
//返回值代表的是代理对象[list2]
public static List<String> method(List<String> list){
//动态代理
/*
三个参数:
1.被代理对象的类加载器
2.被代理对象的所实现的接口
3.操作方式 匿名内部类
*/
List list2 = (List)Proxy.newProxyInstance(list.getClass().getClassLoader(), list.getClass().getInterfaces(), new InvocationHandler() {
/*
当使用代理对象[list2]调用任何方法时,系统都会执行invoke
list2.get(1);
list2.add("凤姐");
三个参数:
1.被代理对象[list2]
2.被调用的方法[get add]
3.方法的参数值[1 "凤姐"]
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//目的: 不允许调用增删改方法
if(method.getName().equals("add") ||method.getName().equals("remove") ||method.getName().equals("set")){
//产生异常
throw new RuntimeException("不可以调用" + method.getName() + "方法!");
}
//其他方法 正常调用
Object value = method.invoke(list, args);
return value;
}
});
return list2;
}
}
import java.util.ArrayList;
import java.util.List;
public class Demo01 {
public static void main(String[] args) {
//创建集合
ArrayList<String> list = new ArrayList<>();
//添加方法
list.add("柳岩");
list.add("石原里美");
list.add("杏园");
//list被代理对象 list2代理对象
//调用方法
List<String> list2 = MyCollections.method(list);
//使用代理对象调用方法
//添加方法
//list2.add("郭富城");
//获取方法
String ss = list2.get(1);
System.out.println(ss);
//获取长度
int size = list2.size();
System.out.println(size);
}
}
4.工厂设计模式
- 把创建对象的权利交过给工厂。让工厂创建各种类型的对象
public class Demo01 {
public static void main(String[] args) {
/*
传统写法
BaoMa b = new BaoMa();
BenChi bc = new BenChi();
WuLin wl = new WuLin();
*/
//工厂模式写法
Car car = Factory.getCar("宝马");
System.out.println(car);
Car bc = Factory.getCar("奔驰");
System.out.println(bc);
Car wl = Factory.getCar("五菱");
System.out.println(wl);
}
}
public class Factory {
//工厂类
//工厂类的作用就是帮我们创建对象
//造车工厂返回车类型
public static Car getCar(String name){
//name代表的是车种类
if(name.equals("奔驰")){
return new BenChi();
}
if(name.equals("宝马")){
return new BaoMa();
}
if(name.equals("五菱")){
return new WuLin();
}
return null;
}
}