disign2

海前 王 - Aug 27 - - Dev Community

// ConsoleApplication30.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

include

include

using namespace std;
//
class fontFactory;
class font
{
private:
std::string key;
public:
font(const std::string& key ):key(key)
{

}
Enter fullscreen mode Exit fullscreen mode

};

class fontFactory
{
map footpool;
public:
font * getfont(const string &key)
{
map::iterator it = footpool.find(key);

    if (it != footpool.end())
    {
        return footpool[key];

    }
    else
    {
        font* font = new ::font(key);
        footpool[key] = font;
        return font;
    }
}
Enter fullscreen mode Exit fullscreen mode

};
int main()
{
std::cout << "Hello World!\n";
}

// expressionPattern.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

include

include

include

using namespace std;

class expression
{
public:
virtual int interperet(std::map var)
{};

private:
};

class varexpression : public expression
{
string m_key;

public:
varexpression(string key): m_key(key)
{
}

virtual int interperet(std::map<std::string, int> var)
{
    return var[m_key];
}
Enter fullscreen mode Exit fullscreen mode

};

class symbolExpression : public expression
{
public:
expression* left;
expression* right;

symbolExpression(expression* l, expression* r):
    left(l), right(r)
{
}

expression* getleft() { return left; }
expression* getright() { return right; }
Enter fullscreen mode Exit fullscreen mode

};

class addExpression : public symbolExpression
{
public:
addExpression(expression* l, expression* r) : symbolExpression(l, r)
{
}
};

class subExpression : public symbolExpression
{
public:
subExpression(expression* l, expression* r) : symbolExpression(l, r)
{
}
};

class calculator
{
public:
expression* m_expression;

calculator(string ex)
{
    m_expression = nullptr;
    stack<expression*> strexp;
    expression* right;
    expression* left;
    for (int i = 0; ex.length(); i++)
    {
        switch (ex[i])
        {
        case '+':
            left = strexp.top();
            strexp.pop();
            right = new varexpression(ex.substr(++i, 1));
            strexp.push(new addExpression(left, right));
            break;
        case '-':
            left = strexp.top();
            strexp.pop();
            right = new varexpression(ex.substr(++i, 1));
            strexp.push(new subExpression(left, right));
            break;

        default:
            strexp.push(new varexpression(ex.substr(i, 1)));
        }
    }
    if(!strexp.empty())
    {
        //m_expression = strexp.pop();
        strexp.pop();
    }

}

int runing(map<string, int>& val)
{
    return (m_expression == nullptr) ? 0 : m_expression->interperet(val);
}
Enter fullscreen mode Exit fullscreen mode

};

int main()
{
}
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

include

include

include

using namespace std;

struct ShareState
{
string m_brand;
string m_model;
string m_color;

ShareState(const string& brand,
           const string model, const string color):m_brand(brand),m_model(model),m_color(color)
{
}

friend ostream& operator<<(ostream& os, const ShareState& ss)
{
    return os << ss.m_brand <<"\t"<< ss.m_color << "\t"<<ss.m_model<<"\n";
}
Enter fullscreen mode Exit fullscreen mode

};

struct uniqueState
{
string m_owner;
string m_plate;

uniqueState(const string& owner, const string& plate)
    : m_owner(owner), m_plate(plate)
{
}

//toString
friend ostream& operator<<(ostream& os, const uniqueState& ss)
{
    return os << ss.m_owner <<"\t"<< ss.m_plate<<"\n";
}
Enter fullscreen mode Exit fullscreen mode

};

//flyweight
class flyweight
{
private:
ShareState m_shareState;

public:
flyweight(const ShareState shares_state)
: m_shareState(shares_state)
{
}

void operation(uniqueState unique)
{
    cout <<"\n"<<"waibu" << "\t" <<m_shareState << "neibu" << unique;
}
Enter fullscreen mode Exit fullscreen mode

};

