GeekBand-筆記-設(shè)計(jì)模式-02

建議下載pdf附件。

l Factory Method****(工廠方法)****
**

意圖:**
**

定義一個(gè)用于創(chuàng)建對(duì)象的接口伏伐,讓子類決定實(shí)例化哪一個(gè)類祈餐。Factory Method 使一個(gè)類的實(shí)例化延遲到其子類鹃操。

適用性:**
**

當(dāng)一個(gè)類不知道它所必須創(chuàng)建的對(duì)象的類的時(shí)候仓坞。

當(dāng)一個(gè)類希望由它的子類來指定它所創(chuàng)建的對(duì)象的時(shí)候颜说。

當(dāng)類將創(chuàng)建對(duì)象的職責(zé)委托給多個(gè)幫助子類中的某一個(gè)购岗,并且你希望將哪一個(gè)幫助子類是代理者這一信息局部化的時(shí)候。

代碼實(shí)現(xiàn):
**

#include
<iostream.h>
*

/
Abstract base class declared by framework /*

class
Document
*

{*

  • public:**

  •        Document(char *fn)**
    
  •        {**
    
  •                          strcpy(name,
    

fn);**

  •        }**
    
  •        virtual void Open() = 0;**
    
  •        virtual void Close() = 0;**
    
  •        char *GetName()**
    
  •        {**
    
  •                          return name;**
    
  •        }**
    
  • private:**

  •        char name[20];**
    

};*

/
Concrete derived class defined by client /*

class
MyDocument: public Document
*

{*

  • public:**

  •        MyDocument(char *fn):
    

Document(fn){}**

  •        void Open()**
    
  •        {**
    
  •                          cout <<
    

" MyDocument: Open()" << endl;**

  •        }**
    
  •        void Close()**
    
  •        {**
    
  •                          cout <<
    

" MyDocument: Close()" << endl;**

  •        }**
    

};*

/
Framework declaration /*

class
Application
*

{*

  • public:**

  •        Application(): _index(0)**
    
  •        {**
    
  •                          cout <<
    

"Application: ctor" << endl;**

  •        }**
    
  •        /* The client will call this
    

"entry point" of the framework /*

  •        NewDocument(char *name)**
    
  •        {**
    
  •                          cout <<
    

"Application: NewDocument()" << endl;**

  •                          /* Framework
    

calls the "hole" reserved for client customization /*

  •                          _docs[_index]
    

= CreateDocument(name);**

  •                          _docs[_index++]->Open();**
    
  •        }**
    
  •        void OpenDocument(){}**
    
  •        void ReportDocs();**
    
  •        /* Framework declares a
    

"hole" for the client to customize /*

  •        virtual Document
    

CreateDocument(char) = 0;**

  • private:**

  •        int _index;**
    
  •        /* Framework uses Document's
    

base class /*

  •        Document *_docs[10];**
    

};*

void
Application::ReportDocs()
*

{*

  • cout << "Application:
    ReportDocs()" << endl;**

  • for (int i = 0; i < _index;i++)**

  •        cout << "  
    

" << _docs[i]->GetName() << endl;**

}*

/
Customization of framework defined by client /*

class
MyApplication: public Application
*

{*

  • public:**

  •        MyApplication()**
    
  •        {**
    
  •                          cout <<
    

"MyApplication: ctor" << endl;**

  •        }**
    
  •        /* Client definesFramework's "hole" */**
    
  •        Document *CreateDocument(char
    

fn)*

  •        {**
    
  •                          cout <<
    

" MyApplication: CreateDocument()" << endl;**

  •                          return new
    

MyDocument(fn);**

  •        }**
    

};*

int
main()
*

