Article:
Khái quát về design pattern
6753
phananhvu.myopenid.com 125Over 4 years ago |
Trong kỹ thuật phần mềm(software engineering), design pattern là giải pháp tổng quát có thể dùng lại cho các vấn đề phổ biến trong thiết kế phần mềm. Design pattern không phải là design cuối cùng có thể dùng để chuyển thành code. Nó chỉ là các gợi ý, mẫu mà chỉ ra cách giải quyết vấn đề trong các trường hợp. Các design pattern trong thiết kế hướng đối tượng thường chỉ ra mối quan hệ và tương tác giữa các lớp hay các đối tượng, chứ không chỉ ra các lớp, đối tượng cụ thể nào. Thuật toán không phải design patterns vì chúng chỉ qiải quyết các vấn đề tính toán chứ không giải quyết các vấn đề thiết kế.
Ứng dụng
Design pattern giúp tăng tốc độ phát triển phần mềm bằng cách đưa ra các mô hình test, mô hình phát triển đã qua kiểm nghiệm. Thiết kế phần mềm hiệu quả đòi hỏi phải cân nhắc các vấn đề sẽ nảy sinh trong quá trình hiện thực hóa (implementation). Dùng lại các design pattern giúp tránh được các vấn đề tiềm ẩn có thể gây ra những lỗi lớn, đồng thời giúp code dễ đọc hơn.
Thông thường, chúng ta chỉ biết áp dụng một kĩ thuật thiết kế nhất định để giải quyết một bài toán nhất định. Các kĩ thuật này rất khó áp dụng với các vấn đề trong phạm vi rộng hơn. Design pattern cung cấp giải pháp ở dạng tổng quát.
Design pattern gồm các phần như Structure, Participants, Collaboration, .. Các phần này mô tả một design motif: là một micro-architecture nguyên mẫu mà các developer sẽ lấy và áp dụng vào thiết kế cụ thể của họ. Micro-architecture là tập hợp các thành phần (class, method..) và mối quan hệ giữa chúng. Developer sử dụng design pattern bằng cách đưa các micro-architecture vào trong thiết kế của họ, nghĩa là các micro-architecture trong thiết kế của họ có cấu trúc và cách tổ chức tương tự như trong design motif được chọn.
Phân loại
Design pattern có thể được phân loại dựa vào vấn đề mà chúng giải quyết như sau:
- Fundamental patterns
- Delegation pattern: an object outwardly expresses certain behaviour but in reality delegates responsibility
- Functional design: assures that each modular part of a computer program has only one responsibility and performs that with minimum side effects
- Interface pattern: method for structuring programs so that they're simpler to understand
- Proxy pattern: an object functions as an interface to another, typically more complex, object
- Facade pattern: provides a simplified interface to a larger body of code, such as a class library.
- Composite pattern: defines Composite object (e.g. a shape) designed as a composition of one-or-more similar objects (other kinds of shapes/geometries), all exhibiting similar functionality. The Composite object then exposes properties and methods for child objects manipulation as if it were a simple object.
- Creational patterns which deal with the creation of objects. Abstract Factory and Factory Method are creation patterns
- Abstract factory pattern: centralize decision of what factory to instantiate
- Factory method pattern: centralize creation of an object of a specific type choosing one of several implementations
- Builder pattern: separate the construction of a complex object from its representation so that the same construction process can create different representations
- Lazy initialization pattern: tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed
- Object pool: avoid expensive acquisition and release of resources by recycling objects that are no longer in use
- Prototype pattern: used when the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) is prohibitively expensive for a given application
- Singleton pattern: restrict instantiation of a class to one object
- Structural patterns that ease the design by identifying a simple way to realize relationships between entities. Adapter is a structural pattern
- Adapter pattern: 'adapts' one interface for a class into one that a client expects
- Aggregate pattern: a version of the Composite pattern with methods for aggregation of children
- Bridge pattern: decouple an abstraction from its implementation so that the two can vary independently
- Composite pattern: a tree structure of objects where every object has the same interface
- Decorator pattern: add additional functionality to a class at runtime where subclassing would result in an exponential rise of new classes
- Extensibility pattern: aka. Framework - hide complex code behind a simple interface
- Facade pattern: create a simplified interface of an existing interface to ease usage for common tasks
- Flyweight pattern: a high quantity of objects share a common properties object to save space
- Proxy pattern: a class functioning as an interface to another thing
- Pipes and filters: a chain of processes where the output of each process is the input of the next
- Private class data pattern: restrict accessor/mutator access
- Behavioral patterns that identify common communication patterns between objects and realize these patterns. Interpreter is a behavioral pattern
- Chain of responsibility pattern: Command objects are handled or passed on to other objects by logic-containing processing objects
- Command pattern: Command objects encapsulate an action and its parameters
- Interpreter pattern: Implement a specialized computer language to rapidly solve a specific set of problems
- Iterator pattern: Iterators are used to access the elements of an aggregate object sequentially without exposing its underlying representation
- Mediator pattern: Provides a unified interface to a set of interfaces in a subsystem
- Memento pattern: Provides the ability to restore an object to its previous state (rollback)
- Null Object pattern: Designed to act as a default value of an object
- Observer pattern: aka Publish/Subscribe or Event Listener. Objects register to observe an event which may be raised by another object
- State pattern: A clean way for an object to partially change its type at runtime
- Strategy pattern: Algorithms can be selected on the fly
- Specification pattern: Recombinable Business logic in a boolean fashion
- Template method pattern: Describes the program skeleton of a program
- Visitor pattern: A way to separate an algorithm from an object
- Single-serving visitor pattern: Optimise the implementation of a visitor that is allocated, used only once, and then deleted
- Hierarchical visitor pattern: Provide a way to visit every node in a hierarchical data structure such as a tree.
Tài liệu
Tài liệu về một design pattern mô tả hoàn cảnh mà pattern nên được sử dụng, vấn đề cần phải giải quyết và giải pháp gợi ý. Không có một format chung cho việc viết tài liệu cho design pattern. Format được sử dụng phổ biến nhất là:
- Pattern Name and Classification: A descriptive and unique name that helps in identifying and referring to the pattern.
- Intent: A description of the goal behind the pattern and the reason for using it.
- Also Known As: Other names for the pattern.
- Motivation (Forces): A scenario consisting of a problem and a context in which this pattern can be used.
- Applicability: Situations in which this pattern is usable; the context for the pattern.
- Structure: A graphical representation of the pattern. Class diagrams and Interaction diagrams may be used for this purpose.
- Participants: A listing of the classes and objects used in the pattern and their roles in the design.
- Collaboration: A description of how classes and objects used in the pattern interact with each other.
- Consequences: A description of the results, side effects, and trade offs caused by using the pattern.
- Implementation: A description of an implementation of the pattern; the solution part of the pattern.
- Sample Code: An illustration of how the pattern can be used in a programming language
- Known Uses: Examples of real usages of the pattern.
- Related Patterns: Other patterns that have some relationship with the pattern; discussion of the differences between the pattern and similar patterns.
Nguồn: Wikipedia.
Design pattern
125