.


:




:

































 

 

 

 


. - ,




- , .

, , . .

Virtual Constructor ( ).

. , .

, . Application Document. , . , , DrawingApplication DrawingDocument. Application , , Open () New ().

, Document , , Application , . , , . : , , .

. , Document , .

 

 

Application

CreateDocument , Document. Application , , . CreateDocument , .

 

, :

- , ;

- , , , ;

- , , .

 

 

 

 

- Product (Document) - :

- , ;

- ConcreteProduct (MyDocument) - :

- Product;

- Creator (Application) - :

- , Product.

Creator -

, ConcreteProduct;

- Product.

- ConcreteCreator (MyApplication) - :

- , ConcreteProduct.

, .

. Product, .

 

, , , Creator ConcreteProduct. , Creator, .

 

:

- - (hooks). , . - .

Document CreateFileDialog, . , . , ;

- . , , . : , .

, , .

, , , : , .

-

. . , , , . -, . , , - .

- Manipulator, . , Manipulator. Manipulator ( , ) Figure.

Figure CreateManipulator, . Figure , Manipulator. Figure CreateManipulator , Manipulator , Figure

 

 

. ,

, ,

.

, . , .

 

, :

- . -, , Creator . : Creator , . , , ;

, . , . Creator , . : , . , , ;

- . , .

, . , , Product. Application . CreateDocument , , .

Unidraw [VL90] ', .

Unidraw Creator Create, , , . Unidraw , , . .

, Create, . Create .

, Create Read , .

( My Product Your Product - Product):

 

class Creator {

public:

virtual Product* Create(Productld);

};

Product* Creator::Create (Productld id) {

if (id == MINE) return new MyProduct;

if (id == YOURS) return new YourProduct;

// ...

return 0;

}

 

, . .

, MyCreator MyProduct YourProduct Their Product:

 

Product* MyCreator::Create (Productld id) {

if (id == YOURS) return new MyProduct;

if (id == MINE) return new YourProduct;

// N.B.: YOURS MINE

if (id == THEIRS) return new TheirProduct;

return Creator::Create(id); // ,

/ /

}

 

, Create . , MyCreator:: Create

 

YOURS, MINE THEIRS , . MyCreator , ;

- - . .

, Smalltalk , . Creator , a ConcreteCreator . ConcreteProduct .

Document Smalltalk documentClass Application. Document . documentClass MyApplication MyDocument. , Application clientMethod

 

document:= self documentClass new.

documentClass

self subclassResponsibility

MyApplication

documentClass

^ MyDocument

 

MyDocument, -

Application.

, , , Application. Application.

C++ , . Creator: ConcreteCreator .

, , . , . , , . , . . :

 

class Creator {

public:

Product* GetProduct();

protected:

virtual Product* CreateProduct();

private:

Product* _product;

};

Product* Creator::GetProduct () {

if (.product == 0) {

_product = CreateProduct ();

}

return _product;

}

 

- , . , , , -. C++ , Creator, Product:

 

class Creator {

public:

virtual Product* CreateProduct () = 0;

};

template <class TheProduct>

class StandardCreator: public Creator {

public:

virtual Product* CreateProduct();

};

template <class TheProduct>

Product* StandardCreator<TheProduct>::CreateProduct () {

return new TheProduct;

}

 

, Creator :

 

class MyProduct: public Product {

public:

MyProduct();

//...

};

StandardCreator<MyProduct> myCreator;

 

- . , , . , Macintosh [App89] , , Class* DoMakeClass (), Class - .

CreateMaze . , , , . , .

MazeGame , , :

 

class MazeGame {

public:

Maze* CreateMaze();

// :

virtual Maze* MakeMazeO const

{ return new Maze; }

virtual Room* MakeRoom(int n) const

{ return new Room(n); }

virtual Wall* MakeWalK) const

{ return new Wall; }

virtual Door* MakeDoor(Room* rl, Room* r2) const

{ return new Door(rl, r2); }

};

 

. MazeGame , , , .

CreateMaze

:

 

