Vinova tuyển lập trình viên Mobile & Web ở Hà Nội, lương $300-1000

Article: Khái quát về design pattern 6753

Design pattern
phananhvu.myopenid.com 125
Over 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

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.

Comments

You must login to be able to comment

Uploaded files

No file uploaded yet

You must login to be able to upload

Nhà tài trợ:

Mọi người đều tự do viết bài, sửa bài của người khác, và bình luận ở trang web này. Bạn muốn chủ động tạo bài mới để chia sẻ kinh nghiệm với mọi người? Xin click link ở dưới.

Create new content