C++C++ 编写GoFGoF设计模式里Lexi样例

本文通过C++代码示例展示了多种GoF设计模式的应用,包括桥接模式、组合模式、装饰者模式等,并实现了具体的设计模式如点击命令、抽象工厂等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++C++ 编写GoFGoF设计模式里Lexi样例 C++C++ 编写GoFGoF设计模式里Lexi样例 C++C++ 编写GoFGoF设计模式里Lexi样例 C++C++ 编写GoFGoF设计模式里Lexi样例 C++C++ 编写GoFGoF设计模式里Lexi样例 C++C++ 编写GoFGoF设计模式里Lexi样例C++C++ 编写GoFGoF设计模式里Lexi样例 C++C++ 编写GoFGoF设计模式里Lexi样例  C++C++ 编写GoFGoF设计模式里Lexi样例 C++C++ 编写GoFGoF设计模式里Lexi样例 C++C++ 编写GoFGoF设计模式里Lexi样例C++C++ 编写GoFGoF设计模式里Lexi样例 C++C++ 编写GoFGoF设计模式里Lexi样例 C++C++ 编写GoFGoF设计模式里Lexi样例

/////////////////////////////////////////////////////////////////////////////////

#pragma once
#include<string>
#include<iostream>
using namespace std;


namespace GUI
{


class WindowIMP
{
protected:
WindowIMP(void);
public:
~WindowIMP(void);
virtual string DrawnText()=0;
virtual void DrawText(string s)=0;
virtual void RemoveTextChars(int count)=0;
};


class ConcereteWindowIMP:public WindowIMP
{
private:
string StringBuilder;
public:
ConcereteWindowIMP()
{
StringBuilder="";
}
string DrawnText()
{
return this->StringBuilder;
}
void DrawText(string s)
{
this->StringBuilder=this->StringBuilder+s;
//printf("%s",StringBuilder);
cout<<StringBuilder<<endl;


}
void RemoveTextChars(int count)
{
this->StringBuilder.clear();
}
};




}




#include "StdAfx.h"
#include "WindowIMP.h"
namespace GUI
{


WindowIMP::WindowIMP(void)
{
}




WindowIMP::~WindowIMP(void)
{
}
}

///////////////////////////////////////////////////////








#pragma once
#include"WindowIMP.h"
#include <string>
using namespace std;


namespace GUI
{


class Window
{
protected:
WindowIMP *windowIMP;
Window(void);
public:
Window(WindowIMP *windowIMP);
~Window(void);
virtual string  DranwnText()const=0;
virtual void DrawText(string s)=0;
virtual void RomoveTextChars(int count)=0;
};
class TextWindow:public Window
{
public:
TextWindow(WindowIMP *windowIMP):Window(windowIMP){};
string  DranwnText() const
{
return this->windowIMP->DrawnText();
}
void DrawText(string s)
{
this->windowIMP->DrawText(s);
}
void RomoveTextChars(int count)
{
this->windowIMP->RemoveTextChars(count);
}
};


}







#include "StdAfx.h"
#include "Window.h"


namespace GUI
{
Window::Window(void)
{
}




Window::~Window(void)
{


}
Window::Window(WindowIMP *windowIMP)
{
this->windowIMP=windowIMP;
}


}



//////////////////////////////////////////////////////////



#pragma once
namespace GUI{


class Strategy
{
public:
Strategy(void);
~Strategy(void);
virtual void Compose()=0;
};


class ConcreteStrategy1:public Strategy
{
public:void Compose()
  {
  printf("Strategy1");
  }
};


class ConcreteStrategy2:public Strategy
{
public:void Compose()
  {
  printf("Strategy2");
  }
};

class ConcreteStrategy3:public Strategy
{
public:void Compose()
  {
  printf("Strategy3");
  }
};
}

#include "StdAfx.h"
#include "Strategy.h"


namespace GUI{


Strategy::Strategy(void)
{


}




Strategy::~Strategy(void)
{


}


}

//////////////////////////////////////////////////////////////////////////



// GoF.Lexi.Application.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include"Window.h"
#include"Glyph.h"
#include"command.h"
#include"Factory.h"
#include"Derecator.h"
#include"Strategy.h"


using namespace GUI;


/*
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
*/




/*
bridge 模式,通过一个间接接口实现定义与实现的分离
*/
/*
int _tmain(int argc, _TCHAR* argv[])
{
WindowIMP *windowIMP=new ConcereteWindowIMP();
    Window *window=new TextWindow(windowIMP);
window->DrawText("dfdf");
window->DrawText("fancong");
return 0;
}
*/




/*
   composite模式,主要利用递归思想进行组合
*/
/*
   composite模式,主要利用递归思想进行组合


int _tmain(int argc, _TCHAR* argv[])
{
WindowIMP *windowIMP=new ConcereteWindowIMP();
    Window *window=new TextWindow(windowIMP);
Glyph *glyph1=new Rectangle("fancong"); 
Glyph *glyph2=new Rectangle("yingying"); 
Row *row=new Row();
row->Insert(glyph1);
row->Insert(glyph2);
//row->Insert(row);

Glyph *glyph3=new Rectangle("Fan"); 
Glyph *glyph4=new Rectangle("Ying"); 
Row *row1=new Row();
row1->Insert(glyph3);
row1->Insert(glyph4);
row1->Insert(row);


row1->Draw(window);
return 0;
}
*/


/*
AbstractFactory抽象工厂


int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
*/