Maze* MazeGame::CreateMaze () {

Maze* aMaze = MakeMaze();

Room* rl = MakeRoom(l);

Room* r2 = MakeRoom(2);

Door* theDoor = MakeDoor(rl, r2);

aMaze->AddRoom(rl);

aMaze->AddRoom(r2);

rl->SetSide(North, MakeWall());

rl->SetSide(East, theDoor);

rl->SetSide(South, MakeWall());

rl->SetSide(West, MakeWall());

r2->SetSide(North, MakeWall());

r2->SetSide(East, MakeWall());

r2->SetSide(South, MakeWall());

r2->SetSide(West, theDoor);

return aMaze;

}

 

MazeGame . , . , BombedMazeGame Room Wall , :

 

class BombedMazeGame: public MazeGame {

public:

BombedMazeGame();

virtual Wall* MakeWall() const

{ return new BombedWall; }

virtual Room* MakeRoom(int n) const

{ return new RoomWithABomb(n); }

};

 

- Enchant edMazeGame :

 

class EnchantedMazeGame: public MazeGame {

public:

EnchantedMazeGame();

virtual Room* MakeRoomdnt n) const

{ return new EnchantedRoom(n, CastSpell()); }

virtual Door* MakeDoor(Room* rl, Room* r2) const

{ return new DoorNeedingSpell(rl, r2); }

protected:

Spell* CastSpell() const;

};

. - ++ [WGM88]. Unidraw.

View // Smalltalk-80 defaultController, , []. View , def aultControllerClass, , defaultController. , def aultControllerClass, , .

parserClass,

Smalltalk-80, Behavior ( , ). . , SQLParser , SQL. Behavior par serClass , Smalltalk Parser. , SQL, ( ) SQLParser.

Orbix ORB IONA Technologies [ION94] (. ) , . , , .

.

. .

.

NewDocument - .

Creator. Initialize Product.

Treator Initialize .

.

 

 

Prototype

Prototype

- , .

- .

, , . . , . , , , , . , ,: , .

, Graphic , Tool . , GraphicTool , .

GraphicTool

. , GraphicTool . , . GraphicTool , , , . , . , GraphicTool Graphic, .

- GraphicTool , ≪≫ Graphic. . GraphicTool , . Graphic Clone, GraphicTool .

, - GraphicTool, . GraphicTool , .

, . , , , . , . GraphicTool, MusicalNote . .

.

, , , :

- , ;

- , ;

- . , .

- Prototype (Graphic) - :

- ;

- ConcretePrototype (S t a f f - , WholeNote - , Half Note - ) - :

- ;

- Client (GraphicTool) - :

- , .

, .

, : , . , .

:

- . , -. , , ;

- . - , , - . , , - . , .

. . . GraphicTool ;

- .

. , .1 , , , .

. . , , , Clone , ;

- . Creator, .

,

. Creator . C++, . Smalltalk Objective , - . - ;

- . . - C++.

.

, , . (. ≪≫). , . ++ [WGM88] .

, Prototype Clone, . , Clone, . , .

C++, , . " , Smalltalk Objective , ( - -) ::≫ . , , Self [US87], , .

 

, :

- . ( ), . , . . .

- , , . . ≪≫ , , ;

- Clone. - Clone. , .

. , Smalltalk , Object. C++ . [GR83]. : ?

, . Smalltalk. C++ , . , . , . , .

Save () Load (), Clone , . Save , a Load , ;

. , . Clone , . , . Clone .

, .

, . , , Initialize (. ≪ ≫), . , Clone : ( Initialize) .

MazePrototypeFactory MazeFactory.

, , .

 

MazePrototypeFactory MazeFactory , :

 

class MazePrototypeFactory: public MazeFactory {

public:

MazePrototypeFactory(Maze*, Wall*, Room*, Door*);

virtual Maze* MakeMaze() const;

virtual Room* MakeRoom(int) const;

virtual Wall* MakeWalK) const;

virtual Door* MakeDoor(Room*, Room*) const;

private:

Maze* _prototypeMaze;

Room* __prototypeRoom;

Wall* _prototypeWall;