class FlyweightFactory
{
unordered_map m_flyweigt;

public:
string getKey(const ShareState& ss)
{
return ss.m_brand + " -" + ss.m_color + "-" + ss.m_model;
}

FlyweightFactory(initializer_list<ShareState> share_states)
{
    for (auto& ss : share_states)
    {
        m_flyweigt.insert({getKey(ss), flyweight(ss)});
    }
}

flyweight* getFlyweight(const ShareState& share_state)
{
    string key = getKey(share_state);
    if (m_flyweigt.find(key) == m_flyweigt.end())
    {
        std::cout << "not find";
        m_flyweigt.insert({key, share_state});
    }
    else
    {
        cout << "find";
    }

    return &m_flyweigt.at(key);
}

void listflyw() const
{
    int count = m_flyweigt.size();
    std::cout << "has" <<count;
    for (pair<string, flyweight> it : m_flyweigt)
    {
        cout << it.first << "\n";
    }
}
Enter fullscreen mode Exit fullscreen mode

};

class CarInfo
{
private:
flyweight* m_flyweight_ = nullptr;
uniqueState* m_unique_state_;

public:
CarInfo(flyweight* flyweight, uniqueState* unique_state)
: m_flyweight_(flyweight), m_unique_state_(unique_state)
{
}

void operation()
{
    m_flyweight_->operation(*m_unique_state_);
}
Enter fullscreen mode Exit fullscreen mode

};

include

class policeDatabase
{
private:
list car_infos_;
public:
~policeDatabase()
{
for (auto item : car_infos_)
{
delete item;
}
}

void addcartoPolicedatabase(FlyweightFactory& ff, const string& woner, const string& plate, const string& brand,
                            const string& color, const string& model)
{
    flyweight* flyweight = ff.getFlyweight({brand, model, color});
    uniqueState unique_state(woner, plate);
    car_infos_.push_back(new CarInfo(flyweight, &unique_state));
    for (auto item : car_infos_)

        item->operation();
}
Enter fullscreen mode Exit fullscreen mode

};

int main()
{
FlyweightFactory factory({
ShareState("an", "ax", "red"),
ShareState("ax", "ddd", "ff")
});
cout << "\n----------------------------------\n";
factory.listflyw();
cout << "\n----------------------------------\n";

policeDatabase database;
database.addcartoPolicedatabase(factory, "xx", "xx", "xx", "xx", "xx");
cout << "\n----------------------------------\n";

factory.listflyw();
cout << "\n----------------------------------\n";

database.addcartoPolicedatabase(factory, "yy", "yy", "an", "ay", "red");
cout << "\n----------------------------------\n";

factory.listflyw();
cout << "\n----------------------------------\n";

database.addcartoPolicedatabase(factory, "zz", "zz", "an", "ay", "red");
cout << "\n----------------------------------\n";

factory.listflyw();
Enter fullscreen mode Exit fullscreen mode

}

// ConsoleApplication2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
/**

include

class a
{
public:

};

class b:public a
{
public:

};

int main()
{
int* p;
if(typeid(int*)==typeid(p))
{
std::cout << "equal";
}
a x;
a y;
if(typeid(x)!=typeid(y))
{

    std::cout << "not ii";
}
Enter fullscreen mode Exit fullscreen mode

}*/

include

using namespace std;

class A;
class B;
class C;
class D;
class E;

class Base
{
public:
class IVisitor
{
public:
virtual void visit(A*) = 0;
virtual void visit(B*) = 0;
virtual void visit(C*) = 0;
virtual void visit(D*) = 0;
virtual void visit(E*) = 0;
};

virtual void say() = 0;
virtual void accept(IVisitor*) = 0;

virtual ~Base()
{
}
Enter fullscreen mode Exit fullscreen mode

};

class A : public Base
{
public:
void say() override
{
cout << "Class A" << endl;
}

void accept(IVisitor* vis) override
{
    vis->visit(this); // 匹配数据操作类中的重载函数
}
Enter fullscreen mode Exit fullscreen mode

};

