当前位置: 代码迷 >> 综合 >> Head First 设计模式 Design Pattern 7-8 Adapter Facade, Template Method
  详细解决方案

Head First 设计模式 Design Pattern 7-8 Adapter Facade, Template Method

热度:58   发布时间:2023-12-20 11:25:24.0

Section 7 适配器模式和外观模式 Adapter & Facade

>客户使用适配器 Client - Request -> Adapter -> TranslateRequest - Adaptee 

客户通过目标接口调用适配器的方法发出请求; 适配器使用被适配者的接口把请求转换成被适配者的一个或多个调用接口; 客户收到调用结果, 并不知道是适配器在转化请求

>双向适配器, 实现2个接口, 新旧并存

适配器模式 将一个类的接口转化成客户期望的另一个接口. 让原本接口不兼容的类一起工作.

>让接口兼容, 实现接口的去耦合

对象和类的适配器

>类适配器需要多重继承, Java无法完成 >对象适配器-组合; 类适配器-继承;


>适配器文档, 使用说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//***********Adapter & Facade**********
class IPanda
{
public:
    virtual void eat() = 0;
    virtual void climb() = 0;
};
                                                                                                                                              
class IBear
{
public:
    virtual void eat() = 0;
    virtual void creep() = 0;
};
                                                                                                                                              
class BigPanda : public IPanda
{
public:
    virtual void eat() { cout << "Eat Bamboo" << endl ;}
    virtual void climb() { cout << "Climb" << endl ;}
};
                                                                                                                                              
class BlackBear : public IBear
{
 public:
    virtual void eat() { cout << "Eat Fish" << endl ;}
    virtual void creep() { cout << "Creep" << endl ;}
};
                                                                                                                                              
class BearAdapter : public IPanda
{
public:
    BearAdapter(IBear* bear);
    virtual void eat() { mpBear->eat(); }
    virtual void 
  相关解决方案