8-9设计模式复习

/* 设计模式 23种  */
/* 开放封闭原则: 类的改动 是通过增加代码来实现的  不是修改源代码 */
/* 比如一个类 实现了各种功能 但是后期想修改某个功能就很难 那我们把它写成纯虚函数 让继承的类去实现 */

#include <iostream>
#include <windows.h>

using namespace std;

/*class Bankworker//一开始的想法 一个类实现多个操作
{
public:
    void savemoney()
    {
        cout << " savemoney " << endl;
    }
    void getmoney()
    {
        cout << " getmoney " << endl;
    }
    void transformmoney()
    {
        cout << " transformmoney " << endl;
    }
};*/

class Bankworker//把这个类函数变成 纯虚函数
{
public:
    /*virtual void savemoney() = 0;
    
    virtual void getmoney() = 0;
    
    virtual void transformmoney() = 0;*/
    virtual void work() = 0;
    
};

class Savework : public Bankworker//继承的类进行实现 savemoney
{
public:
    void work()
    {
        cout << " savemoney " << endl;
    }

};

class Getwork : public Bankworker//继承的类进行实现 getmoney
{
public:
    void work()
    {
        cout << " getmoney " << endl;
    }

};

class transformwork : public Bankworker//继承的类进行实现 transformmoney
{
public:
    void work()
    {
        cout << " transformmoney " << endl;
    }

};

int main()
{
    /*Bankworker *b1 = new Bankworker;

    b1->getmoney();
    b1->savemoney();*/

    Bankworker *b1 = NULL;
    b1 = new Savework;
    b1->work();

    b1 = new Getwork;
    b1->work();

    b1 = new transformwork;
    b1->work();

    system("pause");
    return 0;
}

/* 依赖倒置原则 : */

/*
#include <iostream>
#include <windows.h>

using namespace std;

class Ram2G
{
public:
	void Ramwork()
	{
		cout << "Ramwork 2G" << endl;
	}
};

class Rom32G
{
public:
	void Romwork()
	{
		cout << "Romwork 32G" << endl;
	}
};


class Phone//依赖于实现
{
private:
	Ram2G  *Ra;
	Rom32G *Ro;
public:
	Phone(Ram2G *a, Rom32G *b)
	{
		Ra = a;
		Ro = b;
	}
	void Phonework()
	{
		Ra->Ramwork();
		Ro->Romwork();
	}

};

int main()
{
	Ram2G *Ra2 = new Ram2G;
	Rom32G *Ro32 = new Rom32G;
	Phone *p = new Phone(Ra2, Ro32);
	p->Phonework();
	
	system("pause");
	return 0;
}  
*/

#include <iostream>
#include <windows.h>

using namespace std;

class Ram
{
public:
	virtual void Ramwork() = 0;
};
class Ram2G : public Ram
{
public:
	void Ramwork()
	{
		cout << "Ramwork 2G" << endl;
	}
};
class Ram4G : public Ram
{
public:
	void Ramwork()
	{
		cout << "Ramwork 4G" << endl;
	}
};

class Rom
{
public:
	virtual void Romwork() = 0;
};
class Rom32G : public Rom
{
public:
	void Romwork()
	{
		cout << "Romwork 32G" << endl;
	}
};
class Rom64G : public Rom
{
public:
	void Romwork()
	{
		cout << "Romwork 64G" << endl;
	}
};


class Phone
{
private:
	Ram  *Ra;
	Rom *Ro;
public:
	Phone(Ram *a, Rom *b)
	{
		Ra = a;
		Ro = b;
	}
	void Phonework()
	{
		Ra->Ramwork();
		Ro->Romwork();
	}
};

int main()
{
	/*Ram *Ra = new Ram2G;
	Rom *Ro = new Rom32G;
	Phone *p = new Phone(Ra, Ro);
	p->Phonework();
	delete Ra;
	delete Ro;
	delete p;*/

	Ram *Ra1 = new Ram4G;
	Rom *Ro1 = new Rom64G;
	Phone *p1 = new Phone(Ra1, Ro1);
	p1->Phonework();
	delete Ra1;
	delete Ro1;
	delete p1;

	system("pause");
	return 0;
}