{*

  • /* Client's customization of the
    Framework /*

  • MyApplication myApp;**

  • myApp.NewDocument("foo");**

  • myApp.NewDocument("bar");**

  • myApp.ReportDocs();**

}

l Abstract Factory****(抽象工廠)****
**

意圖:**
**

提供一個(gè)創(chuàng)建一系列相關(guān)或相互依賴對(duì)象的接口门粪,而無需指定它們具體的類喊积。

適用性:**
**

一個(gè)系統(tǒng)要獨(dú)立于它的產(chǎn)品的創(chuàng)建、組合和表示時(shí)玄妈。

一個(gè)系統(tǒng)要由多個(gè)產(chǎn)品系列中的一個(gè)來配置時(shí)乾吻。

當(dāng)你要強(qiáng)調(diào)一系列相關(guān)的產(chǎn)品對(duì)象的設(shè)計(jì)以便進(jìn)行聯(lián)合使用時(shí)髓梅。

當(dāng)你提供一個(gè)產(chǎn)品類庫,而只想顯示它們的接口而不是實(shí)現(xiàn)時(shí)绎签。

代碼實(shí)現(xiàn):
**

#include
<iostream.h>
*

class
Shape {
*

  • public:**

  •        Shape() {**
    
  •                 id_ = total_++;**
    
  •        }**
    
  •        virtual void draw() = 0;**
    
  • protected:**

  •        int id_;**
    
  •        static int total_;**
    

};*

int
Shape::total_ = 0;
*

class
Circle : public Shape {
*

  • public:**

  •        void draw() {**
    
  •                 cout <<
    

"circle " << id_ << ":
draw" << endl;**

  •        }**
    

};*

class
Square : public Shape {
*

  • public:**

  •        void draw() {**
    
  •                 cout <<
    

"square " << id_ << ": draw" << endl;**

  •        }**
    

};*

class
Ellipse : public Shape {
*

  • public:**

  •        void draw() {**
    
  •                 cout <<
    

"ellipse " << id_ << ": draw" << endl;**

  •        }**
    

};*

class
Rectangle : public Shape {
*

  • public:**

  •        void draw() {**
    
  •                 cout <<
    

"rectangle " << id_ << ": draw" << endl;**

  •        }**
    

};*

class
Factory {
*

  • public:**

  •        virtual Shape*
    

createCurvedInstance() = 0;**

  •        virtual Shape*
    

createStraightInstance() = 0;**

};*

class
SimpleShapeFactory : public Factory {
*

  • public:**

  •        Shape* createCurvedInstance() {**
    
  •                 return new Circle;**
    
  •        }**
    
  •        Shape* createStraightInstance()
    

{**

  •                 return new Square;**
    
  •        }**
    

};*

class
RobustShapeFactory : public Factory {
*

  • public:**

  •        Shape* createCurvedInstance()  {**
    
  •                 return new Ellipse;**
    
  •        }**
    
  •        Shape* createStraightInstance()
    

{**

  •                 return new Rectangle;**
    
  •        }**
    

};*

int
main() {
*

#ifdef
SIMPLE
*

  • Factory* factory = new
    SimpleShapeFactory;**

#elif
ROBUST
*

  • Factory* factory = new
    RobustShapeFactory;**

#endif*

  • Shape* shapes[3];**

  • shapes[0] =
    factory->createCurvedInstance(); // shapes[0] = new Ellipse;**

  • shapes[1] =
    factory->createStraightInstance(); // shapes[1] = new Rectangle;**

  • shapes[2] =
    factory->createCurvedInstance(); // shapes[2] = new Ellipse;**

  • for (int i=0; i < 3; i++) {**

  •        shapes[i]->draw();**
    
  • }**

}*

l Prototype****(原型)****
**

意圖:**
**

用原型實(shí)例指定創(chuàng)建對(duì)象的種類枯饿,并且通過拷貝這些原型創(chuàng)建新的對(duì)象。

適用性:**
**

當(dāng)要實(shí)例化的類是在運(yùn)行時(shí)刻指定時(shí)诡必,例如鸭你,通過動(dòng)態(tài)裝載。

為了避免創(chuàng)建一個(gè)與產(chǎn)品類層次平行的工廠類層次時(shí)擒权。

當(dāng)一個(gè)類的實(shí)例只能有幾個(gè)不同狀態(tài)組合中的一種時(shí)袱巨。

建立相應(yīng)數(shù)目的原型并克隆它們可能比每次用合適的狀態(tài)手工實(shí)例化該類更方便一些。

代碼實(shí)現(xiàn):
**

#include
<iostream.h>
*

enum
imageType
*

{*

  • LSAT, SPOT**

};*

class
Image
*

{*

  • public:**

  •        virtual void draw() = 0;**
    
  •        static Image
    

findAndClone(imageType);*

  • protected:**

  •        virtual imageType returnType()
    

= 0;**

  •        virtual Image *clone() = 0;**
    
  •        // As each subclass of Image is
    

declared, it registers its prototype**

  •        static void addPrototype(Image
    

image)*

  •        {**
    
  •                          _prototypes[_nextSlot++]
    

= image;**

  •        }**
    
  • private:**

  •        // addPrototype() saves each
    

registered prototype here**

  •        static Image *_prototypes[10];**
    
  •        static int _nextSlot;**
    

};*

Image
Image::_prototypes[];

int
Image::_nextSlot;
*

//
Client calls this public static member function when it needs an instance
*

//
of an Image subclass
*

Image
Image::findAndClone(imageType type)

{*

  • for (int i = 0; i < _nextSlot; i++)**

  •        if
    

(_prototypes[i]->returnType() == type)**

  •                 return _prototypes[i]->clone();**
    

}*

class
LandSatImage: public Image
*

{*

  • public:**

  •        imageType returnType()**
    
  •        {**
    
  •                          return LSAT;**
    
  •        }**
    
  •        void draw()**
    
  •        {**
    
  •                          cout <<
    

"LandSatImage::draw " << _id << endl;**

  •        }**
    
  •        // When clone() is called, call
    

the one-argument ctor with a dummy arg**

  •        Image *clone()**
    
  •        {**
    
  •                          return new
    

LandSatImage(1);**

  •        }**
    
  • protected:**

  •        // This is only called from
    

clone()**

  •        LandSatImage(int dummy)**
    
  •        {**
    
  •                          _id =
    

_count++;**

  •        }**
    
  • private:**

  •        // Mechanism for initializing
    

an Image subclass - this causes the**

  •        // defaultctor to be called, which registers the subclass's prototype**
    
  •        static LandSatImage
    

_landSatImage;**

  •        // This is only called when the
    

private static data member is initiated**

  •        LandSatImage()**
    
  •        {**
    
  •                          addPrototype(this);**
    
  •        }**
    
  •        // Nominal "state"
    

per instance mechanism**

  •        int _id;**
    
  •        static int _count;**
    

};*

//
Register the subclass's prototype
*

LandSatImage
LandSatImage::_landSatImage;
*

//
Initialize the "state" per instance mechanism
*

int
LandSatImage::_count = 1;
*

class
SpotImage: public Image
*

{*

  • public:**

  •        imageTypereturnType()**
    
  •        {**
    
  •                          return SPOT;**
    
  •        }**
    
  •        void draw()**
    
  •        {**
    
  •                          cout <<
    

"SpotImage::draw " << _id << endl;**

  •        }**
    
  •        Image *clone()**
    
  •        {**
    
  •                          return new
    

SpotImage(1);**

  •        }**
    
  • protected:**

  •        SpotImage(int dummy)**
    
  •        {**
    
  •                          _id =
    

_count++;**

  •        }**
    
  • private:**

  •        SpotImage()**
    
  •        {**
    
  •                          addPrototype(this);**
    
  •        }**
    
  •        static SpotImage _spotImage;**
    
  •        int _id;**
    
  •        static int _count;**
    

};*

SpotImage
SpotImage::_spotImage;
*

int
SpotImage::_count = 1;
*

//
Simulated stream of creation requests
*

const
int NUM_IMAGES = 8;
*

*imageType
input[NUM_IMAGES] = **

{*

  • LSAT, LSAT,
    LSAT, SPOT, LSAT, SPOT, SPOT, LSAT**

};*

int
main()
*

{*

  • Image images[NUM_IMAGES];*

  • // Given an image type, find the right
    prototype, and return a clone**

  • for (int i = 0; i < NUM_IMAGES; i++)**

  •        images[i] =
    

Image::findAndClone(input[i]);**

  • // Demonstrate
    that correct image objects have been cloned**

  • for (i = 0; i < NUM_IMAGES; i++)**

  •        images[i]->draw();**
    
  • // Free the dynamic memory**

  • for (i = 0; i < NUM_IMAGES; i++)**

  •        delete images[i];**
    

}*

l Builder(建造者)

意圖:**
**

將一個(gè)復(fù)雜對(duì)象的構(gòu)建與它的表示分離碳抄,使得同樣的構(gòu)建過程可以創(chuàng)建不同的表示愉老。

適用性:**
**

當(dāng)創(chuàng)建復(fù)雜對(duì)象的算法應(yīng)該獨(dú)立于該對(duì)象的組成部分以及它們的裝配方式時(shí)。

當(dāng)構(gòu)造過程必須允許被構(gòu)造的對(duì)象有不同的表示時(shí)剖效。

代碼實(shí)現(xiàn):
**

#include
<iostream.h>
*

#include
<stdio.h>
*

#include
<string.h>
*

enum
PersistenceType
*

{*

  • File, Queue, Pathway**

};*

struct
PersistenceAttribute
*

{*

  • PersistenceType type;**

  • char value[30];**

};*

class
DistrWorkPackage
*

{*

  • public:**

  •        DistrWorkPackage(char *type)**
    
  •        {**
    
  •                          sprintf(_desc,
    

"Distributed Work Package for: %s", type);**

  •        }**
    
  •        void setFile(char *f, char *v)**
    
  •        {**
    
  •                          sprintf(_temp,
    

"\n File(%s): %s", f, v);**

  •                          strcat(_desc,
    

_temp);**

  •        }**
    
  •        void setQueue(char
    

q, char v)

  •        {**
    
  •                          sprintf(_temp,
    

"\n Queue(%s): %s", q, v);**

  •                          strcat(_desc,
    

_temp);**

  •        }**
    
  •        void setPathway(char *p, char
    

v)*

  •        {**
    
  •                          sprintf(_temp,
    

"\n Pathway(%s): %s", p, v);**

  •                          strcat(_desc,
    

_temp);**

  •        }**
    
  •        const char *getState()**
    
  •        {**
    
  •                          return _desc;**
    
  •        }**
    
  • private:**

  •        char _desc[200], _temp[80];**
    

};*

class
Builder
*

{*

  • public:**

  •        virtual void
    

configureFile(char) = 0;*

  •        virtual void
    

configureQueue(char) = 0;*

  •        virtual void
    

configurePathway(char) = 0;*

  •        DistrWorkPackage *getResult()**
    
  •        {**
    
  •                          return _result;**
    
  •        }**
    
  • protected:**

  •        DistrWorkPackage *_result;**
    

};*

class
UnixBuilder: public Builder
*

{*

  • public:**

  •        UnixBuilder()**
    
  •        {**
    
  •                          _result = new
    

DistrWorkPackage("Unix");**

  •        }**
    
  •        void configureFile(char *name)**
    
  •        {**
    
  •                          _result->setFile("flatFile",
    

name);**

  •        }**
    
  •        void configureQueue(char
    

queue)*

  •        {**
    
  •                          _result->setQueue("FIFO",
    

queue);**

  •        }**
    
  •        void configurePathway(char
    

type)*

  •        {**
    
  •                          _result->setPathway("thread",
    

type);**

  •        }**
    

};*

class
VmsBuilder: public Builder
*

{*

  • public:**

  •        VmsBuilder()**
    
  •        {**
    
  •                          _result = new
    

DistrWorkPackage("Vms");**

  •        }**
    
  •        void configureFile(char *name)**
    
  •        {**
    
  •                          _result->setFile("ISAM",
    

name);**

  •        }**
    
  •        void configureQueue(char
    

queue)*

  •        {**
    
  •                          _result->setQueue("priority",
    

queue);**

  •        }**
    
  •        void configurePathway(char
    

type)*

  •        {**
    
  •                          _result->setPathway("LWP", type);**
    
  •        }**
    

};*

class
Reader
*

{*

  • public:**

  •        void setBuilder(Builder *b)**
    
  •        {**
    
  •                          _builder = b;**
    
  •        }**
    
  •        void
    

construct(PersistenceAttribute[], int);**

  • private:**

  •        Builder *_builder;**
    

};*

void
Reader::construct(PersistenceAttribute list[], int num)
*

{*

  • for (int i =
    0; i < num; i++)**

  •        if (list[i].type == File)**
    
  •                 _builder->configureFile(list[i].value);**
    
  •        else if (list[i].type == Queue)**
    
  •                 _builder->configureQueue(list[i].value);**
    
  •        else if (list[i].type ==
    

Pathway)**

  •                 _builder->configurePathway(list[i].value);**
    

}*

const int NUM_ENTRIES = 6;*

*PersistenceAttribute
input[NUM_ENTRIES] = **

{*

  • {**

  •        File, "state.dat"**
    
  • }**

  • , **

  • {**

  •        File, "config.sys"**
    
  • }**

  • , **

  • {**

  •        Queue, "compute"**
    
  • }**

  • , **

  • {**

  •        Queue, "log"**
    
  • }**

  • , **

  • {**

  •        Pathway,
    

"authentication"**

  • }**

  • , **

  • {**

  •        Pathway, "error
    

processing"**

  • }**

};*

int
main()
*

{*

  • UnixBuilder unixBuilder;**

  • VmsBuilder vmsBuilder;**

  • Reader reader;**

  • reader.setBuilder(&unixBuilder);**

  • reader.construct(input, NUM_ENTRIES);**

  • cout <<
    unixBuilder.getResult()->getState() << endl;**

  • reader.setBuilder(&vmsBuilder);**

  • reader.construct(input, NUM_ENTRIES);**

  • cout <<
    vmsBuilder.getResult()->getState() << endl;**

}*

l Facade****(外觀)****
**

意圖:**
**

為子系統(tǒng)中的一組接口提供一個(gè)一致的界面嫉入,F(xiàn)acade模式定義了一個(gè)高層接口,這個(gè)接口使得這一子系統(tǒng)更加容易使用璧尸。
**

適用性:**
**

當(dāng)你要為一個(gè)復(fù)雜子系統(tǒng)提供一個(gè)簡(jiǎn)單接口時(shí)咒林。子系統(tǒng)往往因?yàn)椴粩嘌莼兊迷絹碓綇?fù)雜。大多數(shù)模式使用時(shí)都會(huì)產(chǎn)生更多更小的類爷光。這使得子系統(tǒng)更具可重用性垫竞,也更容易對(duì)子系統(tǒng)進(jìn)行定制,但這也給那些不需要定制子系統(tǒng)的用戶帶來一些使用上的困難蛀序。Facade 可以提供一個(gè)簡(jiǎn)單的缺省視圖欢瞪,這一視圖對(duì)大多數(shù)用戶來說已經(jīng)足夠,而那些需要更多的可定制性的用戶可以越過facade層徐裸。

客戶程序與抽象類的實(shí)現(xiàn)部分之間存在著很大的依賴性遣鼓。引入facade 將這個(gè)子系統(tǒng)與客戶以及其他的子系統(tǒng)分離,可以提高子系統(tǒng)的獨(dú)立性和可移植性重贺。

當(dāng)你需要構(gòu)建一個(gè)層次結(jié)構(gòu)的子系統(tǒng)時(shí)骑祟,使用facade模式定義子系統(tǒng)中每層的入口點(diǎn)。如果子系統(tǒng)之間是相互依賴的气笙,你可以讓它們僅通過facade進(jìn)行通訊次企,從而簡(jiǎn)化了它們之間的依賴關(guān)系。

代碼實(shí)現(xiàn):
**

#include
<iostream.h>
*

class
MisDepartment
*

{*

  • public:**

  •        void submitNetworkRequest()**
    
  •        {**
    
  •                          _state = 0;**
    
  •        }**
    
  •        bool checkOnStatus()**
    
  •        {**
    
  •                          _state++;**
    
  •                          if (_state ==
    

Complete)**

  •                                   return
    

1;**

  •                          return 0;**
    
  •        }**
    
  • private:**

  •        enum States**
    
  •        {**
    
  •                          Received,
    

DenyAllKnowledge, ReferClientToFacilities,**

  •                                   FacilitiesHasNotSentPaperwork,
    

ElectricianIsNotDone,**

  •                                   ElectricianDidItWrong,
    

DispatchTechnician, SignedOff, DoesNotWork,**

  •                                   FixElectriciansWiring,
    

Complete**

  •        };**
    
  •        int _state;**
    

};*

class
ElectricianUnion
*

{*

  • public:**

  •        void submitNetworkRequest()**
    
  •        {**
    
  •                          _state = 0;**
    
  •        }**
    
  •        bool checkOnStatus()**
    
  •        {**
    
  •                          _state++;**
    
  •                          if (_state ==
    

Complete)**

  •                                   return
    

1;**

  •                          return 0;**
    
  •        }**
    
  • private:**

  •        enum States**
    
  •        {**
    
  •                          Received,
    

RejectTheForm, SizeTheJob, SmokeAndJokeBreak,**

  •                                   WaitForAuthorization,
    

DoTheWrongJob, BlameTheEngineer, WaitToPunchOut,**

  •                                   DoHalfAJob,
    

ComplainToEngineer, GetClarification, CompleteTheJob,**

  •                                   TurnInThePaperwork,
    

Complete**

  •        };**
    
  •        int _state;**
    

};*

class
FacilitiesDepartment
*

{*

  • public:**

  •        void submitNetworkRequest()**
    
  •        {**
    
  •                          _state = 0;**
    
  •        }**
    
  •        bool checkOnStatus()**
    
  •        {**
    
  •                          _state++;**
    
  •                          if (_state ==
    

Complete)**

  •                                   return
    

1;**

  •                          return 0;**
    
  •        }**
    
  • private:**

  •        enum States**
    
  •        {**
    
  •                          Received,
    

AssignToEngineer, EngineerResearches, RequestIsNotPossible,**

  •                                   EngineerLeavesCompany,
    

AssignToNewEngineer, NewEngineerResearches,**

  •                                   ReassignEngineer,
    

EngineerReturns, EngineerResearchesAgain,**

  •                                   EngineerFillsOutPaperWork,
    

Complete**

  •        };**
    
  •        int _state;**
    

};*

class
FacilitiesFacade
*

{*

  • public:**

  •        FacilitiesFacade()**
    
  •        {**
    
  •                          _count = 0;**
    
  •        }**
    
  •        void submitNetworkRequest()**
    
  •        {**
    
  •                          _state = 0;**
    
  •        }**
    
  •        bool checkOnStatus()**
    
  •        {**
    
  •                          _count++;**
    
  •                          /* Job
    

request has just been received /*

  •                          if (_state ==
    

Received)**

  •                          {**
    
  •                                            _state++;**
    
  •                                            /* Forward the job request to the
    

engineer /*

  •                                            _engineer.submitNetworkRequest();**
    
  •                                            cout << "submitted to Facilities - "
    

<< _count << **

  •                                                     " phone calls so far" << endl;**
    
  •                          }**
    
  •                          else if
    

(_state == SubmitToEngineer)**

  •                          {**
    
  •                                            /* If engineer is complete, forward
    

to electrician /*

  •                                            if (_engineer.checkOnStatus())**
    
  •                                            {**
    
  •                                                              _state++;**
    
  •                                                              _electrician.submitNetworkRequest();**
    
  •                                                              cout << "submitted to Electrician - "
    

<< _count << **

  •                                                                       " phone calls so far" << endl;**
    
  •                                            }**
    
  •                          }**
    
  •                          else if (_state == SubmitToElectrician)**
    
  •                          {**
    
  •                                            /* If electrician is complete, forward to technician */**
    
  •                                            if (_electrician.checkOnStatus())**
    
  •                                            {**
    
  •                                                              _state++;**
    
  •                                                              _technician.submitNetworkRequest();**
    
  •                                                              cout << "submitted to MIS - " <<_count << **
    
  •                                                                       " phone calls so far" << endl;**
    
  •                                            }**
    
  •                          }**
    
  •                          else if
    

