[关闭]
@BIGBALLON 2015-06-29T12:33:16.000000Z 字数 10619 阅读 1767

设计模式

说实话对于面向对象的研究并不是很深,理解自然也不是很深刻。
软件结构体系这门课还是计算机系的老师来给我们带的课,却是本学期所有老师里面教得最好的课了,如果看不懂UML图和不了解设计模式,真的很难说自己是软件工程专业毕业的学生,惭愧。
下面开始小结下设计模式吧。


设计模式的定义

设计模式是一套被反复使用、多人知晓的、经过分类编目的、代码设计经验的总结
每一个设计模式描述一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心。这样,你就能一次一次地使用该方案而不必做重复劳动。

设计模式四人帮

设计模式的四要素

设计原则

设计原则的作用

七个设计原则

设计原则名称 设计原则简介
单一职责原则 (SRP) 类的职责要单一,不能将太多的职责放在一个类中。
开放封闭原则 (OCP) 软件实体对扩展是开放的,对修改是关闭的
里氏代换原则 (LSP) 一个可以接受基类对象的地方必然可以接受一个子类对象
依赖倒置原则 (DIP) 要针对抽象层编程,而不要针对具体类编程
接口隔离原则 (ISP) 使用多个专用接口来取代一个统一的接口
合成复用原则 (CRP) 尽量多使用组合和聚合关联关系,尽量少使用甚至不使用继承关系
迪米特法则 (LoD) 一个软件实体对其他实体的引用越少越好

设计模式的分类

创建型模式

创建型模式,就是创建对象的模式,抽象了实例化的过程。它帮助一个系统独立于如何创建、组合和表示它的那些对象。

创建型模式关注的是对象的创建,将创建对象的过程进行了抽象和封装,作为客户程序仅仅需要去使用对象,而不再惯性创建对象过程中的逻辑。
当你想使用new关键字的时候,就可以考虑创建型模式

结构型模式

结构型模式(Structural Pattern)描述如何将类或者对象结合在一起形成更大的结构,就像搭积木,可以通过简单积木的组合形成复杂的、功能更为强大的结构。

结构型模式

行为型模式分为类行为型模式和对象行为型模式两种:

常用的设计模式

工厂方法模式(5星)

定义

Factory Method Pattern: Define an interface for creating an object, but let subclasses decide which class to instantiate.Factory Method lets a class defer instantiation to subclasses

工厂方法模式又称为工厂模式,也叫做虚拟构造器(Virtual Constructor)模式或者多态工厂(polymorphic Factory)模式

简单工厂模式

简单工厂模式,可以根据参数的不同类型返回不同类的实例

简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有相同的父类

简单工厂模式的核心:一个工厂,多种厂品

由于只有一个工厂,不用产生具体的实例,可以直接用静态方法,因此简单工厂模式也经常称为静态工厂模式(static factory pattern)

实例

  1. public interface Human{
  2. public void getColor(); //每个人种的皮肤都有相应的颜色
  3. public void talk(); //人类会说话
  4. }
  5. public class YellowHuman implements Human{
  6. public void getColor(){
  7. System.out.println("黄色");
  8. }
  9. public void talk(){
  10. System.out.println("黄种人,双字节");
  11. }
  12. }
  13. public class HumanFactory{
  14. public static <T extends Human> T createHuman( Class<T> c ){
  15. Human human = null;
  16. try{
  17. human = (Human)CLass.forName(c.getName()).newInstance();
  18. }catch( Exception e ){
  19. System.out.println("人种生成错误");
  20. }
  21. return (T)human;
  22. }
  23. }
  24. public NvWa{
  25. public static void main( String[] args ){
  26. System.out.println("---造出第一个人---");
  27. Human whiteHuman = HumanFactory.createHuman( whiteHuman.class );
  28. whiteHuman.getColor();
  29. whiteHuman.talk();
  30. System.out.println("---造出第二个人---");
  31. Human blackHuman = HumanFactory.createHuman( blackHuman.class );
  32. blackHuman.getColor();
  33. blackHuman.talk();
  34. System.out.println("---造出第三个人---");
  35. Human yellowHuman = HumanFactory.createHuman( yellowHuman.class );
  36. yellowHuman.getColor();
  37. yellowHuman.talk();
  38. }
  39. }