/* 工厂模式 */

#include <iostream>
#include <windows.h>
#include <stdlib.h>

using namespace std;

class car
{
public:
	virtual void show() = 0;
};

class benz : public car
{
public:
	void show()
	{
		cout << "benz" << endl;
	}
};

class baoma : public car
{
public:
	void show()
	{
		cout << "baoma" << endl;
	}
};

class carfactory
{
public:
	virtual car * showcar() = 0;
};

class factorybaoma :public carfactory
{
public:
	car * showcar()
	{
		return new baoma;
	}
};

class factorybenz :public carfactory
{
public:
	car * showcar()
	{
		return new benz;
	}
};

int main()
{
	carfactory *f = new factorybaoma;
	f->showcar()->show();

	delete f;
	
	system("pause");
	return 0;
}

/* 抽象工厂 */

#include <iostream>
#include <windows.h>
#include <stdlib.h>

using namespace std;

class fruit
{
public :
	virtual void show() = 0;
};

class Northapple : public fruit
{
public:
	void show()
	{
		cout << " Northapple " << endl;
	}
};

class Northpear : public fruit
{
public:
	void show()
	{
		cout << " Northpear " << endl;
	}
};

class Sorthapple : public fruit
{
public:
	void show()
	{
		cout << " Sorthapple " << endl;
	}
};

class Sorthpear : public fruit
{
public:
	void show()
	{
		cout << " Sorthpear " << endl;
	}
};

class factory
{
public:
	virtual fruit *createapple() = 0;
	virtual fruit *createpear() = 0;
};

class Northfactory : public factory
{
public:
	fruit *createapple()
	{
		return new Northapple;
	}
	fruit *createpear()
	{
		return new Northpear;
	}
};

class Sorthfactory : public factory
{
public:
	fruit *createapple()
	{
		return new Sorthapple;
	}
	fruit *createpear()
	{
		return new Sorthpear;
	}
};

int main()
{
	factory *n1 = new Northfactory;
	n1->createapple()->show();
	n1->createpear()->show();
	delete n1;

	factory *n2 = new Sorthfactory;
	n2->createapple()->show();
	n2->createpear()->show();
	delete n2;

	system("pause");
	return 0;
}

/* 建造者模式 */

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <string>

using namespace std;

class house
{
private:
	string wall;
	string door;
	string window;
public:
	void setwall(string w)
	{
		wall = w;
	}
	void setdoor(string d)
	{
		door = d;
	}
	void setwindow(string wi)
	{
		window = wi;
	}

	string getwall()
	{
		return wall;
	}
	string getdoor()
	{
		return door;
	}
	string getwindow()
	{
		return window;
	}
};

class builder
{
private:
	house *h;
public:
	builder()
	{
		h = new house;
	}


	void constuctor()
	{
		h->setdoor("door");
		h->setwall("wall");
		h->setwindow("window");
	}

	void getinfo()
	{
		cout << h->getdoor() << endl;
		cout << h->getwall() << endl;
		cout << h->getwindow() << endl;
	}
};


int main()
{
	builder *b1 = new builder;
	b1->constuctor();
	b1->getinfo();

	system("pause");
	return 0;
}

/* 建造者模式改良版 */

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <string>

using namespace std;

class house
{
private:
	string wall;
	string door;
	string window;
public:
	void setwall(string w)
	{
		wall = w;
	}
	void setdoor(string d)
	{
		door = d;
	}
	void setwindow(string wi)
	{
		window = wi;
	}

	string getwall()
	{
		return wall;
	}
	string getdoor()
	{
		return door;
	}
	string getwindow()
	{
		return window;
	}
};

class builder
{
private:
	house *h;
public:
	virtual void constuctor() = 0;
	virtual void getinfo() = 0;
};

class buildernormal : public builder
{
private:
	house *h;
public:
	buildernormal()
	{
		h = new house;
	}


	void constuctor()
	{
		h->setdoor("doornormal");
		h->setwall("wallnormal");
		h->setwindow("windownormal");
	}

	void getinfo()
	{
		cout << h->getdoor() << endl;
		cout << h->getwall() << endl;
		cout << h->getwindow() << endl;
	}
};