(_state == SubmitToTechnician)**

  •                          {**
    
  •                                            /* If technician is complete, job is done */**
    
  •                                            if (_technician.checkOnStatus())**
    
  •                                                     return 1;**
    
  •                          }**
    
  •                          /* The job is
    

not entirely complete /*

  •                          return 0;**
    
  •        }**
    
  •        int getNumberOfCalls()**
    
  •        {**
    
  •                          return
    

_count;**

  •        }**
    
  • private:**

  •        enum States**
    
  •        {**
    
  •                          Received,
    

SubmitToEngineer, SubmitToElectrician, SubmitToTechnician**

  •        };**
    
  •        int _state;**
    
  •        int _count;**
    
  •        FacilitiesDepartment _engineer;**
    
  •        ElectricianUnion _electrician;**
    
  •        MisDepartment _technician;**
    

};*

int
main()
*

{*

  • FacilitiesFacade facilities;**

  • facilities.submitNetworkRequest();**

  • /* Keep checking until job is complete
    /*

  • while (!facilities.checkOnStatus())**

  •        ;**
    
  • cout << "job completed after only " << facilities.getNumberOfCalls()
    << **

  •        " phone calls"
    

<< endl;**

}*

l Proxy(代理)

**意圖:****
**

為其他對(duì)象提供一種代理以控制對(duì)這個(gè)對(duì)象的訪問健民。

適用性:**
**

在需要用比較通用和復(fù)雜的對(duì)象指針代替簡(jiǎn)單的指針的時(shí)候抒巢,使用Proxy模式。下面是一些可以使用Proxy 模式常見情況:

遠(yuǎn)程代理(Remote Proxy )為一個(gè)對(duì)象在不同的地址空間提供局部代表秉犹。

虛代理(Virtual Proxy )根據(jù)需要?jiǎng)?chuàng)建開銷很大的對(duì)象蛉谜。

保護(hù)代理(Protection Proxy )控制對(duì)原始對(duì)象的訪問。

智能指引(Smart Reference )取代了簡(jiǎn)單的指針崇堵,它在訪問對(duì)象時(shí)執(zhí)行一些附加操作型诚。

代碼實(shí)現(xiàn):
**

class
Subject
*

{*

  • public:**

  •        virtual void execute() = 0;**
    

};*

class
RealSubject: public Subject
*

{*

  •        string str;**
    
  • public:**

  •        RealSubject(string s)**
    
  •        {**
    
  •                          str = s;**
    
  •        }**
    
  •        /*virtual*/void execute()**
    
  •        {**
    
  •                          cout <<
    

str << '\n';**

  •        }**
    

};*