模式角色

实例

  1. public abstract class AbstractHumanFactory{
  2. public abstract <T extends Human> T createHuman( Class<T> c );
  3. }
  4. public class HumanFactory extends AbstractHumanFactory{
  5. public <T extends Human> T createHuman( Class<T> c ){
  6. Human human = null;
  7. try{
  8. human = (Human)Class.forName( c.getName() ).newInstance();
  9. }catch ( Exception e ){
  10. System.out.println("人种生成错误");
  11. }
  12. return (T)human;
  13. }
  14. }
  15. public class NvWa{
  16. public static void main( String[] args ) {
  17. //声明阴阳八卦炉
  18. AbstractHumanFactory YinYangLu = new HumanFactory();
  19. Human whiteHuman = YinYangLu.createHuman(whiteHuman.class);
  20. whiteHuman.getColor();
  21. whiteHuman.talk();
  22. }
  23. }

抽象工厂模式(5星)

定义

Abstract Factory Pattern: Provide an interface for creating families of ralated or dependent objects without specifying their concrete classes

抽象工厂模式:提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。

抽象工厂模式又称为工具(Kit)模式
抽象工厂模式中的具体工厂不只是创建的一种产品,它负责创建一族产品

当一个工厂等级结构可以创建出分别属于不同产品等级结构的一个产品族中的所有对象时,抽象工厂模式比工厂方法模式更为简单、更有效率

实例

  1. //抽象产品:人种接口的实现
  2. public interface Human{
  3. public void getColor(); //每个人种的皮肤都有相应的颜色
  4. public void talk(); //人类会说话
  5. public void getSex();
  6. }
  7. //子抽象产品:各色人种的接口的实现
  8. public abstract class AbstractYellowHuman implements Human{
  9. public void getColor(){
  10. System.out.println("黄色");
  11. }
  12. public void talk(){
  13. System.out.println("黄种人,双字节");
  14. }
  15. }
  16. //具体产品类
  17. public class FemaleYellowHuman extends AbstractYellowHuman{
  18. public void getSex(){
  19. System.out.println("黄种女性");
  20. }
  21. }
  22. public class MaleYellowHuman extends AbstractYellowHuman{
  23. public void getSex(){
  24. System.out.println("黄种男性");
  25. }
  26. }
  27. //抽象工厂
  28. public interface HumanFactory{
  29. public Human createYellowHuman();
  30. public Human createWhiteHuman();
  31. public Human createBlackHuman();
  32. }
  33. //具体工厂
  34. public class FemaleFactory implements HumanFactory{
  35. public createYellowHuman(){
  36. return new FemaleYellowHuman();
  37. }
  38. ...
  39. ...
  40. ...
  41. }
  42. public class MaleFactory implements HumanFactory{
  43. public createYellowHuman(){
  44. return new FemaleYellowHuman();
  45. }
  46. ...
  47. ...
  48. ...
  49. }
  50. public class NvWa{
  51. public static void main(String[] args) {
  52. HumanFactory maleHumanFactory = new MaleFactory();
  53. HumanFactory femaleHumanFactory = new FemaleFactory();
  54. Human maleYellowHuam = maleHumanFactory.createYellowHuman();
  55. maleYellowHuam.getColor();
  56. maleYellowHuam.talk();
  57. maleYellowHuam.getSex();
  58. }
  59. }

外观模式(5星)

定义

Facade Pattern: Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

外观模式(Facade Pattern):外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

外观模式又翻译为门面模式,它是一种对象结构型模式

模式角色

模式结构

实例

  1. //子系统角色类
  2. public class AirConditioner{
  3. public void on(){
  4. ...
  5. }
  6. public void off(){
  7. ...
  8. }
  9. }
  10. public class Television{
  11. ...
  12. }
  13. public class Fan{
  14. ...
  15. }
  16. public class Light{
  17. private String position;
  18. public Light( String position ){
  19. this.position = position;
  20. }
  21. public void on(){
  22. }
  23. public void off(){
  24. }
  25. }
  26. //外观角色类
  27. public class GenralSwitchFacade{
  28. private Light lights[] = new Light[4];
  29. private Fan fan;
  30. private AirConditioner ac;
  31. private Television tv;
  32. public GenralSwitchFacade(){
  33. lights[0] = new Light( "左前");
  34. lights[1] = new Light( "右前");
  35. lights[2] = new Light( "左后");
  36. lights[3] = new Light( "右后");
  37. fan = new Fan();
  38. ac = new AirConditioner();
  39. tv = new Television();
  40. }
  41. public void on(){
  42. lights[0].on();
  43. lights[1].on();
  44. lights[2].on();
  45. lights[3].on();
  46. fan.on();
  47. ac.on();
  48. tv.on();
  49. }
  50. public void off(){
  51. lights[0].off();
  52. lights[1].off();
  53. lights[2].off();
  54. lights[3].off();
  55. fan.off();
  56. ac.off();
  57. tv.off();
  58. }
  59. }