class buildervilla : public builder
{
private:
	house *h;
public:
	buildervilla()
	{
		h = new house;
	}


	void constuctor()
	{
		h->setdoor("doorvilla");
		h->setwall("wallvilla");
		h->setwindow("windowvilla");
	}

	void getinfo()
	{
		cout << h->getdoor() << endl;
		cout << h->getwall() << endl;
		cout << h->getwindow() << endl;
	}
};


int main()
{
	builder *b = new buildernormal;

	b->constuctor();
	b->getinfo();

	builder *b1 = new buildervilla;

	b1->constuctor();
	b1->getinfo();

	system("pause");
	return 0;
}

/* 原型模式 */

#include <iostream>

using namespace std;

class Person
{
protected:
	string name;
public:
	Person()
	{
		
	}
	Person(string n)
	{
		name = n;
	}
	string GetName()
	{
		return name;
	}

	virtual void show() = 0;
	virtual Person *clone() = 0;
};

class Student : public Person
{
private:
	int id;
public:
	Student()
	{	
		
	}
	Student(string n, int i) : Person(n)
	{
		id = i;
	}
	void show()
	{
		cout << name << " " << id << endl;
	}
	Person *clone()
	{
		Student *tmp = new Student;
		*tmp = *this;

		return tmp;
	}
};

int main()
{
	Person *s1 = new Student("aa", 1);
	s1->show();

	Person *s2 = s1->clone();
	s2->show();

	return 0;
}

/* 组合模式 */

#include <iostream>
#include <list>

using namespace std;

class BaseFile
{
public:
	virtual string GetName() = 0;
	virtual int Add(BaseFile *file) = 0;
	virtual int Remove(BaseFile *file) = 0;
	virtual list<BaseFile *> *GetChild() = 0;
};

class IFile : public BaseFile
{
private:
	string name;
public:
	IFile(string n)
	{
		name = n;
	}
	string GetName()
	{
		return name;
	}
	int Add(BaseFile *file)
	{
		return -1;
	}

	int Remove(BaseFile *file)
	{
		return -1;
	}
	list<BaseFile *> *GetChild()
	{
		return NULL;
	}
};

class Dir : public BaseFile
{
private:
	string name;
	list<BaseFile *> *l;
public:
	Dir(string n)
	{
		name = n;
		l = new list<BaseFile *>;
	}
	string GetName()
	{
		return name;
	}
	int Add(BaseFile *file)
	{
		l->push_back(file);
		return 1;
	}
	int Remove(BaseFile *file)
	{
		l->remove(file);
		return 1;
	}
	list<BaseFile *> *GetChild()
	{
		return l;
	}
};

void show(BaseFile *root, int gap)
{
	for (int i = 0; i < gap; i++)
	{	
		cout << "---";
	}

	if (root != NULL)
	{
		cout << root->GetName() << endl;
	}
	else
	{
		return;
	}

	list<BaseFile *> *l = root->GetChild();
	if (l != NULL)
	{
		for (list<BaseFile *>::iterator it = l->begin(); it != l->end(); it++)
		{
			show((*it), gap + 1);
		}
	}
}

int main()
{	
	BaseFile *root = new Dir("root");
	BaseFile *dir1 = new Dir("home");
	BaseFile *file1 = new IFile("1.c");
	BaseFile *file2 = new IFile("2.c");
	BaseFile *file3 = new IFile("3.c");
	BaseFile *file4 = new IFile("4.c");
	BaseFile *file5 = new IFile("5.c");
	BaseFile *dir2 = new Dir("hello");

	dir1->Add(file1);
	dir1->Add(file2);
	dir1->Add(file3);
	dir1->Add(file4);
	dir1->Add(file5);

	dir2->Add(file1);
	dir2->Add(file2);
	dir2->Add(file3);
	dir2->Add(file4);
	dir2->Add(file5);

	root->Add(dir1);
	root->Add(dir2);
	/*list<BaseFile *> *l = dir1->GetChild();
	for (list<BaseFile *>::iterator it = l->begin(); it != l->end(); it++)
	{
		cout << (*it)->GetName() << endl;
	}*/

	show(root, 0);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38313246/article/details/81530732
8-9
今日推荐