首页 > 其他分享 >Tutorial 8_适配器模式

Tutorial 8_适配器模式

时间:2022-10-20 17:34:35浏览次数:41  
标签:CatImpl Auto 适配器 模式 public TODO void Tutorial DogImpl

双向适配器

实现一个双向适配器,使得猫可以学狗叫,狗可以学猫抓老鼠。

 

 代码

 

 

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

相关文章

  • Tutorial 9_桥接模式
    两个维度的桥接模式用桥接模式实现在路上开车这个问题,其中,车可以是car或bus,路可以是水泥路或沥青路。类图  代码Vehicle.javapackageqiaojie;publicinterfa......
  • Collections 类中设计模式的应用
    装饰器模式Collections类是一个集合容器的工具类,提供了很多静态方法,用来创建各种集合容器,比如通过unmodifiableColletion()静态方法,来创建UnmodifiableCollection类......
  • Calendar 类中设计模式的应用
    包名:java.util.Calendar工厂模式Calendar类提供了大量跟日期相关的功能代码,同时,又提供了一个getInstance()工厂方法,用来根据不同的TimeZone和Locale创建不同的Ca......
  • 享元模式
    实验13:享元模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解享元模式的动机,掌握该模式的结构;2、能够利用享元模式解决实际问题。   ......
  • 9000字,唠唠架构中的设计模式
    1设计模式概述​ 软件设计模式(SoftwareDesignPattern),俗称设计模式,设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结。它描述了在软件设......
  • MybatisPlus对租户模式的支持(一)
    前言最近接到一个任务,要将现有的用户系统改成租户模式。改造成租户模式最简单的方式就是为需要进行数据隔离的表加上租户id字段,然后前端调接口查询数据时,根据当前用户的租......
  • 设计模式之UML类图
    UML图示简介在UML中,类使用包含类名、属性和操作且带有分割线的长方形来表示,如图所示,定义一个Student类,它包含属性name、age和id,以及操作modifyInfo()。其对应的......
  • 【多线程那些事儿】如何使用C++写一个线程安全的单例模式?
    如何写一个线程安全的单例模式?单例模式的简单实现单例模式大概是流传最为广泛的设计模式之一了。一份简单的实现代码大概是下面这个样子的:classsingleton{public: s......
  • 数字化转型后企业的管理模式有哪些改变呢?
    数字化转型后的企业在管理模式上整体呈现的就是数字化的管理,这是因为数字化转型的主题就是要构建“业务数字化、数字资产化、资产服务化、服务业务化”闭环,通过数字化技术能......
  • JAVA设计模式-代理模式
    JAVA设计模式-代理模式一、介绍代理模式是一种结构型模式,它指的是给某一个对象提供一个代理对象,并且由代理对象控制原有对象的引用,可以增强原有对象的功能以及降低系统......