adapter的两种实现

// adapter_class.cpp

#include <stdio.h>

#ifdef _X_RELEASE_
    #define TRACE(str)
#else
    #define TRACE(str) printf("%s/n", str)
#endif

// 被适配者
class CAdaptee
{
public:
    CAdaptee()
    {
        TRACE("CAdaptee");
    }

    virtual ~CAdaptee()
    {
        TRACE("~CAdaptee");
    }
   
    void SpecialRequest()
    {
        TRACE("SpecialRequest");
    }
} ;

// 客户操作的接口
class CTarget
{
public:
    CTarget()
    {
        TRACE("CTarget");
    }

    virtual ~CTarget()
    {
        TRACE("~CTarget");
    }
   
    virtual void Request() = 0;
} ;

// 类适配器,继承方式
class CAdapter : public CTarget, private CAdaptee
{
public:
    CAdapter()
    {
        TRACE("CAdapter");
    }

    virtual ~CAdapter()
    {       
        TRACE("~CAdapter");
    }
   
    virtual void Request()
    {
        TRACE("CAdapter::Request");
       
        SpecialRequest(); // 直接转发请求
    }
} ;

int main()
{
    CTarget* pTarget = new CAdapter();
    if (!pTarget)
    {
        TRACE("error 1");
        return 1;
    }
   
    pTarget->Request();

    delete pTarget;
    pTarget = 0;

    return 0;
}

/*
root@ubuntu:/home/hgc/test# ./adapter_class
CTarget
CAdaptee
CAdapter
CAdapter::Request
SpecialRequest
~CAdapter
~CAdaptee
~CTarget
root@ubuntu:/home/hgc/test#
*/

 

// adapter_object.cpp

#include <stdio.h>

#ifdef _X_RELEASE_
    #define TRACE(str)
#else
    #define TRACE(str) printf("%s/n", str)
#endif

// 被适配者
class CAdaptee
{
public:
    CAdaptee()
    {
        TRACE("CAdaptee");
    }

    virtual ~CAdaptee()
    {
        TRACE("~CAdaptee");
    }
   
    void SpecialRequest()
    {
        TRACE("SpecialRequest");
    }
} ;

// 客户操作的接口
class CTarget
{
public:
    CTarget()
    {
        TRACE("CTarget");
    }

    virtual ~CTarget()
    {
        TRACE("~CTarget");
    }
   
    virtual void Request() = 0;
} ;

// 类适配器,继承方式
class CAdapter : public CTarget
{
private:
    CAdaptee* m_pAdaptee;
   
public:
    CAdapter(CAdaptee* pAdaptee)
    {
        TRACE("CAdapter");

        m_pAdaptee = pAdaptee;
    }

    virtual ~CAdapter()
    {       
        TRACE("~CAdapter");

        if (m_pAdaptee)
        {
            delete m_pAdaptee;
            m_pAdaptee = 0;
        }
    }
   
    virtual void Request()
    {
        TRACE("CAdapter::Request");
       
        m_pAdaptee->SpecialRequest(); // 直接转发请求
    }
} ;

int main()
{
    CTarget* pTarget = new CAdapter(new CAdaptee());
    if (!pTarget)
    {
        TRACE("error 1");
        return 1;
    }
   
    pTarget->Request();

    delete pTarget;
    pTarget = 0;

    return 0;
}

/*
root@ubuntu:/home/hgc/test# g++ adapter_object.cpp -o adapter_object -g -Wall
root@ubuntu:/home/hgc/test# ./adapter_object
CAdaptee
CTarget
CAdapter
CAdapter::Request
SpecialRequest
~CAdapter
~CAdaptee
~CTarget
root@ubuntu:/home/hgc/test#
*/


// adapter_demo_class.cpp

#include <stdio.h>

#ifdef _X_RELEASE_
    #define TRACE(str)
#else
    #define TRACE(str) printf("%s/n", str)
#endif

// 注意: x轴向右延伸,y轴向下延伸,原点在左上角
class Point
{
private:
    int m_x, m_y;

public:
    Point()
    {
    }
   
    Point(int x, int y)
    {
        m_x = x;
        m_y = y;
    }

    int GetX() const
    {
        return m_x;
    }

    int GetY() const
    {
        return m_y;
    }

