Design Mode C ++ study note 11 (Bridge Mode)

Source: Internet
Author: User

Bridge Mode: separates abstract parts from their implementations so that they can all change independently. The method to implement separation is to add a class,

11. 1. Explanation

Main (), customer

IProduct, product Interface

CHouse, house

CIPod, ipod

CClothes, clothing

CNewCorp, bridge class, MakeMoney () is the Bridge Method

CNewHouseCorp can only produce houses, so the constructor is CHouse *

CShanZhaiCorp, the constructor is IProduct *

Note: The customer directly uses the CNewHouseCorp and CShanZhaiCorp classes, constructs the product in the main () function, and then uploads it to these two classes. The MakeMoney () functions of these two classes first call the MakeMoney () of the base class and then execute their respective logic.

Note: CNewCorp serves as a bridge. You can add products and companies separately.

Check the Code:

// NewCorp. h

# Pragma once
# Include "IProduct. h"
Class CNewCorp
{
Public:
CNewCorp (IProduct * pproduct );
Virtual ~ CNewCorp (void );
Void MakeMoney ();
Private:
IProduct * m_pProduct;
};

// NewCorp. cpp

# Include "StdAfx. h"
# Include "NewCorp. h"
CNewCorp: CNewCorp (IProduct * pproduct)
{
This-> m_pProduct = pproduct;
}
CNewCorp ::~ CNewCorp (void)
{
}
Void CNewCorp: MakeMoney ()
{
// Every company is the same, first produce
This-> m_pProduct-> BeProducted ();

// Then sell
This-> m_pProduct-> BeSelled ();
}

// NewHouseCorp. h

# Pragma once
# Include "newcorp. h"
# Include "House. h"
Class CNewHouseCorp:
Public CNewCorp
{
Public:
CNewHouseCorp (CHouse * pHouse );
~ CNewHouseCorp (void );
Void MakeMoney ();
};

// NewHouseCorp. cpp

# Include "StdAfx. h"
# Include "NewHouseCorp. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CNewHouseCorp: CNewHouseCorp (CHouse * pHouse): CNewCorp (pHouse)
{
}
CNewHouseCorp ::~ CNewHouseCorp (void)
{
}
Void CNewHouseCorp: MakeMoney ()
{
This-> CNewCorp: MakeMoney ();
Cout <"real estate companies make a lot of money..." <endl;
}

// ShanZhaiCorp. h

# Pragma once
# Include "newcorp. h"
# Include "IProduct. h"
Class CShanZhaiCorp:
Public CNewCorp
{
Public:
CShanZhaiCorp (IProduct * pproduct );
~ CShanZhaiCorp (void );
Void MakeMoney ();
};

// ShanZhaiCorp. cpp

# Include "StdAfx. h"
# Include "ShanZhaiCorp. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CShanZhaiCorp: CShanZhaiCorp (IProduct * pproduct): CNewCorp (pproduct)
{
}
CShanZhaiCorp ::~ CShanZhaiCorp (void)
{
}
Void CShanZhaiCorp: MakeMoney ()
{
This-> CNewCorp: MakeMoney ();
Cout <"I am making money..." <endl;
}

// IProduct. h

# Pragma once
Class IProduct
{
Public:
IProduct (void)
{
}
Virtual ~ IProduct (void)
{
}
Virtual void BeProducted () = 0;
Virtual void BeSelled () = 0;
};

// House. h

# Pragma once
# Include "iproduct. h"
Class CHouse:
Public IProduct
{
Public:
CHouse (void );
~ CHouse (void );
Void BeProducted ();
Void BeSelled ();
};

// House. cpp

# Include "StdAfx. h"
# Include "House. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CHouse: CHouse (void)
{
}
CHouse ::~ CHouse (void)
{
}
Void CHouse: BeProducted ()
{
Cout <"the house produced looks like this..." <endl;
}
Void CHouse: BeSelled ()
{
Cout <"the house produced has been sold..." <endl;
}

// Clothes. h

# Pragma once
# Include "iproduct. h"
Class CClothes:
Public IProduct
{
Public:
CClothes (void );
~ CClothes (void );
Void BeProducted ();
Void BeSelled ();
};

// Clothes. cpp

# Include "StdAfx. h"
# Include "Clothes. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CClothes: CClothes (void)
{
}
CClothes ::~ CClothes (void)
{
}
Void CClothes: BeProducted ()
{
Cout <"the clothes produced are like this..." <endl;
}
Void CClothes: BeSelled ()
{
Cout <"The produced clothes are sold..." <endl;
}

// IPod. h

# Pragma once
# Include "iproduct. h"
Class CIPod:
Public IProduct
{
Public:
CIPod (void );
~ CIPod (void );
Void BeProducted ();
Void BeSelled ();
};

// IPod. cpp

# Include "StdAfx. h"
# Include "IPod. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CIPod: CIPod (void)
{
}
CIPod ::~ CIPod (void)
{
}
Void CIPod: BeProducted ()
{
Cout <"the ipod produced looks like this..." <endl;
}
Void CIPod: BeSelled ()
{
Cout <"the ipod was sold out..." <endl;
}

//

// Bridge. cpp: defines the entry point of the console application.
//

# Include "stdafx. h"
# Include "ClothesCorp. h"
# Include "NewHouseCorp. h"
# Include "Clothes. h"
# Include "IPod. h"
# Include "ShanZhaiCorp. h"
# Include <iostream>
Using std: cout;
Using std: endl;

Void DoNewRun1 ()
{
Cout <"---------- real estate companies run like this ----------" <endl;
CHouse house;
CNewHouseCorp newHouseCorp (& house );
NewHouseCorp. MakeMoney ();
Cout <endl;

Cout <"---------- this is how the company runs ----------" <endl;
CClothes clothes;
CShanZhaiCorp shanZhaiCorp (& clothes );
ShanZhaiCorp. MakeMoney ();
Cout <endl;
}

Void DoNewRun2 ()
{
Cout <"---------- real estate companies run like this ----------" <endl;
CHouse house;
CNewHouseCorp newHouseCorp (& house );
NewHouseCorp. MakeMoney ();
Cout <endl;

Cout <"---------- this is how the company runs ----------" <endl;
CIPod ipod;
CShanZhaiCorp shanZhaiCorp (& ipod );
ShanZhaiCorp. MakeMoney ();
Cout <endl;
}

Int _ tmain (int argc, _ TCHAR * argv [])
{
// There are only two companies. One is a real estate company, and the other is clothing that produces clothes when making money.
DoNewRun1 ();

// There are only two companies. One is a real estate company, and the other is an ipod.
DoNewRun2 ();

_ CrtSetDbgFlag (_ CRTDBG_LEAK_CHECK_DF | _ CRTDBG_ALLOC_MEM_DF );
_ CrtDumpMemoryLeaks ();
Return 0;
}

The bridge mode (also known as the bridge mode) belongs to the structural mode. CNewCorp implements the bridge function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.