设计模式(二)——结构型模式



一.代理模式


Proxy模式又叫做代理模式,是构造型的设计模式之一,它可以为其他对象提供一种代理(Proxy)以控制对这个对象的访问。    
所谓代理,是指具有与代理元(被代理的对象)具有相同的接口的类,客户端必须通过代理与被代理的目标类交互,而代理一般在交互的过程中(交互前后),进行某些特别的处理。
 subject(抽象主题角色)://如下例中的卖书
真实主题与代理主题的共同接口。
RealSubject(真实主题角色)://书店卖书
     定义了代理角色所代表的真实对象。 
Proxy(代理主题角色)://淘宝店代理书店卖书

     含有对真实主题角色的引用,代理角色通常在将客户端调用传递给真是主题对象之前或者之后执行某些操作,而不是单纯返回真实的对象。

#include<iostream>
using namespace std;
class SaleBook
{
	public:
	virtual void SaleBooks()=0;
};
class BookStore:public SaleBook
{
	private:
	static int count;
	public:
	void SaleBooks()
	{
		cout<<"书店卖书"<<endl;
		count++;
	}
	static int GetCount()
	{
		return count;
	}
};
int BookStore::count=0;
class TaoBao:public SaleBook
{
	private:
	SaleBook *salebook;
	public:
	TaoBao(SaleBook *s)
	{
		salebook=s;
	}
	void SaleBooks()
	{
		cout<<"淘宝店卖书带动";
		salebook->SaleBooks();
	}
};
int main()
{
	SaleBook *bookstore=new BookStore;
	SaleBook *taobao=new TaoBao(bookstore); 
	bookstore->SaleBooks();
	taobao->SaleBooks();
	cout<<"卖了"<<BookStore::GetCount()<<"本书"<<endl;
        delete bookstore;
        delete taobao;
	return 0;
}

二,装饰模式

概念:装饰( Decorator )模式又叫做包装模式。通过一种对客户端透明的方式来扩展对象的功能,是继承关系的一个替换方案。
     装饰模式就是把要添加的附加功能分别放在单独的类中,并让这个类包含它要装饰的对象,当需要执行时,客户端就可以有选择地、按顺序地使用装饰功能包装对象。

#include<iostream>
using namespace std;
class Phone
{
    public:
        virtual void Function()=0;
};
class CallPhone:public Phone
{
        void Function()
        {
            cout<<"能打电话的手机哦……"<<endl;
        }
};
class PhotoPhone:public Phone
{
    
    private:
        Phone *phone;
    public:
        PhotoPhone(Phone *p)
        {
            phone=p;
        }
        void Function()
        {
            phone->Function();
            cout<<"能拍照的手机哦……"<<endl;
        }
};
class MusicPhone:public Phone
{
    
    private:
        Phone *phone;
    public:
        MusicPhone(Phone *p)
        {
            phone=p;
        }
        void Function()
        {
            phone->Function();
            cout<<"能听音乐的手机哦……"<<endl;
        }
};
class InternetPhone:public Phone
{
    
    private:
        Phone *phone;
    public:
        InternetPhone(Phone *p)
        {
            phone=p;
        }
        void Function()
        {
            phone->Function();
            cout<<"能上网冲浪的手机哦……"<<endl;
        }
};
int main()
{
    cout<<"大哥大:"<<endl;
    Phone *call_phone=new CallPhone;
    call_phone->Function();
    cout<<"2000万柔光双摄:"<<endl;
    Phone *photo_phone=new PhotoPhone(call_phone);
    photo_phone->Function();
    cout<<"HIFI立体声:"<<endl;
    Phone *music_phone=new MusicPhone(call_phone);
    music_phone->Function();
    cout<<"vivox21,照亮你的美:"<<endl;
    Phone *photo_music_phone=new PhotoPhone(music_phone);
    photo_music_phone->Function();
    cout<<"iphone X人脸识别手机:"<<endl;
    Phone *internet_phone=new InternetPhone(photo_music_phone);
    internet_phone->Function();
    return 0;
}

 

三。适配器模式

概念:Adapter模式也叫适配器模式,是构造型模式之一,通过Adapter模式可以改变已有类(或外部类)的接口形式。