    void Display()
    {
        printf("(%d, %d)/n", m_y, m_x);
    }
};

// target
class Shape
{
public:
    Shape()
    {
    }
   
    virtual void BoundingBox(Point& topLeft, Point& bottomRight) const = 0; // 客户期待的接口
    virtual bool IsEmpty() const = 0; // 客户期待的接口
};

// adaptee
class TextView
{
private:
    int m_top, m_left, m_width, m_height;
   
public:
    TextView(int top, int left, int width, int height)
    {
        m_top = top;
        m_left = left;
        m_width = width;
        m_height = height;
    }

    // 获取原点的接口,需要适配
    void GetOrigin(int& top, int& left) const
    {
        top = m_top;
        left = m_left;
    }

    // 获取长(向右延伸)和高(向下延伸)的接口,需要适配
    void GetExtent(int& width, int& height) const
    {
        width = m_width;
        height = m_height;
    }
   
    virtual bool IsEmpty() const
    {
        if ((0 == m_width) || (0 == m_height))
        {
            return true;
        }

        return false;
    }
};

// adapter (class)
class TextShape : public Shape, private TextView
{
public:
    TextShape(int top, int left, int width, int height)
        : TextView(top, left, width, height)
    {
    }
   
    virtual void BoundingBox(Point& topLeft, Point& bottomRight) const;
    virtual bool IsEmpty() const; // 该接口无需适配,可以直接使用
};

void TextShape::BoundingBox(Point& topLeft, Point& bottomRight) const
{
    int top, left, width, height;

    // 适配过程
    GetOrigin(top, left);
    GetExtent(width, height);

    topLeft = Point(top, left);
    bottomRight = Point(top + height, left + width);
}

bool TextShape::IsEmpty() const
{
    // 因为是private继承的,所以定义一个相同的public接口,然后转发请求
    return TextView::IsEmpty();
}

int main()
{
    Shape* pShape = new TextShape(1, 0, 5, 4);
    if (!pShape)
    {
        TRACE("error 1");
        return 1;
    }

    if (!pShape->IsEmpty())
    {
        Point topLeft, bottomRight;
        pShape->BoundingBox(topLeft, bottomRight);

        topLeft.Display();
        bottomRight.Display();
    }

    delete pShape;
    pShape = 0;

    return 0;
}

/*
root@ubuntu:/home/hgc/test# g++ adapter_demo_class.cpp -o adapter_demo_class -g -Wall
root@ubuntu:/home/hgc/test# ./adapter_demo_class
(0, 1)
(5, 5)
root@ubuntu:/home/hgc/test#
*/

 

// adapter_demo_object.cpp

#include <stdio.h>

#ifdef _X_RELEASE_
    #define TRACE(str)
#else
    #define TRACE(str) printf("%s/n", str)
#endif

// 注意: x轴向右延伸,y轴向下延伸,原点在左上角
class Point
{
private:
    int m_x, m_y;

public:
    Point()
    {
    }
   
    Point(int x, int y)
    {
        m_x = x;
        m_y = y;
    }

    int GetX() const
    {
        return m_x;
    }

    int GetY() const
    {
        return m_y;
    }

    void Display()
    {
        printf("(%d, %d)/n", m_y, m_x);
    }
};

// target
class Shape
{
public:
    Shape()
    {
    }
   
    virtual void BoundingBox(Point& topLeft, Point& bottomRight) const = 0; // 客户期待的接口
    virtual bool IsEmpty() const = 0; // 客户期待的接口
};

// adaptee
class TextView
{
private:
    int m_top, m_left, m_width, m_height;
   
public:
    TextView(int top, int left, int width, int height)
    {
        m_top = top;
        m_left = left;
        m_width = width;
        m_height = height;
    }

    // 获取原点的接口,需要适配
    void GetOrigin(int& top, int& left) const
    {
        top = m_top;
        left = m_left;
    }

    // 获取长(向右延伸)和高(向下延伸)的接口,需要适配
    void GetExtent(int& width, int& height) const
    {
        width = m_width;
        height = m_height;
    }
   
    virtual bool IsEmpty() const
    {
        if ((0 == m_width) || (0 == m_height))
        {
            return true;
        }

        return false;
    }
};

