设计模式——6.装饰模式
Posted sylvan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式——6.装饰模式相关的知识,希望对你有一定的参考价值。
装饰模式(Decorator)
动态的为一个对象增加额外的职责,为对象增加功能时,使用装饰模式比单纯的子类继承要更加的灵活。
装饰模式结构图:
C++
BaseClass对象抽象基类:
//file: BaseClass.h
#pragma once
class BaseClass
{
public:
BaseClass();
virtual ~BaseClass();
virtual void operation();
};
//file: BaseClass.cpp
#include "pch.h"
#include "BaseClass.h"
BaseClass::BaseClass() {}
BaseClass::~BaseClass() {}
void BaseClass::operation() {}
ConcreteClass具体对象实现类:
//file: ConcreteClass.h
#pragma once
#include "BaseClass.h"
class ConcreteClass : public BaseClass
{
public:
ConcreteClass();
virtual ~ConcreteClass();
void operation();
};
//file: ConcreteClass.cpp
#include "pch.h"
#include "ConcreteClass.h"
#include <iostream>
ConcreteClass::ConcreteClass() {}
ConcreteClass::~ConcreteClass() {}
void ConcreteClass::operation()
{
std::cout << "ConcreteComponent's normal operation!" << std::endl;
}
Decorator装饰抽象类:
//file: Decorator.h
#pragma once
#include "BaseClass.h"
class Decorator : public BaseClass
{
public:
Decorator(BaseClass *pcom);
virtual ~Decorator();
void operation();
private:
BaseClass *m_BaseClass;
};
//file: Decorator.cpp
#include "pch.h"
#include "Decorator.h"
Decorator::Decorator(BaseClass *pcom) : m_BaseClass(pcom) {}
Decorator::~Decorator() {}
void Decorator::operation()
{
m_BaseClass->operation();
}
ConcreteDecoratorA具体装饰是实现类:
//file: ConcreteDecoratorA.h
#pragma once
#include "Decorator.h"
class ConcreteDecoratorA : public Decorator
{
public:
ConcreteDecoratorA(BaseClass *pcom);
virtual ~ConcreteDecoratorA();
virtual void operation();
void addBehavior();
};
//file: ConcreteDecoratorA.cpp
#include "pch.h"
#include "ConcreteDecoratorA.h"
#include <iostream>
ConcreteDecoratorA::ConcreteDecoratorA(BaseClass *pcom) : Decorator(pcom) {}
ConcreteDecoratorA::~ConcreteDecoratorA() {}
void ConcreteDecoratorA::addBehavior()
{
std::cout << "addBehavior AAAA" << std::endl;
}
void ConcreteDecoratorA::operation()
{
Decorator::operation();
addBehavior();
}
客户端代码:
//file: DecoratorPattern.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include "pch.h"
#include "ConcreteClass.h"
#include "ConcreteDecoratorA.h"
#include "ConcreteDecoratorB.h"
#include <iostream>
using namespace std;
int main()
{
ConcreteClass *p = new ConcreteClass();
BaseClass *pa = new ConcreteDecoratorA(p);
pa->operation();
BaseClass *pb = new ConcreteDecoratorB(pa);
pb->operation();
delete p;
delete pa;
delete pb;
return 0;
}
CSharp
BaseClass对象抽象基类:
public abstract class BaseClass
{
public abstract void Operation();
}
ConcreteClass具体对象实现类:
public class ConcreteClass : BaseClass
{
public override void Operation()
{
Console.WriteLine("Concrete Base Operation .");
}
}
Decorator装饰抽象类:
public abstract class Decorator : BaseClass
{
protected BaseClass m_BaseClass;
public Decorator(BaseClass baseClass)
{
m_BaseClass = baseClass;
}
public override void Operation()
{
if (m_BaseClass != null)
m_BaseClass.Operation();
}
}
ConcreteDecoratorA具体装饰是实现类:
public class ConcreteDecoratorA : Decorator
{
public ConcreteDecoratorA(BaseClass baseClass) : base(baseClass)
{
}
public void AddBehavior()
{
Console.WriteLine("ConcreteDecoratorA 's Decorator Behavior .");
}
public override void Operation()
{
base.Operation();
AddBehavior();
}
}
客户端代码:
class Program
{
static void Main(string[] args)
{
ConcreteClass p = new ConcreteClass();
BaseClass pa = new ConcreteDecoratorA(p);
pa.Operation();
BaseClass pb = new ConcreteDecoratorB(pa);
pb.Operation();
Console.ReadKey(false);
}
}
以上是关于设计模式——6.装饰模式的主要内容,如果未能解决你的问题,请参考以下文章