Welcome to my blog!

Design Patterns là gì?

July 20, 2018 10:56

1. Design Patterns là gì?

Design patterns là một kỹ thuật trong lập trình hướng đối tượng, được các nhà nghiên cứu đúc kết và tạo ra các mẫu thiết kế chuẩn. Và design pattern không phải là một ngôn ngữ lập trình cụ thể nào cả, mà nó có thể sự dụng được trong hầu hết các ngôn lập trình có hỗ trợ OOP hiện nay.

2. Tại sao nên sử dụng design patterns?

- Design pattern giúp cho dự án của chúng ta dễ bảo trì, nâng cấp và mở rộng.

- Hơn nữa do design pattern đã được các nhà nghiên cứu đúc kết ra nên khi sử dụng design pattern thì chúng ta sẽ hạn chế được các lỗi tiềm ẩn.

- Và cuối cùng là khi sử dụng design pattern thì sẽ giúp code của chúng ta sẽ dễ đọc hơn. Điều này rất có lợi khi làm việc nhóm.

3. Cần gì để học design patterns.

- Điều đầu tiên mình muốn nói là design pattern không dành cho nhưng bạn mới bắt đầu tìm hiểu về lập trình.

- Muốn tìm hiểu và học được design pattern thì bạn phải nắm chắc được kiến thức oop đặc biệt là về abstract class, interface và static.

4. Các loại design patterns.

- Về cơ bản thì design pattern sẽ được chia làm 3 dạng chính và tổng cộng 32 mẫu design:

  • Creational Pattern (nhóm khởi tạo): Nhóm này sẽ giúp bạn rất nhiều trong việc khởi tạo đối tượng, mà bạn khó có thể nhận ra (nó sẽ không dùng từ khóa new như bình thường). Nhóm này gồm 9 mẫu design là:
    • Abstract Factory.
    • Builder.
    • Factory Method.
    • Multiton.
    • Pool.
    • Prototype.
    • Simple Factory.
    • Singleton.
    • Static Factory.
  • Structural (nhóm cấu trúc): Nhóm này sẽ giúp chúng ta thiết lập, định nghĩa quan hệ giữa các đối tượng. Nhóm này gồm có 11 mẫu design là:
    • Adapter/ Wrapper.
    • Bridge.
    • Composite.
    • Data Mapper.
    • Decorator.
    • Dependency Injection.
    • Facade.
    • Fluent Interface.
    • Flyweight.
    • Registry.
    • Proxy
  • Behavioral patterns (nhóm ứng xử): Nhóm này sẽ tập trung thực hiện các hành vi của đối tượng. Gồm 12 mẫu design là:
    • Chain Of Responsibilities.
    • Command.
    • Iterator.
    • Mediator.
    • Memento.
    • Null Object.
    • Observer.
    • Specification.
    • State.
    • Strategy.
    • Template Method.
    • Visitor.

- Ngoài ra thì trong thời gian gần đây đã xuất hiện thêm 4 mẫu design nữa đó là:

  • Delegation.
  • Service Locator.
  • Repository.
  • Entity-Attribute-Value (EAV).

Design Patterns

January 5, 2018 10:47


  1. Design principles: SOLID
  2. Design patterns: Creational, Structural, Behavioral
  3. Guidelines for Asynchronous Programming: C# Asynchronous Programming Guideline Cheat Sheet
  4. Threading Design Guidelines: 
  5. Parallel Programming with .NET
    1. Guidelines & Sample:  Parallel Programming
    2. Task Parallel Library (TPL)
    3. Language Integrated Query (LINQ)
    4. Parallel Language Integrated Query (PLINQ)

What's design pattern?

The simplest way to describe a pattern is that it provides a proven solution to a common problem individually documented in a consistent format and usually as part of a larger collection.

The notion of a pattern is already a fundamental part of everyday life. Without acknowledging it each time, we naturally use proven solutions to solve common problems each day. Patterns in the IT world that revolve around the design of automated systems are referred to as design patterns.