class
ProxySubject: public Subject
*

{*

  •        string first, second, third;**
    
  •        RealSubject *ptr;**
    
  • public:**

  •        ProxySubject(string s)**
    
  •        {**
    
  •                          int num =
    

s.find_first_of(' ');**

  •                          first =
    

s.substr(0, num);**

  •                          s = s.substr(num + 1);**
    
  •                          num =
    

s.find_first_of(' ');**

  •                          second =
    

s.substr(0, num);**

  •                          s =
    

s.substr(num + 1);**

  •                          num =
    

s.find_first_of(' ');**

  •                          third =
    

s.substr(0, num);**

  •                          s =
    

s.substr(num + 1);**

  •                          ptr = new
    

RealSubject(s);**

  •        }**
    
  •        ~ProxySubject()**
    
  •        {**
    
  •                          delete ptr;**
    
  •        }**
    
  •        RealSubject *operator->()**
    
  •        {**
    
  •                          cout <<
    

first << ' ' << second << ' ';**

  •                          return ptr;**
    
  •        }**
    
  •        /*virtual*/void execute()**
    
  •        {**
    
  •                          cout <<
    

first << ' ' << third << ' ';**

  •                          ptr->execute();**
    
  •        }**
    

};*

int
main()
*

{*

  • ProxySubject obj(string("the quick brown fox jumped over the dog"));**

  • obj->execute();**

  • obj.execute();**

}*

