设计模式(Ch03)

          ## 学习目标
          理解设计模式的概念 理解面向对象的设计原则 掌握简单工厂模式的应用 掌握工厂方法模式的应用 掌握代理模式的应用
          一、设计模式的概念
      1.设计模式简介
          1.软件设计中的“三十六计”
          2.是人们在长期的软件开发中的经验总结
          3.是对某些特定问题的经过实践检验的特定解决方法
          4.被广泛运用在Java框架技术中
      
     2.设计模式的优点 设计模式是可复用的面向对象软件的基础 可以更加简单方便地复用成功的设计和体系结构 帮助开发者做出有利于系统复用的选择,避免损害系统复用性的设计 使其他开发者更加容易理解其设计思路,便于团队交流
      3.设计模式分类
                  1.GoF(Gang of Four,四人组)设计模式
          简单的工厂模式
          ```public class NewsServiceImpl implements NewsService { 	private    NewsDao dao = new NewsDaoImpl(); } ```
          ```public class NewsServiceImpl implements NewsService { 	private    NewsDao dao; 	public void setDao(NewsDao dao) {
   		this.dao = dao; 	}    } ```
          ```public static NewsDao getInstance(String key) {
          switch (key) {
              case "mysql":
                  return new NewsDaoMySqlImpl();
              case "oracle":
                  return new NewsDaoOracleImpl();
              case "redis":
                  return new NewsDaoRedisImpl();
              default:
                  throw new RuntimeException("无效的数据库类型:" + key
                                  + " ,DAO获取失败");
          }    }
          ```
          ```public <T> T getInstance(String clz) { 		Class<T> class1; 		try {
      			class1 = (Class<T>) Class.forName(clz); 			return    class1.newInstance(); 		} catch (ClassNotFoundException e) {
      			e.printStackTrace(); 		} catch (InstantiationException e) {
      			e.printStackTrace(); 		} catch (IllegalAccessException e) {
      			e.printStackTrace(); 		} 		return null;
      
      	} ```工厂方法模式
          1.对简单工厂模式的进一步抽象化
   2.工厂方法模式的主要角色如下 抽象产品(Product) 抽象工厂(Abstract Factory) 具体产品(Concrete Product) 具体工厂(Concrete Factory)
      
          五,代理模式 单一职责原则的体现 包含如下角色 抽象主题(Subject) 真实主题(Real Subject) 代理(Proxy)    实现方式总体上分为静态代理和动态代理
   静态代理由开发者针对抽象主题编写相关的代理类实现,编译之后生成代理类的class文件
   动态代理是在运行时动态生成的,在运行时动态生成代理类字节码
          ```ublic class DaoProxy implements QuestionDao {
      
      	private QuestionDao target;
      
      	public DaoProxy(QuestionDao target) { 		this.target = target; 	}
      	@Override 	public List<Question> getAll(int subject) { 		return    target.getAll(subject); 	} 	@Override 	public int insert(Question
   qt)    { 		return 0; 	}
          } ```
          ```QuestionDao questionDao= new DaoProxy(new QuestionDaoImpl());
      		questionDao.getAll(1); ```
          六,JDK动态代理
          ```package com.dao.proxy;
          import java.lang.reflect.InvocationHandler; import    java.lang.reflect.Method;
          public class DaoInvocationHandler<T> implements InvocationHandler {
      	private T target;
      
      	public void setTarget(T target) { 		this.target = target; 	}
      
      	@Override 	public Object invoke(Object proxy, Method method,    Object[] args) throws Throwable { 		bfore(); 		Object result =   
   method.invoke(target, args); 		after(); 		return "调用情况:" + result;
   	}
      
      	public void bfore() { 		System.out.println("方法执行之前"); 	}
      
      	public void after() { 		System.out.println("方法执行之后"); 	}
          }
          ```
          ```public class DaoInvocationHandlerFactory {
      
      	public static <T> T create(T obj) { 		DaoInvocationHandler<T>    handler = new DaoInvocationHandler<>(); 		handler.setTarget(obj);
      
      		return (T) Proxy.newProxyInstance(obj.getClass().getClassLoader(),   
   obj.getClass().getInterfaces(), handler); 	} } ```
          ```public static void main(String[] args) { 		QuestionDao qDao=    DaoInvocationHandlerFactory.create(new QuestionDaoImpl());
      		System.out.println( qDao.getName()); ```~~~~  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值