宸ュ巶鏂規硶鍜屾娊璞″伐鍘傚疄闄呬笂鏄粠涓嶅悓瑙掑害鍦ㄦ弿榪伴棶棰樸?br />宸ュ巶鏂規硶鎻忚堪浜嗗叿浣撲駭鍝佺殑鍒涘緩錛岃屾娊璞″伐鍘傛弿榪扮殑鏄駭鍝佺郴鍒楃殑緇勭粐銆?/p>
//
聽Computer.cpp聽:聽Defines聽the聽entry聽point聽for聽the聽console聽application.
//
#include聽
"
stdafx.h
"
#include聽
<
string
>
#include聽
<
iostream
>
using
聽
namespace
聽std;


class
聽Ram
{}
;

class
聽IBMRam:聽
public
聽Ram
{}
;

class
聽HPRam:聽
public
聽Ram
{}
;

class
聽Cpu
{}
;

class
聽IBMCpu:聽
public
聽Cpu
{}
;

class
聽HPCpu:聽
public
聽Cpu
{}
;

class
聽Computer

{
public
:
聽Computer(
string
聽strName,聽Ram
*
聽pRam,聽Cpu
*
聽pCpu)

聽
{
聽聽m_strName聽
=
聽strName;
聽聽m_pRam聽
=
聽pRam;
聽聽m_pCpu聽
=
聽pCpu;
聽聽cout
<<
"
A聽
"
<<
聽m_strName聽
<<
"
聽computer聽is聽produced
"
<<
endl;
聽}
聽
~
Computer()

聽
{
聽聽delete聽m_pRam;
聽聽delete聽m_pCpu;
聽聽cout
<<
"
A聽
"
<<
聽m_strName聽
<<
"
聽computer聽is聽deleted
"
<<
endl;
聽}
public
:
聽
string
聽m_strName;
private
:
聽Ram
*
聽m_pRam;
聽Cpu
*
聽m_pCpu;
}
;

class
聽ComputerProducer

{
public
:
聽Computer
*
聽createComputer()

聽
{
聽聽
return
聽
new
聽Computer(setName(),聽createRam(),聽createCpu());
聽}
聽
virtual
聽Ram
*
聽createRam()聽
=
聽
0
;
聽
virtual
聽Cpu
*
聽createCpu()聽
=
聽
0
;
聽
virtual
聽
string
聽setName()聽
=
聽
0
;
}
;

class
聽IBMProducer:聽
public
聽ComputerProducer

{
public
:
聽
virtual
聽Ram
*
聽createRam()

聽
{
聽聽cout
<<
"
A聽IBMRam聽is聽producted
"
<<
endl;
聽聽
return
聽
new
聽IBMRam;
聽}
聽
virtual
聽Cpu
*
聽createCpu()

聽
{
聽聽cout
<<
"
A聽IBMCPU聽is聽producted
"
<<
endl;
聽聽
return
聽
new
聽IBMCpu;
聽}
聽
virtual
聽
string
聽setName()

聽
{
聽聽
return
聽
string
(
"
IBM
"
);
聽}
}
;

class
聽HPProducer:聽
public
聽ComputerProducer

{
public
:
聽
virtual
聽Ram
*
聽createRam()

聽
{
聽聽cout
<<
"
A聽HPRam聽is聽producted
"
<<
endl;
聽聽
return
聽
new
聽HPRam;
聽}
聽
virtual
聽Cpu
*
聽createCpu()

聽
{
聽聽cout
<<
"
A聽HPCPU聽is聽producted
"
<<
endl;
聽聽
return
聽
new
聽HPCpu;
聽}
聽
virtual
聽
string
聽setName()

聽
{
聽聽
return
聽
string
(
"
HP
"
);
聽}
}
;

int
聽main(
int
聽argc,聽
char
*
聽argv[])

{
聽
//
聽client聽code
聽ComputerProducer
*
聽pIBMFac
=
聽
new
聽IBMProducer;
聽ComputerProducer
*
聽pHPFac聽
=
聽
new
聽HPProducer;
聽Computer
*
聽pIBMComputer聽
=
聽pIBMFac
->
createComputer();
聽Computer
*
聽pHPComputer聽
=
聽pHPFac聽
->
createComputer();
聽delete聽pIBMComputer;
聽delete聽pHPComputer;
聽delete聽pIBMFac;
聽delete聽pHPFac;
聽
return
聽
0
;
}
榪欎釜渚嬪瓙姣旇緝娓呮浜嗭紝涓嶅悓鐨勫伐鍘傜敓浜т笉鍚岀殑璁$畻鏈猴紝浣嗚綆楁満鐨勫熀鏈粍鎴?榪欓噷鍋囪璁$畻鏈轟粎鐢眗am鍜宑pu緇勬垚)鏄竴鏍風殑錛岃繖鏍風殑浜у搧緋誨垪寰堥傚悎鐢ㄦ娊璞″伐鍘傛潵緇勭粐銆?br />鑰屽湪瀹為檯鐢熶駭璁$畻鏈虹殑鏃跺欙紝createRam()鍜宑reateCpu()榪欎袱涓伐鍘傛柟娉曞張璧峰埌浜嗕綔鐢ㄣ?br />

]]>