迭代器模式(5星)

定义

Iterator Pattern: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

迭代器模式(Iterator Pattern) :提供一种方法来访问聚合对象,而不用暴露这个对象的内部表示,其别名为游标(Cursor)。迭代器模式是一种对象行为型模式。

一个聚合对象,如一个列表(list)或者一个集合(set),应该提供一种方法来让别人可以访问它的元素,而又不需要暴露它的内部结构。

针对不同的需要,可能还要以不同的方式遍历整个聚合对象,但是我们并不希望在聚合对象的抽象层接口中充斥着各种不同遍历的操作。
怎样遍历一个聚合对象,又不需要了解聚合对象的内部结构,还能够提供多种不同的遍历方式,这就是迭代器模式所要解决的问题。
怎样遍历一个聚合对象,又不需要了解聚合对象的内部结构,还能够提供多种不同的遍历方式,这就是迭代器模式所要解决的问题。

模式角色

模式结构

实例

  1. //抽象迭代器
  2. public interface TVIterator{
  3. void setChannel( int i );
  4. void next();
  5. void previous();
  6. boolean isLast();
  7. Objects currentChannel();
  8. boolean isFirst();
  9. }
  10. //抽象聚合类
  11. public interface Television{
  12. TVIterator createIterator();
  13. }
  14. //具体聚合类和具体迭代类
  15. public class TCLTelevision implements Television{
  16. private Objects[] obj = {"湖南卫视","北京卫视","上海卫视","湖北卫视","黑龙江卫视"};
  17. public TVIterator createIterator(){
  18. return new TCLIterator();
  19. }
  20. class TCLIterator implements TVIterator{
  21. private int currentIndex = 0;
  22. public void next(){
  23. if( currentIndex < obj.length ){
  24. currentIndex++;
  25. }
  26. }
  27. public void previous(){
  28. if( currentIndex > 0 ){
  29. currentIndex--;
  30. }
  31. }
  32. public void setChannel( int i ){
  33. currentIndex = i;
  34. }
  35. public Objects currentChannel(){
  36. return obj[currentIndex];
  37. }
  38. public boolean isFirst(){
  39. ...
  40. }
  41. public boolean isLast(){
  42. ...
  43. }
  44. }
  45. }

观察者模式(5星)

定义

Observer Pattern: Define a one-to-many dependency between objects so tha when one object changes state, all its dependents are notified and updated automatically

观察者模式:定义对象间的一种一对多的依赖关系,使得每当一个对象状态发生改变时,其相关依赖对象皆得到通知并被自动更新。观察者模式又叫做发布-视图(Model/View)模式,源-监听器(Source/Listener)模式或从属者(Dependents)模式。观察者模式是一种对象行为型模式

模式角色

模式结构