class B : public Base
{
public:
void say() override
{
cout << "Class B" << endl;
}

void accept(IVisitor* vis) override
{
    vis->visit(this);
}
Enter fullscreen mode Exit fullscreen mode

};

class C : public Base
{
public:
void say() override
{
cout << "Class C" << endl;
}

void accept(IVisitor* vis) override
{
    vis->visit(this);
}
Enter fullscreen mode Exit fullscreen mode

};

class D : public Base
{
public:
void say() override
{
cout << "Class D" << endl;
}

void accept(IVisitor* vis) override
{
    vis->visit(this);

}
Enter fullscreen mode Exit fullscreen mode

};

class E : public Base
{
public:
void say() override
{
cout << "Class E" << endl;
}

void accept(IVisitor* vis) override
{
    vis->visit(this);
}
Enter fullscreen mode Exit fullscreen mode

};

class calcVisitor : public Base::IVisitor
{
// 数据操作类
public:
calcVisitor(int& x) : x(x)
{
}

void visit(A* obj) override
{
    x *= 2;
}

void visit(B* obj) override
{
    x -= 3;
}

void visit(C* obj) override
{
    x += 6;
}

void visit(D* obj) override
{
    x /= 3;
}

void visit(E* obj) override
{
    x -= 2;
}
Enter fullscreen mode Exit fullscreen mode

private:
int& x;
};

int main()
{
// 访问者模式实现, 类型->功能(补充知识: 解决新增功能维护难——通过让编译器提示改代码)
srand(time(0));
Base* p[5] = {nullptr};
for (int i = 0; i < 5; ++i)
{
// 第一次类指针类型转换
switch (rand() % 5)
{
case 0: p[i] = new A();
break;
case 1: p[i] = new B();
break;
case 2: p[i] = new C();
break;
case 3: p[i] = new D();
break;
case 4: p[i] = new E();
break;
}
}
int x = 1;
calcVisitor vis(x);
for (int i = 0, pre = x; i < 5; ++i)
{
p[i]->accept(&vis); // 第二次类指针类型转换
p[i]->say();
cout << pre << " -> " << x << endl;
pre = x;
}
return 0;
}

//// ConsoleApplication31.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
////
//
//#include
//class a
//{
//public:
// a(int m,int n):i(m),j(n){}
// int i;
// int j;
// void print()
// {
// std::cout << "Hello World!\n";
//
// }
//};
//void fun(std::shared_ptr f)
//{
//}
//
//int main()
//{
// std::unique_ptr
f(new a(2, 4));
// f->print();
// fun(std::make_shared
(3, 4));
// std::shared_ptr
dd = std::make_shared(2, 3);
// std::shared_ptr
ff = dd;
//
// std::cout << (*ff).i;
//}
//
//// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
//// 调试程序: F5 或调试 >“开始调试”菜单
//
//// 入门使用技巧:
//// 1. 使用解决方案资源管理器窗口添加/管理文件
//// 2. 使用团队资源管理器窗口连接到源代码管理
//// 3. 使用输出窗口查看生成输出和其他消息
//// 4. 使用错误列表窗口查看错误
//// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。

include

include

include

class AbstractHero {
public:
virtual void showStatus() = 0;
int mHp, mMp, mAt, mDf;
};

class HeroA :public AbstractHero {
public:
HeroA() {
mHp = 0, mMp = 0, mAt = 0, mDf = 0;
}
virtual void showStatus() {
std::cout << "血量:" << mHp << std::endl;
std::cout << "魔法:" << mMp << std::endl;
std::cout << "攻击:" << mAt << std::endl;
std::cout << "防御:" << mDf << std::endl;
}
};

//英雄穿上某个装饰物,那么也还是一个英雄
class AbstractEquipment : public AbstractHero {
public:
AbstractEquipment(AbstractHero* hero) {
pHero = hero;
}
virtual void showStatus() {}
AbstractHero* pHero;
};