#include<iostream>
using namespace std;
class Voltage
{
	public:
	virtual int GetVoltage()=0;
	
};
class Voltage220:public Voltage
{
	private:
	int voltage;
	public:
	Voltage220()
	{
		voltage=220;
	}
	int GetVoltage()
	{
		return voltage;
	}
};
class Adapter:public Voltage
{
	private:
	Voltage *voltage;
	public:
	Adapter(Voltage *vol)
	{
		voltage=vol;
	}
	int GetVoltage()
	{
		if(voltage->GetVoltage()==220)
		{
			return 12;
		}
		else if(voltage->GetVoltage()<220)
		{
			return 10;
		}
	}
};
class Phone
{
	private:
	Voltage *voltage;
	public:
	Phone(Voltage *v)
	{
		voltage=v;
	}
	void PhoneStart()
	{
		if(voltage->GetVoltage()==220)
		{
			cout<<"GG"<<endl;
		}
		else if(voltage->GetVoltage()==12)
		{
			cout<<"phone charge"<<endl;
		}
		else if(voltage->GetVoltage()==10)
		{
			cout<<"low voltage"<<endl;
		}
	}
};
int main()
{
	Voltage *voltage220=new Voltage220;
	Adapter *adapter=new Adapter(voltage220);
	Phone* phone=new Phone(adapter);
	phone->PhoneStart();
	
	return 0;
}

四。组合模式

概念: Composite模式也叫组合模式,是构造型的设计模式之一。通过递归手段来构造树形的对象结构,并可以通过一个对象来访问整个对象树。
  
Component (树形结构的节点抽象)
- 为所有的对象定义统一的接口(公共属性,行为等的定义)
- 提供管理子节点对象的接口方法
- [可选]提供管理父节点对象的接口方法




Leaf (树形结构的叶节点)
Component的实现子类




Composite(树形结构的枝节点)
Component的实现子类
适用于:
单个对象和组合对象的使用具有一致性。将对象组合成树形结构以表示“部分--整体”
 
 
 

#include<iostream>
#include<list>
using namespace std;
class BaseFile
{
	public:
	virtual int Add(BaseFile *file)=0;
	virtual int Remove(BaseFile *file)=0;
	virtual string get_name()=0;
	virtual list<BaseFile *>*get_child()=0;
};
class iFile:public BaseFile
{
	private:
	string mName;
	public:
	iFile(string n)
	{
		mName=n;
	}
	string get_name()
	{
		return mName;
	}
	int Add(BaseFile *file)//鏂囦欢涓嶅瓨鍦ㄥ湪浠栦笅闈㈠鍔犳枃浠躲€?
	{
		return -1;
	}
	int Remove(BaseFile *file)
	{
		return -1;
	}
	list<BaseFile*>*get_child()
	{
		return NULL;
	}
};
class Dir:public BaseFile
{
	private:
	string mName;
	list<BaseFile*>*l;
	public:
	Dir(string n)
	{
		mName=n;
		l=new list<BaseFile*>;
	}
	string get_name()
	{
		return mName;
	}
	int Add(BaseFile* file)
	{
		l->push_back(file);
		return 1;
	}
	int Remove(BaseFile *file)
	{
		l->remove(file);
		delete file;
		return 1;
	}
	list<BaseFile*> *get_child()
	{
		return l;
	}
};
void show(BaseFile* root,int gap)
{
	for(int i=0;i<gap;i++)
	{
		cout<<"----";
	}
	if(root!=NULL)
	{
		cout<<root->get_name()<<endl;
		
	}
	else 
	{
		return ;
	}
	list<BaseFile*> *l=root->get_child();
	if(l!=NULL)
	{
		for(list<BaseFile*>::iterator it=l->begin();it!=l->end();it++)
		{
			show((*it),gap+1);
		}
	}
}
int main()
{
	BaseFile* dir=new Dir("home");
	BaseFile* file1=new iFile("hello.c");
	BaseFile* file2=new iFile("world.c");
	BaseFile* file3=new iFile("hello.cpp");
	BaseFile* file4=new iFile("world.cpp");
	BaseFile* dir2=new Dir("lesson");
	dir->Add(file1);
	dir->Add(file2);
	dir->Add(dir2);
	dir2->Add(file3);
	dir2->Add(file4);
	show(dir,0);
	
	return 0;
}

桥接模式:


概念:bridge 模式又叫做桥接模式,是构造型的设计模式之一。Bridge模式基于类的最小设计原则,通过使用封装,聚合以及继承等行为来让不同的类承担不同的责任。它的主要特点是把抽象(abstraction)与行为实现(implementation)分离开来,从而可以保持各部分的独立性以及应对它们的功能扩展。
Client
    Bridge模式的使用者
Abstraction
    抽象类接口(接口或抽象类)维护对行为实现(Implementor)的引用
Refined Abstraction
    Abstraction子类
Implementor
    行为实现类接口 (Abstraction接口定义了基于Implementor接口的更高层次的操作)
ConcreteImplementor
Implementor子类
 