实例

  1. import java.util.*;
  2. //抽象目标
  3. public abstract class MySubject{
  4. protected ArrayList observers = new ArrayList();
  5. public void attach( MyObserver observer ){
  6. observers.add( observer );
  7. }
  8. public void detach( MyObserver observer ){
  9. observers.remove( observer );
  10. }
  11. public abstract void cry(); //抽象通知方法
  12. }
  13. //抽象观察者
  14. public interface MyObserver{
  15. void response();
  16. }
  17. //具体目标
  18. public class Cat extends MySubject{
  19. public void cry(){
  20. System.ou.println( "猫叫!" );
  21. for (Object obs:observers) {
  22. ((MyObserver)obs).response();
  23. }
  24. }
  25. }
  26. //具体观察者
  27. public class Mouse implements MyObserver{
  28. public void response(){
  29. System.ou.println( "老鼠跑!" );
  30. }
  31. }
  32. public class Dog implements MyObserver{
  33. public void response(){
  34. System.ou.println( "狗叫!" );
  35. }
  36. }
  37. //客户端
  38. public class Client{
  39. public static void main(String[] args) {
  40. MySubject subject = new Cat();
  41. MyObserver obs1, obs2, obs3;
  42. obs1 = new Mouse();
  43. obs2 = new Mouse();
  44. obs3 = new Dog();
  45. subject.attach(obs1);
  46. subject.attach(obs2);
  47. subject.attach(obs3);
  48. subject.cry();
  49. }
  50. }
  1. //抽象目标类
  2. public interface IHanFeiZi{
  3. public void haveBreakfast();
  4. public void haveFun();
  5. }
  6. //抽象观察者类
  7. public interface ILiSi{
  8. public update( String context );
  9. }
  10. //具体目标类
  11. public class HanFeiZi implements IHanFeiZi{
  12. private ILiSi liSi = new LiSi();
  13. public void haveBreakfast(){
  14. System.out.println( "韩非子:开始吃饭了...")
  15. this.liSi.update( "韩非子在吃饭");
  16. }
  17. public void haveFun(){
  18. System.out.println( "韩非子:开始娱乐了...")
  19. this.liSi.update( "韩非子在娱乐");
  20. }
  21. }
  22. //具体观察者类
  23. public class LiSi implements ILiSi{
  24. public void update( String str ){
  25. System.out.println( "李斯:观察到韩非子活动,开始向老板汇报了...");
  26. this.reportToQiShiHuang( str );
  27. System.out.println( "李斯:汇报完毕...");
  28. }
  29. private void reportToQiShiHuang( String reportContext ){
  30. System.out.println( "李斯:报告,秦老板!韩非子有活动了..."
  31. + reportContext );
  32. }
  33. }
  34. //客户端
  35. public class Client{
  36. public static void main(String[] args) {
  37. HanFeiZi hanFeiZi = new HanFeiZi();
  38. hanFeiZi.haveBreakfast();
  39. hanFeiZi.haveFun();
  40. }
  41. }

一些杂项

  1. 设计模式的原理是面向接口编程
  2. 软件危机:泛指在计算机软件的开发和维护过程中所遇到的一系列严重问题
    • 软件开发过程中如何满足日益增长的用户需求
    • 如何维护数量不断膨胀的已有软件
  3. 软件复用:是指利用现有的软件资源来开发新应用系统的过程。包括三个相关的基本过程:
    • 软件对象的开发
    • 软件对象的理解
    • 软件对象的复用
  4. 软件体系结构的内容:风格设计模式框架
  5. 软件开发模型

    • 瀑布模型:文档驱动的、阶段间具有顺序性依赖性、推迟实现、质量保证
    • 渐增模型:由一组有计划的、循环渐增的、不断改进的过程版本组成。
      各个中间版本可以并行开发。

    • 演化模型(原型模型):通过系统各个可执行的中间版本以渐增的形式来开发系统的,但是客户需求可以分步逐渐了解,不用在初始时就确定。

    • 螺旋模型:螺旋模型沿着螺线旋转,在四个象限上分别表达四个方面的活动,制定计划风险分析实施工程客户评估
      螺旋模型将瀑布模型与快速原形模型结合起来,并且加入两种模型均忽略了的风险分析。
      优点:对可选方案和约束条件的强调有利于已有软件的重用,也有助于把软件质量作为软件开发的一个重要目标;
      维护和开发之间并没有本质区别。
      缺点:风险驱动的
      主要适用于内部开发的大规模软件项目

    • 智能模型:把瀑布模型和专家系统结合在一起。
    • 喷泉模型: 开发活动之间的无间隙性和循环迭代性。
  6. UML的三要素:
    • 表示法--图形
    • 过程----适用于面向对象的软件开发过程
    • 工具----(Ratinal Rose等)
  7. UML结构
  8. UML的的四种常用关系五种视图
    • 静态视图: 类图、对象图
    • 实现视图: 构建图、部署图
    • 交互视图: 顺序图、协作图
    • 行为视图: 状态图、活动图
    • 用例视图: 用例图
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注