MFC与设计模式

转载 2008年10月03日 23:20:00
 

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.

The article Bridge Pattern - Bridging the gap between Interface and Implementation talks about Bridge pattern in detail.

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.

MFC 中的设计模式分析

最近在学习设计模式,突然想到MFC里面其实也包含有设计模式的原理,于是分析了一下,做一个笔记,网上也找了一些资料,在此一并感谢。 创建型模式 单例模式(Singleton Patt...
  • hero06
  • hero06
  • 2012年05月02日 22:36
  • 2156

设计模式——状态模式(有限状态机)

状态模式适用的情况:一个context对象根据其所处的状态改变其行为。在状态模式中,我们通过创建不同的state对象来表示不同的状态,而context对象则根据不同的state对象表现出不同的行为。状...
  • GW569453350game
  • GW569453350game
  • 2015年12月29日 17:11
  • 3539

设计模式游戏完美开发的学习总结

一、State模式 GOF的解释”:让一个对象的行为随着内部状态的改变而变化,而该对象也像是换了类一样”。 书中用State模式实现了场景的转换。 这是State模式的结...
  • mcaisw
  • mcaisw
  • 2017年07月07日 18:31
  • 914

论设计模式与敏捷开发

又是一年的五一了,北京的天气今天也热了起来,空气中飘着柳絮但却感觉不到春天的舒适,反而有一点夏天的感觉.就想起来很都朋友说的一句话:“北京是没有春天的!”   这一阵子在潜心研究设计模式以及敏捷软件开...
  • sunnyShineBoy
  • sunnyShineBoy
  • 2006年04月30日 15:40
  • 927

读《大话设计模式》和《head first 设计模式》心得

1.面向对象的编程,并不是类越多越好,类的划分是为了封装,但分类的基础是抽象,具有相同属性和功能的对象的抽象集合才是类。 2.如果你能够想到多于一个的动机去改变一个类,那么这个类就具有多于一个的职责...
  • fuqiaoyimeng
  • fuqiaoyimeng
  • 2013年11月10日 18:17
  • 1253

设计模式与重构是一种追求

1         设计模式与重构是一种追求 当一个软件工程师(DEV)一开始编码的时候,因为技术的缺乏,追求的必当是完成任务(Coding)。 随着时间的发展,DEV已经能够完成任务之后。也便开始追...
  • sunyaqing2008
  • sunyaqing2008
  • 2016年04月05日 12:06
  • 567

【学习笔记javascript设计模式与开发实践----1】

个人吐槽 读了不少的关于js的基础书箱,有必要在js的编码结构和编码效率上有所提高(个人资质很差,所以只代表个人观点。如有说的不对的,还请大神们见谅……),个人一直觉得,不会用设计模式不代表你就不是一...
  • pigpigpig4587
  • pigpigpig4587
  • 2015年09月21日 18:09
  • 2170

《Android源码设计模式解析与实战》读书笔记(二十六)

第二十六章、MVP应用构架模式1.MVP介绍 MVP模式是MVC模式的一个演化版本,MVP全称Model-View-Presenter。目前MVP在Android应用开发中越来越重要了。 在And...
  • qq_17766199
  • qq_17766199
  • 2016年01月27日 08:58
  • 5666

MFC 与 设计模式

设计模式是用来为重复出现的问题提供解决方案。它帮助设计者将系统分解成相互合作的对象集并发现其关系。它为我们提供了一种用来交流设计原则的公共词汇。有两种分类的方法。一种按目的(创造型,结构型,行为型),...
  • mydriverc
  • mydriverc
  • 2007年04月12日 10:16
  • 553

《JavaScript设计模式与开发实践》

《JavaScript设计模式与开发实践》
  • u012755393
  • u012755393
  • 2017年04月04日 13:47
  • 329
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:MFC与设计模式
举报原因:
原因补充:

(最多只允许输入30个字)