Design patterns are helpful because they:

  • represent field-tested solutions to common design problems
  • organize design intelligence into a standardized and easily "referencable" format
  • are generally repeatable by most IT professionals involved with design
  • can be used to ensure consistency in how systems are designed and built
  • can become the basis for design standards
  • are usually flexible and optional (and openly document the impacts of their application and even suggest alternative approaches)
  • can be used as educational aids by documenting specific aspects of system design (regardless of whether they are applied)
  • can sometimes be applied prior and subsequent to the implementation of a system
  • can be supported via the application of other design patterns that are part of the same collection
  • enrich the vocabulary of a given IT field because each pattern is given a meaningful name

Furthermore, because the solutions provided by design patterns are proven, their consistent application tends to naturally improve the quality of system designs.

Note that even though design patterns provide proven design solutions, their mere use cannot guarantee that design problems are always solved as required. Many factors weigh in to the ultimate success of using a design pattern, including constraints imposed by the implementation environment, competency of the practitioners, diverging business requirements, and so on. All of these represent aspects that affect the extent to which a pattern can be successfully applied.

Design Patterns & Design Principles

  • Design principles are applied collectively to solution logic in order to shape it in such a manner that it fosters key design characteristics that support the strategic goals associated with service-oriented computing.
  • Design patterns provide solutions to common problems encountered when applying design principles-and-when establishing an environment suitable for implementing logic designed in accordance with service-orientation principles.

Các chủ điểm cần nghiên cứu của một design pattern

  • Requirement
  • Icon
  • Summary Table
  • Principles
  • Architecture
  • Problem
  • Solution
  • Application
  • Impacts
  • Relationships
  • Case Study Example


Common Design Patterns

1) Creational Patterns

  1. Abstract Factory Pattern: Create instances of classes belonging to different families
  2. Builder Pattern: Separate representation and object construction
  3. Factory Method Pattern: Create instances of derived classes
  4. Prototype Pattern: Clone or copy initialized instances
  5. Singleton Pattern: Class with only one single possible instance

2) Structural Patterns

  1. Adapter Pattern: Match interfaces of classes with different interfaces
  2. Bridge Pattern:: Separate implementation and object interfaces
  3. Composite: Simple and composite objects tree
  4. Decorator: Dynamically add responsibilities to objects
  5. Facade: Class that represents subclasses and subsystems
  6. Flyweight: Minimize memory usage by sharing as much data as possible with similar objects
  7. Proxy: Object that represents another object

3) Behavioral Patterns

  1. Chain of Responsibility: Pass requests between command and processing objects within a chain of objects
  2. Command: Encapsulate a method call as an object containing all necessary information
  3. Interpreter: Include language elements and evaluate sentences in a given language
  4. Iterator: Give sequential access to elements in a collection
  5. Mediator: Encapsulates and simplifies communication between objects
  6. Memento: Undo modifications and restore an object to its initial state
  7. Observer: Notify dependent objects of state changes
  8. State: Change object behavior depending on its state
  9. Strategy: Encapsulate algorithms within a class and make them interchangeable
  10. Template Method: Define an algorithm skeleton and delegate algorithm steps to subclasses so that they may be overridden
  11. Visitor: Add new operations to classes without modifying them

Other design patterns:

  1. MVC
  2. MVP
  3. MVVM
  4. Repository Pattern: Unit Of Work pattern + Repository pattern with Entity Framework
  5. Unit of Work Pattern: The design pattern that
  6. Parallel PatternsThe design
  7. SOA PatternsThe SOA design patterns catalog uses a simple notation to consistently represent different types of patterns.
  8. Enterprise Architecture PatternsThe design

C# Design Patterns


  1. Template Method
  2. Strategy
  3. Factory Method
  4. Abstract Factory
  5. Facade
  6. Command
  7. Observer
  8. Iterator
  9. Singleton


JavaScript Design Patterns


Recent posts