Door* _prototypeDoor;

};

:

MazePrototypeFactory::MazePrototypeFactory (

Maze* m, Wall* w, Room* r, Door* d

) {

_prototypeMaze = m;

_prototypeWall = w;

_prototypeRoom = r;

_prototypeDoor = d;

}

 

- , :

, . ..'akeWall MakeDoor:

 

Wall* MazePrototypeFactory::MakeWall () const {

return _prototypeWall->Clone();

}

Door* MazePrototypeFactory::MakeDoor (Room* rl, Room *r2) const {

Door* door = _prototypeDoor->Clone();

door->Initialize(rl, r2);

return door;

}

 

MazePrototypeFactory , :

 

MazeGame game;

MazePrototypeFactory simpleMazeFactory(

new Maze, new Wall, new Room, new Door

);

Maze* maze = game.CreateMaze(simpleMazeFactory);

 

MazePrototypeFactory

. BombedDoor RoomWithABomb:

 

 

MazePrototypeFactory bombedMazeFactory(

new Maze, new BombedWall,

new RoomWithABomb, new Door

);

, , -

Wall, Clone. .

.

.

Door Initialize, -

.

Door . 91:

class Door: public MapSite {

public:

Door();

Door(const Door&);

virtual void Initialize(Room*, Room*);

virtual Door* Clone() const;

virtual void Enter();

Room* OtherSideFrom(Room*);

private:

Room* _rooml;

Room* _room2;

};

Door::Door (const Door& other) {

_rooml = other._rooml;

_room2 = other._room2;

}

void Door::Initialize (Room* rl, Room* r2) {

_rooml = rl;

_room2 = r2;

}

Door* Door::Clone () const {

return new Door(*this);

}

BombedWall Clone

:

class BombedWall: public Wall {

public:

BombedWall();

BombedWall(const BombedWallk);

virtual Wall* Clone() const;

bool HasBomb();

private:

bool _bomb;

};

Prototype

BombedWall::BombedWall (const BombedWallk other): Wall(other) {

_bomb = other._bomb;

}

Wall* BombedWall::Clone () const {

return new BombedWall(*this);

}

BombedWall:: Clone Wall*, - -

, BombedWal 1 *. Clone

, , -

, . -

, Clone, .

Smalltalk , -

Object, MapSite.

MazeFactory -

. , #room.

MazeFactory , .

make: :

k

make: partName

^ (partCatalog at: partName) copy

MazeFactory ,

:.

CreateMaze

on: (MazeFactory new

with: Door new named: #door;

with: Wall new named: #wall;

with: Room new named: #room;

yourself)

on: CreateMaze

on: aFactory

| rooml room2 |

rooml:= (aFactory make: #room). location: 1@1.

room2:= (aFactory make: #room) location: 2@1.

door:= (aFactory make: #door) from: rooml to: room2.

rooml

atSide: #north put: (aFactory make: #wall);

atSide: #east put: door;

atSide: #south put: (aFactory make: #wall);

atSide: #west put: (aFactory make: #wall).

room2

atSide: #north put: (aFactory make: #wall);

atSide: #east put: (aFactory make: #wall);

atSide: #south put: (aFactory make: #wall);

atSide: #west put: door.

^ Maze new

addRoom: rooml;

addRoom: room2;

yourself

, Sketchpad

(Ivan Sutherland) [Sut63]. -

- Thing-

Lab, , -

,

[81]. -

[GR83], [92]

. C++ -

.

Etgdb - ++,

point-and-click ( ) .

DebuggerAdaptor. , GdbAdaptor

etgdb GNU gdb, a SunDbxAdaptor - -

dbx Sun. DebuggerAdaptor ≪≫ etgdb.

,

, . etgdb

, DebuggerAdaptor, -

.

Mode Composer

, -

[Sha90]. Mode Composer -

, .

-

.

, , -

Unidraw [VL90].

-

. .

, .

, -

, .


Singleton





:


: 2015-11-23; !; : 636 |


:

:

.
==> ...

1521 - | 1356 -


© 2015-2024 lektsii.org - -

: 0.338 .