/*
Execute and Undo模拟command模式


int _tmain(int argc, _TCHAR* argv[])
{
WindowIMP *windowIMP=new ConcereteWindowIMP();
Window *window=new TextWindow(windowIMP);
Command *command=new ClickedCommand(window);
command->Execute();
command->Undo();
return 0;
}
*/




/*
Decorator模式 
int _tmain(int argc, _TCHAR* argv[])
{
WindowIMP *windowIMP=new ConcereteWindowIMP();
Window *window=new TextWindow(windowIMP);
Glyph *p1=new Rectangle("Rectangle!");
Glyph  *glyph=new Scroller(p1);
glyph->Draw(window);
return 0;
}
*/ 


/*
Strategy模式
 
int _tmain(int argc, _TCHAR* argv[])
{
WindowIMP *windowIMP=new ConcereteWindowIMP();
Window *window=new TextWindow(windowIMP);
Strategy *strategy=new ConcreteStrategy1();
Glyph *p1=new Rectangle(strategy);
p1->Draw(window);
return 0;
}
*/ 

//////////////////////////////////////////////////////////////////////////

#pragma once
#include "Window.h"
#include"Strategy.h"
#include<list>


namespace GUI
{
    class Glyph
   {
   protected:
  string value;
  Strategy *strategy;
public:
Glyph(void);
~Glyph(void);
Glyph(Strategy *strategy)
{
strategy=strategy;
}
virtual void Draw(Window *window)=0;
virtual void Insert(Glyph *glyph)=0;
};


class Rectangle:public Glyph
{
private:

public:
Rectangle(string s)
{
value=s;
}
Rectangle(Strategy *strategy):Glyph(strategy)
{
this->strategy=strategy;
}
void Draw(Window *window)
{
if(!strategy)
{
window->DrawText(value);
}
else
{
strategy->Compose();
}
}
void Insert(Glyph *glyph)
{
//nothing
}
};


class Row:public Glyph
{
private:
list<Glyph *> row_Data;


public:
void Draw(Window *window)
{
list<Glyph*>::iterator itr=row_Data.begin();
for(;itr!=row_Data.end();itr++)
{
(*itr)->Draw(window);
}
}
void Insert(Glyph *glyph)
{
row_Data.push_back(glyph);
}
};


#include "StdAfx.h"
#include "Glyph.h"


namespace GUI
{
Glyph::Glyph(void)
{
}




Glyph::~Glyph(void)
{
}
}






////////////////////////

#pragma once
#include"Glyph.h"
#include"command.h"


namespace GUI
{
class Button
{

private:
Command *command;
public:
Button(Command*command)
{
this->command=command;
}
void Click()
{
command->Execute();
}
};
class MacButton:public Button
{
public:
MacButton();
};


class GUIAbstractFactory
{
public:
virtual Button *Operation()=0;


};
class GUIConcreteFactory:public GUIAbstractFactory
{
Button *Operation()
{
return new MacButton();
}
};












class AbstrctFactory
{
public:
AbstrctFactory(void);
~AbstrctFactory(void);
};


}






#include "StdAfx.h"
#include "Factory.h"


namespace GUI
{
AbstrctFactory::AbstrctFactory(void)
{
}




AbstrctFactory::~AbstrctFactory(void)
{
}
}
































}

/////////////////////////////////////////////////

#pragma once
#include "Glyph.h"


namespace GUI{
class Derecator
{
public:
Derecator(void);
~Derecator(void);
};


class MomoGlyph:public Glyph
{
private :
string value;

public:
MomoGlyph(Glyph *glyph)
{
value="MomoGlyph";
this->glyph=glyph;
}

void Draw(Window *window)
{
glyph->Draw(window);
}
void Insert(Glyph *glyph)
{
}
protected:
Glyph *glyph;
};


class Border:public MomoGlyph{
public:
Border(Glyph *glyph):MomoGlyph(glyph)
{
}
void AddBehaviourBorder()
{
printf("Add Border Behaviours!");
}
void Draw(Window *window)
{
glyph->Draw(window);
AddBehaviourBorder();
}


};
class Scroller:public MomoGlyph{
public:
Scroller(Glyph *glyph):MomoGlyph(glyph)
{
}
void Draw(Window *window)
{
glyph->Draw(window);
AddBehaviourScroller();
}
void AddBehaviourScroller()
{
printf("Add Scroller Behaviours!");
}
};


}


#include "StdAfx.h"
#include "Derecator.h"


namespace GUI{
Derecator::Derecator(void)
{
}




Derecator::~Derecator(void)
{
}
}

///////////////////////////////////////////////////////////////////


#pragma once
#include<list>
#include"Window.h"
namespace GUI
{
class Command
{
protected:
std::list<Command *> commands;
public:
virtual void Execute()=0;
virtual void UnExecute()=0;
void Undo()
{
if(commands.begin()!=commands.end())
{
commands.pop_back();
}
}
Command(void);
~Command(void);
};


class ClickedCommand:public Command
{
private:
Window *window;
public:
void Execute()
{
window->DrawText("Clicked");
commands.push_back(this);
}
void UnExecute()
{
window->RomoveTextChars(6);
commands.pop_back();
}


ClickedCommand(Window *window)
{
this->window=window;
}
};
}




#include "StdAfx.h"
#include "command.h"


namespace GUI
{
Command::Command(void)
{
}




Command::~Command(void)
{
}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值