//给英雄穿上狂徒铠甲
class KuangtuEquipment :public AbstractEquipment {
public:
KuangtuEquipment(AbstractHero* hero) :AbstractEquipment(hero) {}
//增加新的功能
void AddKuangtu() {
std::cout << "新英雄穿上狂徒之后" << std::endl;
this->mHp = this->pHero->mHp;
this->mMp = this->pHero->mMp;
this->mAt = this->pHero->mAt;
this->mDf = this->pHero->mDf + 30;
}
virtual void showStatus() {
AddKuangtu();
std::cout << "血量:" << mHp << std::endl;
std::cout << "魔法:" << mMp << std::endl;
std::cout << "攻击:" << mAt << std::endl;
std::cout << "防御:" << mDf << std::endl;
}
};

int main() {

AbstractHero* hero = new HeroA;
hero->showStatus();
std::cout << "------------------------" << std::endl;
//给英雄穿上狂徒
hero = new KuangtuEquipment(hero);
hero->showStatus();

return 0;
Enter fullscreen mode Exit fullscreen mode

}

// ConsoleApplication32.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

include

include

include

include

include

using namespace std;

//
class Company
{
public:
virtual string getInfo() = 0;

virtual string getName()
{
    return name;
}

virtual string getType() = 0;
Enter fullscreen mode Exit fullscreen mode

protected:
string name;
string position;
int salary;
};

class ConcreteCompany : public Company
{
public:
ConcreteCompany(string name, string position, int salary)
{
this->name = name;
this->position = position;
this->salary = salary;
}

void add(Company* company)
{
    shared_ptr<Company> temp(company);
    companyList.push_back(temp);
}

string getType()
{
    return "ConcreteCompany";
}

void remove(string companyName)
{
    list<shared_ptr<Company>>::iterator iter = companyList.begin();
    for (; iter != companyList.end(); iter++)
    {
        if ((*iter).get()->getName() == companyName)
        {
            companyList.erase(iter);
            return;
        }
    }
}

list<shared_ptr<Company>> getChildList()
{
    return companyList;
}

string getInfo()
{
    string info = "";
    info = "名称:" + this->name;
    info = info + "\t职位:" + this->position;
    info = info + "\t薪水:" + to_string(this->salary);
    return info;
}
Enter fullscreen mode Exit fullscreen mode

private:
list> companyList;
};

class Employee : public Company
{
public:
Employee(string name, string position, int salary)
{
this->name = name;
this->position = position;
this->salary = salary;
}

string getType()
{
    return "Employee";
}

string getInfo()
{
    string info = "";
    info = "名称:" + this->name;
    info = info + "\t职位:" + this->position;
    info = info + "\t薪水:" + to_string(this->salary);
    return info;
}
Enter fullscreen mode Exit fullscreen mode

};

void disPlay(ConcreteCompany* root)
{
cout << root->getInfo() << endl;
list> tmp = root->getChildList();
list>::iterator iter = tmp.begin();
for (; iter != tmp.end(); iter++)
{
if ((iter).get()->getType() == string("Employee"))
{
cout << (*iter).get()->getInfo() << endl;
}
else
{
//递归
disPlay((ConcreteCompany
)(*iter).get());
}
}
}

int main()
{
ConcreteCompany* root = new ConcreteCompany("张三", "CEO", 100000);
ConcreteCompany* develop = new ConcreteCompany("李四", "研发组长", 100000);
ConcreteCompany* sale = new ConcreteCompany("王二", "销售组长", 100000);
Employee* e1 = new Employee("A", "研发", 200);
Employee* e2 = new Employee("B", "销售", 200);
Employee* e3 = new Employee("C", "研发", 200);
Employee* e4 = new Employee("D", "销售", 200);
root->add(develop);
root->add(sale);
develop->add(e1);
develop->add(e3);
sale->add(e2);
sale->add(e4);
disPlay(root);
develop->remove("A");
disPlay(root);
delete root;
root = NULL;
}

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player