#include<iostream>
using namespace std;
class Phone
{
	public:
	virtual void show()=0;
};
class Xiaomi:public Phone
{
	public:
	void show()
	{
		cout<<"小米手机"<<endl;
	}
};
class Iphone:public Phone
{
	public:
	void show()
	{
		cout<<"苹果手机"<<endl;
	}
};
class Soft
{
	public:
	virtual void SetSoft(Phone *p)=0;
	virtual void show()=0;
};
class WeChat:public Soft
{
	private:
	Phone *phone;
	public:
	void SetSoft(Phone *p)
	{
		phone=p;
	}
	void show()
	{
		phone->show();
		cout<<"微信"<<endl;
	}
};
class QQ:public Soft
{
	private:
	Phone *phone;
	public:
	void SetSoft(Phone *p)
	{
		phone=p;
	}
	void show()
	{
		phone->show();
		cout<<"QQ"<<endl;
	}
};
int main()
{
	Phone *phone=new Xiaomi;
	Soft *soft=new WeChat;
	soft->SetSoft(phone);
	soft->show();
	return 0;
}


外观模式:

概念: Facade模式也叫外观模式,是由GoF提出的23种设计模式中的一种。Facade模式为一组具有类似功能的类群,比如类库,子系统等等,提供一个一致的简单的界面。这个一致的简单的界面被称作facade。
 Facade
为调用方, 定义简单的调用接口。
Clients
       调用者。通过Facade接口调用提供某功能的内部类群。
Packages
       功能提供者。指提供功能的类群(模块或子系统)

适用情况:为子系统中统一一套接口,让子系统更加容易使用。

外观模式和工厂模式的区别:工厂模式只创建而不会调用,而外观模式不仅包含创建,而且包含调用和析构;

#include<iostream>
using namespace std;
class SystemA
{
	public:
	void Work()
	{
		cout<<"A"<<endl;
	}
};
class SystemB
{
	public:
	void Work()
	{
		cout<<"B"<<endl;
	}
};
class SystemC
{
	public:
	void Work()
	{
		cout<<"C"<<endl;
	}
};
class Facede()
{
	private:
	SystemA *a;
	SystemB *b;
	SystemC *c;
	public:
	Facede()
	{
		a=new SystemA;
		b=new SystemB;
		c=new SystemC;
	}
	void work()
	{
		a->Work();
		b->Work();
		c->Work();
	}
	~Facede()
	{
		delete a;
		delete b;
		delete c;
	}
};
int main()
{
	Facede *f=new Facede;
	f->work();
	delete f;
	return 0;
}



享元模式:

概念:Flyweight模式也叫享元模式,是构造型模式之一,它通过与其他类似对象共享数据来减小内存占用。

抽象享元角色:

所有具体享元类的父类,规定一些需要实现的公共接口。
具体享元角色:
     抽象享元角色的具体实现类,并实现了抽象享元角色规定的方法。
享元工厂角色:
      负责创建和管理享元角色。



#include <iostream>
#include <map>
using namespace std;
class Student
{
     private:
     int age;
    string name;
     int id;
     public:
     Student(string n, int a, int i)
    {
        name=n;
        age=a;
        id=i;
    }
    string GetName()
    {
         return name;
    }
     int GetAge()
    {
         return age;
    }
     int GetId()
    {
         return id;
    }
};
class FwFactory
{
     private:
    multimap< int,Student *>*m;
     public:
     FwFactory()
    {
        m= new multimap< int,Student*>;
    }
     ~FwFactory()
    {
        multimap< int,Student*>::iterator it;
         while(!m-> empty())
        {
            it=m-> begin(); //图,返回一个pair,第一个是它的键值,第二个是它里面的值;
            Student* tmp=it-> second;
             delete tmp;
            m-> erase(it);
        }
    }
    Student * Get_Person( int id)
    {
        multimap< int,Student*>::iterator it;
        it=m-> find(id);
        Student* s;
         if(it==m-> end())
        {
            string name;
             int age,m_id;
            cout<< "id "<<id<< "not exist,please input name age anf id "<<endl;
            cin>>name>>age>>m_id;
            s= new Student(name,age,m_id);
            m-> insert( make_pair(id,s));
        }
         else
        {
                s=it-> second;
                cout<< "id"<<id<< "info :"<<endl;
                cout<< "name: "<<s-> GetName()<< "age: "<<s-> GetAge()<< "id: "<<s-> GetId()<<endl;
        }
         return s;
    }
};
int main()
{
    FwFactory *f= new FwFactory;
    f-> Get_Person( 1);
    f-> Get_Person( 2);
    f-> Get_Person( 3);
    f-> Get_Person( 1);
     return 0;
}










猜你喜欢

转载自blog.csdn.net/zzw_17805056819/article/details/80410056