mirror of
https://github.com/huihut/interview.git
synced 2025-12-18 13:04:38 +03:00
添加设计模式及例子,包括:单例、抽象工厂、适配器、桥接、观察者模式
This commit is contained in:
21
DesignPattern/AdapterPattern/AdapterMain.h
Normal file
21
DesignPattern/AdapterPattern/AdapterMain.h
Normal file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by xiemenghui on 2018/7/20.
|
||||
//
|
||||
|
||||
#ifndef DESIGNPATTERN_ADAPTERMAIN_H
|
||||
#define DESIGNPATTERN_ADAPTERMAIN_H
|
||||
|
||||
#include "adapter.h"
|
||||
|
||||
void AdapterMain()
|
||||
{
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
IRussiaSocket * pAdapter = new PowerAdapter();
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>
|
||||
pAdapter->Charge();
|
||||
|
||||
SAFE_DELETE(pAdapter);
|
||||
}
|
||||
|
||||
#endif //DESIGNPATTERN_ADAPTERMAIN_H
|
||||
20
DesignPattern/AdapterPattern/adaptee.h
Normal file
20
DesignPattern/AdapterPattern/adaptee.h
Normal file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by xiemenghui on 2018/7/20.
|
||||
//
|
||||
|
||||
#ifndef DESIGNPATTERN_ADAPTEE_H
|
||||
#define DESIGNPATTERN_ADAPTEE_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// 自带的充电器(两脚扁型)
|
||||
class OwnCharger
|
||||
{
|
||||
public:
|
||||
void ChargeWithFeetFlat()
|
||||
{
|
||||
std::cout << "OwnCharger::ChargeWithFeetFlat\n";
|
||||
}
|
||||
};
|
||||
|
||||
#endif //DESIGNPATTERN_ADAPTEE_H
|
||||
34
DesignPattern/AdapterPattern/adapter.h
Normal file
34
DesignPattern/AdapterPattern/adapter.h
Normal file
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// Created by xiemenghui on 2018/7/20.
|
||||
//
|
||||
|
||||
#ifndef DESIGNPATTERN_ADAPTER_H
|
||||
#define DESIGNPATTERN_ADAPTER_H
|
||||
|
||||
#include "target.h"
|
||||
#include "adaptee.h"
|
||||
|
||||
#ifndef SAFE_DELETE
|
||||
#define SAFE_DELETE(p) { if(p){delete(p); (p)=NULL;} }
|
||||
#endif
|
||||
|
||||
// 电源适配器
|
||||
class PowerAdapter : public IRussiaSocket
|
||||
{
|
||||
public:
|
||||
PowerAdapter() : m_pCharger(new OwnCharger()){}
|
||||
~PowerAdapter()
|
||||
{
|
||||
SAFE_DELETE(m_pCharger);
|
||||
}
|
||||
void Charge()
|
||||
{
|
||||
// 使用自带的充电器(两脚扁形)充电
|
||||
m_pCharger->ChargeWithFeetFlat();
|
||||
}
|
||||
private:
|
||||
// 持有需要被适配的接口对象(自带的充电器)
|
||||
OwnCharger* m_pCharger;
|
||||
};
|
||||
|
||||
#endif //DESIGNPATTERN_ADAPTER_H
|
||||
16
DesignPattern/AdapterPattern/target.h
Normal file
16
DesignPattern/AdapterPattern/target.h
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by xiemenghui on 2018/7/20.
|
||||
//
|
||||
|
||||
#ifndef DESIGNPATTERN_TARGET_H
|
||||
#define DESIGNPATTERN_TARGET_H
|
||||
|
||||
// 俄罗斯提供的插座
|
||||
class IRussiaSocket
|
||||
{
|
||||
public:
|
||||
// 使用双脚圆形充电(暂不实现)
|
||||
virtual void Charge() = 0;
|
||||
};
|
||||
|
||||
#endif //DESIGNPATTERN_TARGET_H
|
||||
Reference in New Issue
Block a user