// adatper (object)
class TextShape : public Shape
{
private:
    TextView* m_pTextView;
   
public:
    TextShape(TextView* pTextView);
    virtual ~TextShape();
    virtual void BoundingBox(Point& topLeft, Point& bottomRight) const;
    virtual bool IsEmpty() const; // 该接口无需适配,可以直接使用
};

TextShape::TextShape(TextView* pTextView)
{
    m_pTextView = pTextView;
}

TextShape::~TextShape()
{
    if (m_pTextView)
    {
        delete m_pTextView;
        m_pTextView = 0;
    }
}

void TextShape::BoundingBox(Point& topLeft, Point& bottomRight) const
{
    int top, left, width, height;

    // 适配过程
    m_pTextView->GetOrigin(top, left);
    m_pTextView->GetExtent(width, height);

    topLeft = Point(top, left);
    bottomRight = Point(top + height, left + width);
}

bool TextShape::IsEmpty() const
{
    // 因为是private继承的,所以定义一个相同的public接口,然后转发请求
    return m_pTextView->IsEmpty();
}

int main()
{
    Shape* pShape = new TextShape(new TextView(1, 0, 5, 4));
    if (!pShape)
    {
        TRACE("error 1");
        return 1;
    }

    if (!pShape->IsEmpty())
    {
        Point topLeft, bottomRight;
        pShape->BoundingBox(topLeft, bottomRight);

        topLeft.Display();
        bottomRight.Display();
    }

    delete pShape;
    pShape = 0;

    return 0;
}

/*
root@ubuntu:/home/hgc/test# g++ adapter_demo_object.cpp -o adapter_demo_object -g -Wall
root@ubuntu:/home/hgc/test# ./adapter_demo_object
(0, 1)
(5, 5)
root@ubuntu:/home/hgc/test#
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android 中,可以使用 RecyclerView 和 ListView 来实现列表联动。下面分别介绍这两种方式的实现方法。 1. RecyclerView 实现列表联动 RecyclerView 是 Android 中比较新的列表控件,它提供了更加灵活的布局和样式定制方式。实现 RecyclerView 的列表联动,可以使用 RecyclerView 的滚动事件来监听列表的滚动位置,然后通过列表的滚动位置来计算联动列表中需要展示的内容。 具体实现步骤如下: 1)在布局文件中添加两个 RecyclerView 控件。 2)在代码中设置 RecyclerView 的 layoutManager,并为 RecyclerView 设置 Adapter。 3)为第一个 RecyclerView 添加滚动监听事件,监听其滚动位置。 4)在滚动监听事件中计算联动列表的滚动位置,并将其滚动到相应位置。 示例代码如下: ``` // 初始化 RecyclerView RecyclerView recyclerView1 = findViewById(R.id.recyclerView1); RecyclerView recyclerView2 = findViewById(R.id.recyclerView2); recyclerView1.setLayoutManager(new LinearLayoutManager(this)); recyclerView2.setLayoutManager(new LinearLayoutManager(this)); // 为 RecyclerView 设置 Adapter recyclerView1.setAdapter(adapter1); recyclerView2.setAdapter(adapter2); // 监听第一个 RecyclerView 的滚动事件 recyclerView1.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); // 计算联动列表的滚动位置 int firstVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition(); recyclerView2.scrollToPosition(firstVisibleItemPosition); } }); ``` 2. ListView 实现列表联动 ListView 是 Android 中较早的列表控件,它的使用方式和 RecyclerView 有所不同。实现 ListView 的列表联动,需要通过 ListView 的滚动监听事件来监听列表的滚动位置,然后通过滚动位置来计算联动列表中需要展示的内容。 具体实现步骤如下: 1)在布局文件中添加两个 ListView 控件。 2)在代码中设置 ListView 的 Adapter。 3)为第一个 ListView 添加滚动监听事件,监听其滚动位置。 4)在滚动监听事件中计算联动列表的滚动位置,并将其滚动到相应位置。 示例代码如下: ``` // 初始化 ListView ListView listView1 = findViewById(R.id.listView1); ListView listView2 = findViewById(R.id.listView2); // 为 ListView 设置 Adapter listView1.setAdapter(adapter1); listView2.setAdapter(adapter2); // 监听第一个 ListView 的滚动事件 listView1.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // do nothing } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // 计算联动列表的滚动位置 listView2.setSelection(firstVisibleItem); } }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值