• 设计模式享元模式实现C++


    /*********************************
    *设计模式--享元模式实现
    *C++语言
    *Author:WangYong
    *Blog:http://www.cnblogs.com/newwy
    ********************************/
    #include <iostream>
    #include <cassert>
    #include <vector>
    #include <string>
    using namespace std;
    class Flyweight
    {
    	public:
    	virtual ~Flyweight(){}
    	virtual void Operation(const string & extrinsicState){}
    	string GetIntrinsicState(){return this->_intrinsicState;}
    	protected:
    	Flyweight(string intrinsicState){this->_intrinsicState = _intrinsicState;}
    	private:
    	string _intrinsicState;
    };
    class ConcreteFlyweight:public Flyweight
    {
    	public:
    	ConcreteFlyweight(string intrinsicState):Flyweight(intrinsicState)
    	{cout<<"ConcreteFlyweight Build ...."<<intrinsicState<<endl;}
    	~ConcreteFlyweight(){}
    	void Operation(const string & extrinsicState)
    	{
    		cout<<"ConcreteFlyweight:内蕴"<<this->GetIntrinsicState()
    		<<"ConcreteFlyweight:外蕴"<<extrinsicState<<endl;
    	}
    };
    class FlyweightFactory
    {
    	public:
    	FlyweightFactory(){}
    	~FlyweightFactory(){}
    	Flyweight * GetFlyweight(const string &key)
    	{
    		vector<Flyweight *>::iterator it = _fly.begin();
    		for(;it != _fly.end(); it++)
    		{
    			if( (*it)->GetIntrinsicState() == key)
    				cout<<"already created by users..."<<endl;
    			return *it;
    		}
    		Flyweight *fn = new ConcreteFlyweight(key);
    		_fly.push_back(fn);
    		return fn;	
    	}
    	private:
    	vector<Flyweight*> _fly;
    };
    int main()
    {
    	FlyweightFactory *fc = new FlyweightFactory();
    	Flyweight * fw1 = fc->GetFlyweight("hello");
    	Flyweight * fw2 = fc->GetFlyweight("world!");
    	Flyweight * fw3 = fc->GetFlyweight("hello");
    }
    
    
  • 相关阅读:
    如何获取QQ的clientkey
    自动输入QQ密码
    Ext Tree 操作类
    QQ消息记录文件压缩方法
    见过最恶心的代码,发泄一下。。。
    今天尝试获取QQ的clientkey未果,做个记号
    用c# 调用并调试c++的代码
    托盘管理 隐藏/显示/单击/右键/双击/改变位置
    在XP下是可以查看进程命令行参数的
    充分利用你的“二脑”
  • 原文地址:https://www.cnblogs.com/newwy/p/1855224.html
Copyright © 2020-2023  润新知