双向适配器
实现一个双向适配器,使得猫可以学狗叫,狗可以学猫抓老鼠。
代码
ICat.java package shipeiqi; public interface ICat { public void eat(); public void catLook(); } IDog.java package shipeiqi; public interface IDog { public void wang(); public void dogLook(); } DogImpl.java package shipeiqi; public class DogImpl implements IDog{ public void dogLook() { // TODO Auto-generated method stub System.out.println("狗的样子"); } public void wang() { // TODO Auto-generated method stub System.out.println("狗汪汪叫"); } } CatImpl.java package shipeiqi; public class CatImpl implements ICat{ public void catLook() { // TODO Auto-generated method stub System.out.println("猫的样子"); } public void eat() { // TODO Auto-generated method stub System.out.println("猫抓老鼠"); } } Adapter.java package shipeiqi; public class Adapter implements ICat,IDog{ private IDog DogImpl = null; private ICat CatImpl = null; /*public Adapter(IDog DogImpl ) { // TODO Auto-generated constructor stub this.DogImpl = DogImpl; } public Adapter(ICat CatImpl) { // TODO Auto-generated constructor stub this.CatImpl = CatImpl; }*/ public Adapter(IDog DogImpl,ICat CatImpl) { // TODO Auto-generated constructor stub this.DogImpl = DogImpl; this.CatImpl = CatImpl; } @Override public void dogLook() { // TODO Auto-generated method stub System.out.println("狗模仿"); CatImpl.catLook(); } @Override public void wang() { // TODO Auto-generated method stub System.out.println("狗模仿"); CatImpl.eat(); } @Override public void catLook() { // TODO Auto-generated method stub System.out.println("猫模仿"); DogImpl.dogLook(); } @Override public void eat() { // TODO Auto-generated method stub System.out.println("猫模仿"); DogImpl.wang(); } } Client.java package shipeiqi; public class Client { public static void main(String[] args) { Adapter adapter = new Adapter(new DogImpl(),new CatImpl()); adapter.catLook(); adapter.eat(); System.out.println("**************"); adapter.dogLook(); adapter.wang(); } }
Shipeiqi.cpp #include<iostream> #include<string> #include"tinyxml2.h" using namespace std; using namespace tinyxml2; class CatTarget { public: virtual void catchMouse() = 0; }; class ConcreteCatTarget:public CatTarget { public: void catchMouse() { cout<<"抓老鼠"<<endl; } }; class DogAdaptee { public: virtual void wang()=0; }; class ConcreteDogAdaptee:public DogAdaptee { public: void wang() { cout<<"汪汪叫"<<endl; } }; class Adapter:public CatTarget,public DogAdaptee { private: CatTarget *catTarget; DogAdaptee *dogAdaptee; public: Adapter() { } void setCatTarget(CatTarget *catTarget) { this->catTarget = catTarget; } void setDogAdaptee(DogAdaptee *dogAdaptee) { this->dogAdaptee = dogAdaptee; } void catchMouse() { cout<<"猫学狗叫,即目标类调用适配者中的方法:"<<endl; dogAdaptee->wang(); } void wang(){ cout<<"狗学猫抓老鼠,即适配者调用目标类中的方法:"<<endl; catTarget->catchMouse(); } }; int main() { //适配器 Adapter *adapter=new Adapter(); // XMLDocument xml; // xml.LoadFile("config.xml"); // XMLElement *category = xml.RootElement(); // XMLElement *con = category->FirstChildElement("className"); //cout << "13233"; //目标类通过适配器调用适配者方法 // XMLElement* adp = con->FirstChildElement(); // XMLElement *next1 = adp->NextSiblingElement(); adapter->setCatTarget(new ConcreteCatTarget()); adapter->wang(); // cout << next1->GetText() << endl; //适配者通过适配器调用目标类方法 // XMLElement *next2 = next1->NextSiblingElement(); adapter->setDogAdaptee(new ConcreteDogAdaptee()); adapter->catchMouse(); }
标签:CatImpl,Auto,适配器,模式,public,TODO,void,Tutorial,DogImpl From: https://www.cnblogs.com/manmmm/p/16810617.html