Design patterns---Structural Patterns

结构型模式关注如何将类和对象组合成更大的结构。适配器模式用于将类的接口转换为客户期望的接口,使得原本由于接口不兼容而不能一起工作的类能够协同工作。该模式适用于原有类的接口不符合需求,或者需要与不相关的类协同工作的情况。桥接模式则旨在将抽象和实现分离,以便两者可以独立变化。组合模式通过将对象组合成树形结构来表示部分-整体层次结构,客户端可以统一处理单个对象和组合对象。装饰者模式动态地给对象添加一些职责,提供了一种比继承更灵活的方式来扩展功能。外观模式为子系统提供了一个统一的接口,使得子系统更容易使用。
摘要由CSDN通过智能技术生成

Structural patterns are concerned with how classes and objects are composed to form larger structures.Structural class patterns use inheritance to compose interfaces or implementations.
I.Adapter:
Intent:Convert the interface of a class into another interface clients expert.Adapter lets classes work together that couldn’t otherwise because of imcompatible interfaces.
Applicability:Use the Adapter pattern when
*you want to use an existing class,and its interface does not match one you need.
*you want to create a reusable class that cooperates with unrelated or unforeseen classes,that is,classes that don’t necessarily have compatible interfaces.
*(object adapter only)you need to use several existing subclasses,but it’s impractical to adapt their interface by subclassing every one.An object adapter can adapt the interface of its parent class.

Structure:
在这里插入图片描述Consequences:
Class and object adapters have different trade-offs.
A class adapter:
*adapts Adaptee to Target by committing to a concrete Adaptee class.As a consequence,a class adapter won’t work when we want to adapt a class and all its subclasses.
*lets Adapter override some of Adaptee’s behavior,since Adapter is a subclass of Adaptee.
*introduces only one object,and no additional pointer indirection is needed to get to the adaptee.
An object adapter:
*lets a single Adapter work with many Adaptees—that is,the Adaptee itself and all of its subclasses(if any).The Adapter can also add functionality to all Adaptees at once.
*makes it harder to override Adaptee behavior.It will require subclassing Adaptee and making Adapter refer to the subclass rather than the Adaptee itself.

Here are other issues to consider when using the Adapter pattern:
*How much adapting does Adapter do?
*Pluggable adapters.
*Using two-way adapters to provide transparency.

Sample code://c++,this is not a program

class Shape{
     //target
    public:
    virtual void BoundingBox(Point& bottomleft,Point& topRight)const;
    }
    virtual Manipulator* CreateManipulator()const;
};

class TextView{
    //Adaptee
    public:
    TextView();
    void GetOrigin(Coord& x,Coord& y)const;
    void GetExtent(Coord& width,Coord& height)const;
    virtual bool IsEmpty()const;
};
class TextShape:public Shape,(optional)private TextView{
   //Adapter
  public:
  TextShape();
 // TextShape(TextView* );
  virtual void BoundingBox(Point& bottomleft,Point& topRight)const;
  virtual bool IsEmpty() const;
  virtual Manipulator* CreateManipulator()const;
 // private:
 // TextView* _text;
};
/*TextShape::TextShape(TextView* t)
{
    _text=t;
}*/
void TextShape::BoundingBox(Point &bottomLeft,Point& topRight)const{
   
    Coord bottom,left,width,height;
    GetOrigin(left,bottom);
    GetExtent(width,height);
   // _text->GetOrigin(left,bottom);
   // _text->GetExtent(width,height);
    bottomLeft=Point(left,bottom);
    topRight=Point(left+width,bottom+height);
}
bool TextShape::IsEmpty()const{
   
    return TextView::IsEmpty();
   // return _text->IsEmpty();
}
Manipulator* TextShape::CreateManipulator()const{
   
    return new TextManipulator(this);
}

Related Patterns:
Bridge has a structure similar to an object adapter,but Bridge has a different intent:It is meant to separate an interface from its implementation so that they can be varied easily and independently.An adapter is meant to change the interface of an existing object.
Decorator enhances another object without changing its interface.A decorator is thus more transparent to the application than an adapter is.As a consequence,Decorator supports recursive composition,which isn’t possible with pure adapters.
Proxy defines a representative or surrogate for another object and dose not change its interface.

II.Bridge
Intent:
Decouple an abstraction from its implementation so that the two can vary independently.
Applicability:Use the Bridge pattern when:
*you want to avoid a permanent binding between an abstraction and its implementation.
*both the abstractions and their implementations should be extensible by subclass ing.In this case,the Bridge pattern lets you combine the different abstractions and implementations and extend them independently.
*changes in the implementation of an abstraction should have no impact on clients;that is,their code should not have to be recompiled.
*you want to share an implementation among multiple objects,and this fact should be hidden from the client.

Structure:
在这里插入图片描述Consequences:
*Decoupling interface and implementation.
*Improved extensibility.
*Hiding implementation details from clients.
implementation:
*Only one Implementor.
*Creating the right Implementor object.
*Sharing implementors.

Handle& Handle::operator(const Handle& other)
{
   
    other._body->Ref();
    _body->Unref();

    if(_body->RefCount()==0)
      delete _body;

      _body=other._body;
      return *this;
}

*Using multiple inheritance.

Sample Code://C++,Not a program

class Window{
   
    public:
    Window(View* contents);
    //requests handled by window
    virtual void DrawContents();
    virtual void Open();
    virtual void Close();
    virtual void Iconify();
    virtual void Deiconify();
    //requests forwarded to implementation
    virtual void SetOrigin(const Point& at);
    virtual void SetExtent(const Point& extent);
    virtual void Raise();
    virtual void Lower();

    virtual void DrawLine(const Point&,const Point&);
    virtual void DrawRect(const Point&,const Point&);
    virtual void DrawPolygon(const Point[],int n);
    virtual void DrawText(const char*,const Point&);
    protected:
    WindowImp* GetwindowImp();
    View* GetView();
    private:
    WindowImp* _imp;
    View* _contents;
};
class WindowImp{
   
    public:
    virtual void ImpTop()=0;
    virtual void ImpBottom()=0;
    virtual void ImpSetExtent(const Point&)=0;
    virtual void ImpSetOrigin(const Point&)=0;

    virtual void DeviceRect(Coord,Coord,Coord,Coord)=
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值