l Adapter
Class/Object(適配器)

意圖:**
**

將一個(gè)類的接口轉(zhuǎn)換成客戶希望的另外一個(gè)接口。Adapter 模式使得原本由于接口不兼容而不能一起工作的那些類可以一起工作鸳劳。

適用性:**
**

你想使用一個(gè)已經(jīng)存在的類狰贯,而它的接口不符合你的需求。

你想創(chuàng)建一個(gè)可以復(fù)用的類赏廓,該類可以與其他不相關(guān)的類或不可預(yù)見的類(即那些接口可能不一定兼容的類)協(xié)同工作涵紊。

(僅適用于對(duì)象Adapter )你想使用一些已經(jīng)存在的子類,但是不可能對(duì)每一個(gè)都進(jìn)行子類化以匹配它們的接口幔摸。對(duì)象適配器可以適配它的父類接口摸柄。

**代碼實(shí)現(xiàn):
**

#include
<iostream.h>
*

typedef
int Coordinate;
*

typedef
int Dimension;
*

//
Desired interface
*

class
Rectangle
*

{*

  • public:**

  •        virtual
    

void draw() = 0;**

};*

//
Legacy component
*

class
LegacyRectangle
*

{*

  • public:**

  •        LegacyRectangle(Coordinate x1,
    

Coordinate y1, Coordinate x2, Coordinate y2)**

  •        {**
    
  •                          x1_ = x1;**
    
  •                          y1_ = y1;**
    
  •                          x2_ = x2;**
    
  •                          y2_ = y2;**
    
  •                          cout <<
    

"LegacyRectangle: create. ("
<< x1_ << "," << y1_ << ") => ("**

  •                                   <<
    

x2_ << "," << y2_ << ")" << endl;**

  •        }**
    
  •        void oldDraw()**
    
  •        {**
    
  •                          cout <<
    

"LegacyRectangle: oldDraw. (" << x1_ <<
"," << y1_ << **

  •                                   ")
    

=> (" << x2_ << "," << y2_ <<
")" << endl;**

  •        }**
    
  • private:**

  •        Coordinate x1_;**
    
  •        Coordinate y1_;**
    
  •        Coordinate x2_;**
    
  •        Coordinate y2_;**
    

};*

//
Adapter wrapper
*

class
RectangleAdapter: public Rectangle, private LegacyRectangle
*

{*

  • public:**

  •        RectangleAdapter(Coordinate x,
    

Coordinate y, Dimension w, Dimension h):**

  •                 LegacyRectangle(x, y,
    

x + w, y + h)**

  •        {**
    
  •                          cout <<
    

"RectangleAdapter: create. (" << x << ","
<< y << **

  •                                   "),
    

width = " << w << ", height = " << h <<
endl;**

  •        }**
    
  •        virtual void draw()**
    
  •        {**
    
  •                          cout <<
    

"RectangleAdapter: draw." << endl;**

  •                          oldDraw();**
    
  •        }**
    

};*

int
main()
*

{*

  • Rectangle r = new RectangleAdapter(120,
    200, 60, 40);
    *

  • r->draw();**

}*

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市既忆,隨后出現(xiàn)的幾起案子驱负,更是在濱河造成了極大的恐慌,老刑警劉巖患雇,帶你破解...
    沈念sama閱讀 218,858評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件跃脊,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡苛吱,警方通過查閱死者的電腦和手機(jī)酪术,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來翠储,“玉大人拼缝,你說我怎么就攤上這事≌煤ィ” “怎么了咧七?”我有些...
    開封第一講書人閱讀 165,282評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)任斋。 經(jīng)常有香客問我继阻,道長(zhǎng),這世上最難降的妖魔是什么废酷? 我笑而不...
    開封第一講書人閱讀 58,842評(píng)論 1 295
  • 正文 為了忘掉前任瘟檩,我火速辦了婚禮,結(jié)果婚禮上澈蟆,老公的妹妹穿的比我還像新娘墨辛。我一直安慰自己,他們只是感情好趴俘,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評(píng)論 6 392
  • 文/花漫 我一把揭開白布睹簇。 她就那樣靜靜地躺著奏赘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪太惠。 梳的紋絲不亂的頭發(fā)上磨淌,一...
    開封第一講書人閱讀 51,679評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音凿渊,去河邊找鬼梁只。 笑死,一個(gè)胖子當(dāng)著我的面吹牛埃脏,可吹牛的內(nèi)容都是我干的搪锣。 我是一名探鬼主播,決...
    沈念sama閱讀 40,406評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼彩掐,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼构舟!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起佩谷,我...
    開封第一講書人閱讀 39,311評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤旁壮,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后谐檀,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體抡谐,經(jīng)...
    沈念sama閱讀 45,767評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年桐猬,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了麦撵。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,090評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡溃肪,死狀恐怖免胃,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情惫撰,我是刑警寧澤羔沙,帶...
    沈念sama閱讀 35,785評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站厨钻,受9級(jí)特大地震影響扼雏,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜夯膀,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評(píng)論 3 331
  • 文/蒙蒙 一诗充、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧诱建,春花似錦蝴蜓、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽格仲。三九已至,卻和暖如春汽抚,著一層夾襖步出監(jiān)牢的瞬間抓狭,已是汗流浹背伯病。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評(píng)論 1 271
  • 我被黑心中介騙來泰國打工造烁, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人午笛。 一個(gè)月前我還...
    沈念sama閱讀 48,298評(píng)論 3 372
  • 正文 我出身青樓惭蟋,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親药磺。 傳聞我的和親對(duì)象是個(gè)殘疾皇子告组,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容