1.单例模式
保证只产生一个对象
分为懒汉式和恶汉式
主要运用场景:XMl文件的解析
public class Danli {
//恶汉式单例 会造成内存消耗
//private static Danli danli=new Danli();
//懒汉式单例
private static Danli danli=null;
private static Lock lock=new ReentrantLock();
private Danli() {
}
// public synchronized static Object name() {
// if (danli!=null) {
// danli=new Danli();
// }
// return danli;
// }
public static Object getIntance() {
lock.lock();
try {
if (danli==null) {
danli=new Danli();
}
return danli;
} finally {
lock.unlock();
}
}
2.工厂模式
通过工厂类产生不同的实例对象
实例工厂:创建工厂类
静态工厂:将工厂类中的工厂方法静态化,这样就可以直接通过直接调用
public class People {
private int id;
public People(int id) {
super();
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String NameQian() {
return "普通人";
}
}
public class Student extends People{
public Student(int id) {
super(id);
}
@Override
public String NameQian() {
return "学生";
}
}
public class Teacher extends People{
public Teacher(int id) {
super(id);
// TODO Auto-generated constructor stub
}
@Override
public String NameQian() {
return "教师";
}
}
public class Foctory {
public static People createPeople(people) {
if(people。equals("sutdent")){
return new Student(123);
}else if(people。equals("teacher")){
return new Teacher(123);
}
}
}
public static void main(String[] args) {
Foctory foctory=new Foctory();
People people=null;
people=foctory.createPeople(student);
People people1=null;
people1=foctory.createPeople(teacher);
}
3.策略模式
如果工厂模式是针对类的,将不同类通过工厂类进行创建,那么策略模式则是通过自定义策略接口实现对方法的不同的调用
这里要提到一个设计原则---开闭原则
开闭原则:
对于扩展是开放的(Open for extension)。这意味着模块的行为是可以扩展的。当应用的需求改变时,我们可以对模块进行扩展,使其具有满足那些改变的新行为。也就是说,我们可以改变模块的功能。
对于修改是关闭的(Closed for modification)。对模块行为进行扩展时,不必改动模块的源代码或者二进制代码。
策略模式结构中包含了三种角色:
1.策略 2.上下文 3.具体策略
策略:策略是一个接口,该接口定义若干算法标识,即定义了若干个抽象方法。
上下文(context):上下文是依赖接口的类(是面向策略二设计的类),即上下文中包含了策略变量。上下文中提供一个方法,该方法委托策略变量调用具体策略所实现的策略接口中的方法。
具体策略:具体策略是实现策略接口的类。具体策略实现策略接口所定义的抽象方法。
public interface Strategy {
void getAverge();
}
public class CelueA implements Strategy{
@Override
public void getAverge() {
System.out.println("A方法");
}
}
public class CelueB implements Strategy{
@Override
public void getAverge() {
System.out.println("B方法");
}
}
public class StrategyContext {
//策略变量的引用
private Strategy strategy;
public StrategyContext() {
super();
}
public void setStrategy(Strategy strategy){
this.strategy = strategy;
}
public void getAverge(){
if(strategy != null){
//调用引用策略的方法
strategy.getAverge();
}else {
System.out.println("没有求平均值的策略!");
}
}
}
public class Test {
public static void main(String[] args) {
CelueA celueA=new CelueA();
StrategyContext sContext=new StrategyContext();
sContext.setStrategy(celueA);
sContext.getAverge();
}
}
4.代理模式
静态代理:静态代理在使用时,需要定义接口或者父类,被代理对象与代理对象一起实现相同的接口或者是继承相同父类.
动态代理
/**
* 接口实现
* 目标对象
*/
public class UserDao implements IUserDao {
public void save() {
System.out.println("----已经保存数据!----");
}
}
public interface IUserDao {
void save();
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 创建动态代理对象
* 动态代理不需要实现接口,但是需要指定接口类型
*/
public class ProxyFactory{
//维护一个目标对象
private Object target;
public ProxyFactory(Object target){
this.target=target;
}
//给目标对象生成代理对象
public Object getProxyInstance(){
return Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("开始事务2");
//执行目标对象方法
Object returnValue = method.invoke(target, args);
System.out.println("提交事务2");
return returnValue;
}
}
);
}
}
public class App {
public static void main(String[] args) {
// 目标对象
IUserDao target = new UserDao();
System.out.println(target.getClass());
// 给目标对象,创建代理对象
IUserDao proxy = (IUserDao) new ProxyFactory(target).getProxyInstance();
System.out.println(proxy.getClass());
// 执行方法
proxy.save();
}
}