• 全部
  • VC综合技术
  • 互联网技术
  • MFC AppLauncher
  • .NET 技术
  • 界面
  • 进程
  • 算法
  • 硬件/系统
  • 数据库
  • VC++技术资源

關于設計模式和分析模式

VirtualAlex 2001-07-12 10:45:21
各位﹐我目前正巧接触了Design Patterns的一些東東﹐感覺很實用。但這方面的資料很少﹐無法深入了解。另外也很難找到有關Analysis Patterns 的資料.請大家一起探討﹗
...全文
63 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Cline 2001-07-12
剛接觸,不太懂,聽課.
回复
littleme741 2001-07-12
VirtualAlex,需要你的OICQ和MAIL以便有空一起探讨,你所讨论的问题我个人很感兴趣。
回复
VirtualAlex 2001-07-12
另外還有Analysis Patterns的話題﹐請關注。
回复
littleme741 2001-07-12
我只是提出几个问题吧:
1。bridge技术和接口技术各自的优缺点是否值得探讨?
2。如何从singleton技术出发考虑程序中pool的设计,比如在内存中建立缓冲池,在多网络连接时建立socket池,比如多线程设计中建立线程池等等。
3。希望大家结合自己实际的设计体会能举一些实际使用设计模式的例子。

这个帖子很好。
关注。
回复
y_pro 2001-07-12
要学这些东西,E文一定要好,因为大多文档都是E文的。
模式,其实是高级熟练工给低级熟练工用的东西^o^
回复
FG_Hope 2001-07-12
gz
回复
bzshow 2001-07-12
Introduction

Design Patterns are intended to provide solutions to recurring design problems. Design Patterns play a major role in the design of complex systems using Object Oriented methods. Patterns help designers to decompose the system into set of co-operating objects instead of decomposing objects and trying to find relationships between them. In fact, most of us are reusing class relationships and object collaborations in our design without even realizing that we are actually using Design Patterns. Even though Design Patterns are available for a long time, the credit goes to Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides (Gang of Four -GoF) for coming out with a good collection of Patterns for common Design Problems. Their book, Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma et al. (Addison-Wesley, 1995) was one of the first and best works done on Patterns.

In this article, we will be seeing what a Design Pattern is, followed by GoF's classification of Patterns and then jumping directly into the main topic of the article, MFC and Design Patterns. Portions of this article are extracted directly from GoF's book and throughout this article, I will be using the terms from their book.

Design Patterns
As mentioned earlier, the goal of Design Patterns is to provide solutions for recurring design problems. Design Patterns are meant to provide a common vocabulary for communicating design principles. They help designers to share their design using a common language. For example, you can tell your friend who knows pattern vocabulary that you have used Bridge Pattern in your design without explaining the details of class relationships.

So, how it all started? When Christopher Alexander and his colleagues tried to explain the architecture of buildings and cities, they used a Pattern Language. According to Christopher Alexander, "Each pattern describes a problem which occurs over and over again in our environment , and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice". This principle is not only applicable to the construction of building and cities, but it can also be extended to software construction using Object Oriented Design. When patterns are applied to software design, they take the name of Design Patterns. To summarize, Design Patterns provide proven solutions to recurring design problems.





**********************MFC and Design Patterns************************


As we all know, MFC is one of the popular class libraries used by C++ programmers. Nowadays, more and more commercial and business applications are developed using MFC. In fact, MFC and Visual C++ are the backbone for most of the recent Microsoft Products including Visual C++. The success story behind MFC is its class architecture and design principles adopted by the developers of MFC, which makes it one of the popularly reused library.

This main topic of this article is to show how patterns are used in MFC. We will be seeing the usage of three patterns in MFC library.

Creational : Singleton Pattern

First step in any MFC application is the creation of application object (object of class derived from CWinApp). There should be only one application object in an instance of MFC application. CWinApp is designed to make sure that only application object is present in a given instance. CWinApp and its descendants are called Singleton Classes. A class (CWinApp or its descendant) that assures a maximum of ONE object of its type at a given time and provides a global access point (AfxGetApp() method) to this object is a Singleton class.

As this principle is applied over and over again to solve recurring object "creational" problems, this becomes a pattern. Singleton Pattern ensures that a class only has one instance and provides a global access point it. The article Creating Singleton Objects using Visual C++ talks about different approaches for implementing Singletons.


Structural : Bridge Pattern

Bridge Pattern is all about decoupling an abstraction (interface) from its implementation so that the two can vary independently. In MFC, the process of storing/retrieving an object to/from a persistence mechanism (like a file) is called Serialization. MFC uses the Bridge Pattern to implement Serialization. CArchive and CFile classes implement object Serialization. CArchive class provides the interface for writing/reading an object to/from a persistence mechanism whereas the CFile and its sub classes provides implementation for different persistence mechanisms such as memory, disk file, sockets etc.

A CArchive object is configured with an object of class CFile (or a derived class) during its construction, from which it obtains the necessary information for serialization, including the filename and type of the requested operation (a read or write). Client performing the Serialization operation can use CArchive object without regarding the persistence mechanism implemented by CFile classes.



Behavioral : Observer Pattern

The Observer Pattern is intended to "Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically". An object that is subjected to change is called a Subject and an object that depends on the Subject's state is called an Observer.

MFC uses a Document/View variant of the Observer Pattern. MFC's famous Document/View architecture uses this variant. A document contains the data object and acts as a Subject. A view is a window object through which the user updates the document and it acts as an Observer. A document can have multiple views. Whenever the data in the document is changed by one of the views, it updates the document by calling UpdateAllViews method, with optional hint about the modification. To inform about the change to other views, the document object calls OnUpdate method for each view attached to it (except the view that called UpdateAllViews). Derived view classes can override the OnUpdate method and update themselves by querying the data from the document.

The article Applying Observer Pattern in C++ Applications talks about Observer pattern in detail.

Summary
Design Pattern provides a common language for designers and helps them to decompose systems into a set of cooperating classes and objects. They provide proven design solution to recurring problems. In this article, I have introduced the design pattern and how MFC is using some of the patterns in its design. We can easily find more and more patterns when we dig into MFC and its classes.

回复
相关推荐
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2001-07-12 10:45
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……