mirror of
https://github.com/huihut/interview.git
synced 2025-12-18 04:54:39 +03:00
添加设计模式及例子,包括:单例、抽象工厂、适配器、桥接、观察者模式
This commit is contained in:
30
DesignPattern/BridgePattern/BridgeMain.cpp
Normal file
30
DesignPattern/BridgePattern/BridgeMain.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by xiemenghui on 2018/7/21.
|
||||
//
|
||||
|
||||
#include "BridgeMain.h"
|
||||
|
||||
void BridgeMain()
|
||||
{
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ơ<EFBFBD><C6A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD>
|
||||
IElectricalEquipment * light = new Light();
|
||||
IElectricalEquipment * fan = new Fan();
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>ء<EFBFBD><D8A1><EFBFBD>λ<EFBFBD><CEBB><EFBFBD>أ<EFBFBD>
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>غ͵<D8BA><CDB5>ƹ<EFBFBD><C6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD>غͷ<D8BA><CDB7>ȹ<EFBFBD><C8B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
ISwitch * pullChain = new PullChainSwitch(light);
|
||||
ISwitch * twoPosition = new TwoPositionSwitch(fan);
|
||||
|
||||
// <20><><EFBFBD>ơ<EFBFBD><C6A1>ص<EFBFBD>
|
||||
pullChain->On();
|
||||
pullChain->Off();
|
||||
|
||||
// <20><EFBFBD><F2BFAAB7>ȡ<EFBFBD><C8A1>رշ<D8B1><D5B7><EFBFBD>
|
||||
twoPosition->On();
|
||||
twoPosition->Off();
|
||||
|
||||
SAFE_DELETE(twoPosition);
|
||||
SAFE_DELETE(pullChain);
|
||||
SAFE_DELETE(fan);
|
||||
SAFE_DELETE(light);
|
||||
}
|
||||
17
DesignPattern/BridgePattern/BridgeMain.h
Normal file
17
DesignPattern/BridgePattern/BridgeMain.h
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by xiemenghui on 2018/7/21.
|
||||
//
|
||||
|
||||
#ifndef DESIGNPATTERN_BRIDGEMAIN_H
|
||||
#define DESIGNPATTERN_BRIDGEMAIN_H
|
||||
|
||||
#include "refined_abstraction.h"
|
||||
#include "concrete_implementor.h"
|
||||
|
||||
#ifndef SAFE_DELETE
|
||||
#define SAFE_DELETE(p) { if(p){delete(p); (p)=nullptr;} }
|
||||
#endif
|
||||
|
||||
void BridgeMain();
|
||||
|
||||
#endif //DESIGNPATTERN_BRIDGEMAIN_H
|
||||
23
DesignPattern/BridgePattern/abstraction.h
Normal file
23
DesignPattern/BridgePattern/abstraction.h
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Created by xiemenghui on 2018/7/21.
|
||||
//
|
||||
|
||||
#ifndef DESIGNPATTERN_ABSTRACTION_H
|
||||
#define DESIGNPATTERN_ABSTRACTION_H
|
||||
|
||||
#include "implementor.h"
|
||||
|
||||
// 开关
|
||||
class ISwitch
|
||||
{
|
||||
public:
|
||||
ISwitch(IElectricalEquipment *ee){ m_pEe = ee; }
|
||||
virtual ~ISwitch(){}
|
||||
virtual void On() = 0; // 打开电器
|
||||
virtual void Off() = 0; // 关闭电器
|
||||
|
||||
protected:
|
||||
IElectricalEquipment * m_pEe;
|
||||
};
|
||||
|
||||
#endif //DESIGNPATTERN_ABSTRACTION_H
|
||||
44
DesignPattern/BridgePattern/concrete_implementor.h
Normal file
44
DesignPattern/BridgePattern/concrete_implementor.h
Normal file
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// Created by xiemenghui on 2018/7/21.
|
||||
//
|
||||
|
||||
#ifndef DESIGNPATTERN_CONCRETE_IMPLEMENTOR_H
|
||||
#define DESIGNPATTERN_CONCRETE_IMPLEMENTOR_H
|
||||
|
||||
#include "implementor.h"
|
||||
#include <iostream>
|
||||
|
||||
// 电灯
|
||||
class Light : public IElectricalEquipment
|
||||
{
|
||||
public:
|
||||
// 开灯
|
||||
virtual void PowerOn() override
|
||||
{
|
||||
std::cout << "Light is on." << std::endl;
|
||||
}
|
||||
// 关灯
|
||||
virtual void PowerOff() override
|
||||
{
|
||||
std::cout << "Light is off." << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
// 风扇
|
||||
class Fan : public IElectricalEquipment
|
||||
{
|
||||
public:
|
||||
// 打开风扇
|
||||
virtual void PowerOn() override
|
||||
{
|
||||
std::cout << "Fan is on." << std::endl;
|
||||
}
|
||||
//关闭风扇
|
||||
virtual void PowerOff() override
|
||||
{
|
||||
std::cout << "Fan is off." << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //DESIGNPATTERN_CONCRETE_IMPLEMENTOR_H
|
||||
17
DesignPattern/BridgePattern/implementor.h
Normal file
17
DesignPattern/BridgePattern/implementor.h
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by xiemenghui on 2018/7/21.
|
||||
//
|
||||
|
||||
#ifndef DESIGNPATTERN_IMPLEMENTOR_H
|
||||
#define DESIGNPATTERN_IMPLEMENTOR_H
|
||||
|
||||
// 电器
|
||||
class IElectricalEquipment
|
||||
{
|
||||
public:
|
||||
virtual ~IElectricalEquipment(){}
|
||||
virtual void PowerOn() = 0; // 打开
|
||||
virtual void PowerOff() = 0; // 关闭
|
||||
};
|
||||
|
||||
#endif //DESIGNPATTERN_IMPLEMENTOR_H
|
||||
53
DesignPattern/BridgePattern/refined_abstraction.h
Normal file
53
DesignPattern/BridgePattern/refined_abstraction.h
Normal file
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// Created by xiemenghui on 2018/7/21.
|
||||
//
|
||||
|
||||
#ifndef DESIGNPATTERN_REFINED_ABSTRACTION_H
|
||||
#define DESIGNPATTERN_REFINED_ABSTRACTION_H
|
||||
|
||||
#include "abstraction.h"
|
||||
#include <iostream>
|
||||
|
||||
// 拉链式开关
|
||||
class PullChainSwitch : public ISwitch
|
||||
{
|
||||
public:
|
||||
PullChainSwitch(IElectricalEquipment *ee) : ISwitch(ee) {}
|
||||
|
||||
// 用拉链式开关打开电器
|
||||
virtual void On() override
|
||||
{
|
||||
std::cout << "Switch on the equipment with a pull chain switch." << std::endl;
|
||||
m_pEe->PowerOn();
|
||||
}
|
||||
|
||||
// 用拉链式开关关闭电器
|
||||
virtual void Off() override
|
||||
{
|
||||
std::cout << "Switch off the equipment with a pull chain switch." << std::endl;
|
||||
m_pEe->PowerOff();
|
||||
}
|
||||
};
|
||||
|
||||
// 两位开关
|
||||
class TwoPositionSwitch : public ISwitch
|
||||
{
|
||||
public:
|
||||
TwoPositionSwitch(IElectricalEquipment *ee) : ISwitch(ee) {}
|
||||
|
||||
// 用两位开关打开电器
|
||||
virtual void On() override
|
||||
{
|
||||
std::cout << "Switch on the equipment with a two-position switch." << std::endl;
|
||||
m_pEe->PowerOn();
|
||||
}
|
||||
|
||||
// 用两位开关关闭电器
|
||||
virtual void Off() override {
|
||||
std::cout << "Switch off the equipment with a two-position switch." << std::endl;
|
||||
m_pEe->PowerOff();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //DESIGNPATTERN_REFINED_ABSTRACTION_H
|
||||
Reference in New Issue
Block a user