Você está na página 1de 25

Agile design

Lai Duy Ha

Outline
Five

principles for agile design Working with diagrams Common errors Useful patterns

Five principles for agile design


The

Single-Responsibility Principle (SRP) The Open/Closed Principle (OCP) The Liskov Substitution Principle (LSP) The Dependency-Inversion Principle (DIP) The Interface Segregation Principle (ISP)

The Single-Responsibility Principle (SRP)


A

class should have only one reason to change.

The Single-Responsibility Principle (SRP)


Each

responsibility is an axis of change. If a class has more than one responsibility.


have more than one reason to change. the responsibilities become coupled.

Example:

The Single-Responsibility Principle (SRP)

Computational Geometry App:


Use area() operation. computational geometry. never drawing the rectangle on the screen. Use draw() operation. draws the rectangle on the screen.

Graphical App:

The Single-Responsibility Principle (SRP)

Separated responsibilities

The Open/Closed Principle (OCP)


Software

entities (classes, modules, functions, etc.) should be:


open for extension but closed for modification.

The Open/Closed Principle (OCP)

The Open/Closed Principle (OCP)


Real

example:

Validate comment is spam or not Lots of strategy & service


visitors

IP address against Project Honey Pots IP black list. Another protection: Akismet.

The Open/Closed Principle (OCP)

The Liskov Substitution Principle (LSP)


Subtypes

must be substitutable for their base types.

The Liskov Substitution Principle (LSP)

Satisfies LSP if:


as long as arg is the same. every time you call foo on a Derived object it gives exactly the same results as calling foo on a Base object

A Good Example of Liskov Substitution Principle

The Liskov Substitution Principle (LSP)

The Dependency-Inversion Principle (DIP)

High-level modules should not depend on lowlevel modules. Both should depend on abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.

The Dependency-Inversion Principle (DIP)

Ownership Inversion

The Dependency-Inversion Principle (DIP)

The Furnace Example control the regulator of a furnace. read the current temperature from an I/O channel instruct the furnace to turn on or off by sending commands to a different I/O channel

The Dependency-Inversion Principle (DIP)

The Dependency-Inversion Principle (DIP)


A

bank example:
Transfer money between account

transfer money from a bank account to pay bills???

The Dependency-Inversion Principle (DIP)

The Interface Segregation Principle (ISP)


Clients

should not be forced to depend on methods they do not use.

The Interface Segregation Principle (ISP)

breaking the interface

The Interface Segregation Principle (ISP)

breaking the interface

Outline
Five

principles for agile design Working with diagrams Common errors Useful patterns

Reference
Martin,

R. (2006). Agile principles, patterns, and practices in C#. Prentice-Hall PTR.

Você também pode gostar