Click here to Skip to main content
15,892,298 members
Articles / Programming Languages / C++/CLI

Bridge Pattern - Bridging the gap between Interface and Implementation

Rate me:
Please Sign up or sign in to vote.
4.60/5 (26 votes)
8 Jan 2001 198.4K   686   83  
This article discusses the Bridge Pattern, what it is, why and when it is needed.
// BridgeDoc.cpp : implementation of the CBridgeDoc class
//

#include "stdafx.h"
#include "Bridge.h"

#include "BridgeDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBridgeDoc

IMPLEMENT_DYNCREATE(CBridgeDoc, CDocument)

BEGIN_MESSAGE_MAP(CBridgeDoc, CDocument)
	//{{AFX_MSG_MAP(CBridgeDoc)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBridgeDoc construction/destruction

CBridgeDoc::CBridgeDoc()
{
	// Step 1 - Initialize class members
	m_pImage = NULL;
}

CBridgeDoc::~CBridgeDoc()
{
	// Step 1 - Delete image object, if it is valid
	if( m_pImage != NULL )
	{
		delete m_pImage;
	}
}

/////////////////////////////////////////////////////////////////////////////
// CBridgeDoc diagnostics

#ifdef _DEBUG
void CBridgeDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CBridgeDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CBridgeDoc commands

void CBridgeDoc::OnFileOpen() 
{
   CFileDialog FileDialog( TRUE, NULL, NULL,
                        OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT 
                        | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST, 
                        "Bitmap Files (*.bmp;*.wmf)|*.bmp;*.wmf|DIB Files (*.dib)|*.dib|All Files (*.*)|*.*|||", 
								NULL );

	// Step 1 - Show the dialog box and open the document, if a valid file name is selected
	if( FileDialog.DoModal() == IDOK )
	{
		OnOpenDocument( FileDialog.GetPathName() );
	}
}

BOOL CBridgeDoc::OnOpenDocument( LPCTSTR lpszPathName ) 
{
	CWaitCursor WaitCursor;
	CImage		* pTempImage = NULL;

	// Step 1 - Create an image object
	pTempImage = new CBmpImage;
	if( pTempImage == NULL )
	{
		CHAR szTempString[ 512 ];

		memset( szTempString, 0, sizeof( szTempString ) );
		sprintf( szTempString, "Insufficient memory.\n");
		AfxMessageBox( "Insufficient memory", MB_ICONSTOP | MB_OK );
		return FALSE;
	}

	// Step 2 - Load image file and configure with the runtime information 
	// of the implementation class, CWinImp in this case
	if( pTempImage->Load( lpszPathName, RUNTIME_CLASS( CWinImp ) ) != SUCCESS )
	{
		// Step 3 - Delete object and display error message
		delete pTempImage;
		CHAR szTempString[ 512 ];

		memset( szTempString, 0, sizeof( szTempString ) );
		sprintf( szTempString, "Unable to open the image file %s.\n", lpszPathName );
		AfxMessageBox( szTempString, MB_ICONINFORMATION | MB_OK );
		return FALSE;
	}

	// Step 4 - Delete old image object, if it is valid
	if( m_pImage != NULL )
	{
		delete m_pImage;
	}
	// Step 5 - Assign, new image object
	m_pImage = pTempImage;
	UpdateAllViews( NULL );
	SetPathName( lpszPathName );
	return TRUE;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Switzerland Switzerland
Kulathu Sarma is working as a Technology Manager for GoldAvenue, a company based in Geneva, Switzerland and responsible for supporting e-business initiatives of GoldAvenue in B2B and B2C Sectors. He has been programming in C/C++ for 9 years and actively working on Patterns for the past 5 years.

Comments and Discussions