设计模式总览

本文详细介绍了设计模式的概念,包括创建型、结构型和行为型三大类,并阐述了每种模式的目的、动机、适用性、结构、协作、后果、实现及实施问题等关键要素。通过实际代码示例,深入探讨了如何在软件设计中灵活运用这些模式来简化对象创建逻辑、解决类爆炸问题、简化对象间通信、实现责任分担和算法扩展性等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

From http://www.codeproject.com/Articles/106198/Design-Patterns-nuff-said

Popular design pattern introduced by GoF are as follows:

Category: Creational design patterns

  • Abstract Factory - Creates an instance of several families of classes
  • Builder - Separates object construction from its representation
  • Factory Method - Creates an instance of several derived classes
  • Prototype - A fully initialized instance to be copied or cloned
  • Singleton - A class of which only a single instance can exist

Category: Structural design patterns 

  • Adapter - Match interfaces of different classes
  • Bridge - Separates an object’s interface from its implementation
  • Composite - A tree structure of simple and composite objects
  • Decorator - Add responsibilities to objects dynamically
  • Façade - A single class that represents an entire subsystem
  • Flyweight - A fine-grained instance used for efficient sharing
  • Proxy - An object representing another object

Category: Behavioral design patterns

  • Chain of responsibility - A way of passing a request between a chain of objects
  • Command - Encapsulate a command request as an object
  • Interpreter - A way to include language elements in a program
  • Iterator - Sequentially access the elements of a collection
  • Mediator - Defines simplified communication between classes
  • Memento - Capture and restore an object’s internal state
  • Observer - A way of notifying change to a number of classes
  • State - Alter an object’s behavior when its state changes
  • Strategy - Encapsulates an algorithm inside a class
  • Template method - Defer the exact steps of an algorithm to a subclass
  • Visitor - Defines a new operation to a class without change

 

For each design pattern we should explore following points:-

  1. Design pattern name
  2. Intent – What all design problems it solves
  3. Motivation – Understanding the design problems solved by this design pattern to appreciate its intent
  4. Applicability – Known practical applications. These applications are many of the few to give you a head start to explore new software design areas where this pattern is applicable. It will come with practice and experience so beginners do not worry about this now.
  5. Structure / UML diagram
  6. Participants – Roles of each class in design pattern structure described in point 5.
  7. Collaborations – Object communication
  8. Consequences – What things we can do after using design pattern which are not possible without using it.
  9. Implementation and Implementation issue – How to map design into language specific working application 
  10. Sample code – The application code
  11. Known issues – pattern limitations and their solution if any. 
  12. Related patterns – Compound pattern. Matter of advance research for beginners. 

The first thing that GoF done towards making 'the Intent' of the design patterns clear is by doing high level categorization of design patterns:-

  • Creational Design Patterns: If answers to any of the following question is ‘Yes’ then your design need one of the Creational Design Pattern
  • Is my design is having object creation logic?
  • Is object creation logic is complex and needs to be simplified?
  • Is object creation logic is subject to frequent changes in future and hence need to decoupled from client logic?
  • Structural Design Patterns: We need one of the Structural Design Pattern when
  • We are in process of building new types (specifying new class definitions) or extending existing types (through inheritance or composition) and we want that our new types addition will not make our design complex to understand and it works with same client code thus promoting code re-use by the virtue of loosely coupled design
  • Our design is affected by problems like class explosion, machine resources limitations etc.
  • Behavioral Design Patterns: We need one of the Behavioral Design Pattern when
  • We need to simply object communication by virtue of loosely coupled design
  • Having division of responsibility in context of algorithms and at the same time we want our design to be scalable with respect to addition/modification of algorithms

转载于:https://www.cnblogs.com/windy86/p/3978699.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值