1.2 《硬啃设计模式》 第2章 学习设计模式需掌握的UML知识
要看懂 设计模式 ,你需要懂 类图 (Class Diagram ),也需要懂一点对象图(Object Diagram),下面介绍一些 UML 的必要知识,以便你学习设计模式。 属性、操作 下图简单介绍类的属性和操作。 类有属性(attributes)、操作(operations),属性和操作统称特
要看懂设计模式,你需要懂类图(Class Diagram),也需要懂一点对象图(Object Diagram),下面介绍一些UML的必要知识,以便你学习设计模式。属性、操作
下图简单介绍类的属性和操作。
类有属性(attributes)、操作(operations),属性和操作统称特性(features)。

下图进一步详细介绍类图细节,包括:属性和操作的作用域、属性类型、参数类型、方法返回值类型等。

接口、枚举

抽象类
继承关系
类B继承类A,如下图:
抽象类被继承,如下图:
实现关系
实现是指某接口被实现,而不是某类被实例化。
实现如下图:
依赖关系
先看图:
B依赖A,表示如果A的接口发生变化,B也需要相应地改变。
依赖的常见情况有:
1. B调用了A的方法。
2. B的方法中,使用了A为参数。
3. B的方法返回值类型为A。
引用关系
先看图:
Class1有箭头指向Class2,表示Class1中含有Class2的引用。
而上图进一步明确了具体的引用方式,就是:Class1中的私有变量m_Class2的类型是Class2。
你可能会问:某类保持另外一个类的引用,一般都会调用另外一个类的方法,那么它们的关系不应该是依赖关系吗?引用关系与依赖关系有什么区别?
这个问题很好!某些情况下,某类保持另外一个类的引用,但该类不调用另外一个类的方法,而是将另外一个类的引用暴露出来,供外部调用。
例如Class1有某一属性Class2,访问该属性可获得Class2的实例:
class1 = new Class1( );
class2 = class1.Class2;
Class1虽然有Class2的引用,但它自身不调用Class2的方法,而是让外部通过熟悉获得Class2的实例。
不过很多资料及书籍对“依赖关系”及“引用关系”的解释并不到位,不同资料之间的解释甚至是互斥的。而我见到的大部分设计模式的类图都不会区分“依赖关系”和“引用关系”,大部分设计模式的类图都会画成“引用关系”,本书后面的类图也将会不区别这两者,全部画成“引用关系”。
“包含”关系
我将“包含”关系分为“弱包含”和“强包含”,下图上面的是“弱包含”,下图下面的是“强包含”,此图说明了两种包含的区别。
“弱包含”和“强包含”只是我的一种俗称,学术的叫法是“聚合”和“组合”,一般资料可能会搞到你很晕,下图希望可以进一步解释清楚。

设计模式的类图中不少地方会用到“包含”关系,一些资料画成“强包含”,一些资料画成“弱包含”。我个人将“强包含”看成是“弱包含”的一种特例,大部分情况下我先画成“弱包含”,有需要才转化为“强包含”。本书中的包含关系,全部都画成“弱包含”。
对象图
类实例化后就是对象(Object),表示这些对象及对象之间关系的图就是对象图。有时候需要用对象图来表示设计模式。
对象图的例子:
请看下一文……
作者:张传波
创新工场创业课堂(敏捷课程)讲师
软件研发管理资深顾问
CMMI首席专家
《火球——UML大战需求分析》作者
《硬啃设计模式》作者
www.umlonline.org创办人

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In the Java framework, the difference between design patterns and architectural patterns is that design patterns define abstract solutions to common problems in software design, focusing on the interaction between classes and objects, such as factory patterns. Architectural patterns define the relationship between system structures and modules, focusing on the organization and interaction of system components, such as layered architecture.

The decorator pattern is a structural design pattern that allows dynamic addition of object functionality without modifying the original class. It is implemented through the collaboration of abstract components, concrete components, abstract decorators and concrete decorators, and can flexibly expand class functions to meet changing needs. In this example, milk and mocha decorators are added to Espresso for a total price of $2.29, demonstrating the power of the decorator pattern in dynamically modifying the behavior of objects.

The Adapter pattern is a structural design pattern that allows incompatible objects to work together. It converts one interface into another so that the objects can interact smoothly. The object adapter implements the adapter pattern by creating an adapter object containing the adapted object and implementing the target interface. In a practical case, through the adapter mode, the client (such as MediaPlayer) can play advanced format media (such as VLC), although it itself only supports ordinary media formats (such as MP3).

1. Factory pattern: Separate object creation and business logic, and create objects of specified types through factory classes. 2. Observer pattern: allows subject objects to notify observer objects of their state changes, achieving loose coupling and observer pattern.

TDD is used to write high-quality PHP code. The steps include: writing test cases, describing the expected functionality and making them fail. Write code so that only the test cases pass without excessive optimization or detailed design. After the test cases pass, optimize and refactor the code to improve readability, maintainability, and scalability.

Design patterns solve code maintenance challenges by providing reusable and extensible solutions: Observer Pattern: Allows objects to subscribe to events and receive notifications when they occur. Factory Pattern: Provides a centralized way to create objects without relying on concrete classes. Singleton pattern: ensures that a class has only one instance, which is used to create globally accessible objects.

The advantages of using design patterns in Java frameworks include: enhanced code readability, maintainability, and scalability. Disadvantages include complexity, performance overhead, and steep learning curve due to overuse. Practical case: Proxy mode is used to lazy load objects. Use design patterns wisely to take advantage of their advantages and minimize their disadvantages.

The Guice framework applies a number of design patterns, including: Singleton pattern: ensuring that a class has only one instance through the @Singleton annotation. Factory method pattern: Create a factory method through the @Provides annotation and obtain the object instance during dependency injection. Strategy mode: Encapsulate the algorithm into different strategy classes and specify the specific strategy through the @Named annotation.
