Beruflich Dokumente
Kultur Dokumente
CSE8313
Economic Goal
Design Principles
Features/Concerns
1.
Maze Type:
red, blue, enchanted, bombed, HarryPotter, SnowWhite Each type requires a *family* of components
2. 3.
4.
Component Building:
How many walls a room can have? 4? 8? How to build a door?
5.
Current code
public Maze createMaze (MazeFactory factory) { Maze aMaze = factory.makeMaze(); Room r1 = factory.makeRoom(1); Room r2 = factory.makeRoom(2); Door theDoor = factory.makeDoor(r1, r2); aMaze.addRoom(r1); aMaze.addRoom(r2); r1.setSide(MapSite.NORTH, factory.makeWall()); r1.setSide(MapSite.EAST, theDoor); r1.setSide(MapSite.SOUTH, factory.makeWall()); r1.setSide(MapSite.WEST, factory.makeWall()); r2.setSide(MapSite.NORTH, factory.makeWall()); r2.setSide(MapSite.EAST, factory.makeWall()); r2.setSide(MapSite.SOUTH, factory.makeWall()); r2.setSide(MapSite.WEST, theDoor); return aMaze; }
Simplification
We would like to factor out the knowledge about how to assemble Rooms. Solution?
Hire a contractor A Builder And just give orders: Act as the Director of the work
Product
Builder Participants
Builder
specifies an abstract interface for creating parts of a Product object constructs and assembles parts of the product by implementing the Builder interface defines and keeps track of the representation it creates provides an interface for retrieving the product demands the construction of an object using the Builder interface represents the complex object under construction. Concrete builder builds the products internal representation and defines the process by which it is assembled
ConcreteBuilder
Director
Product
Builder: motivation
The algorithm for creating a complex object should be independent of the parts that make up the object and how theyre assembled The construction process must allow different representations for the object thats constructed
Simplify the code of the CreateMaze method by passing it a MazeBuilder as a parameter. MazeBuilder interface can be used to build three things
1) the maze 2) rooms with a particular room number 3) doors between numbered rooms.
abstract
The operations in the abstract MazeBuilder super-class are meant to be overridden by subclasses, i.e. concrete builders. Concrete builders will override also GetMaze() to return the Maze they build
Modified code
the internal representation that is classes that define rooms, doors and walls of the maze how these parts are assembled to form the final maze.
This makes easy to change the way Maze is represented since no client code is dependent on it. For instance we might have windows in the representation of rooms
Subdividing responsibility between the Maze and Builder classes and separating the two
Enabled reusability of part of the construction process Can have a variety of MazeBuilders each constructing mazes with different classes for rooms, walls, doors.
What was the basis for the decision which part of the construction remains in the MazeCreator, and what is delegated to Builder?
Find what must vary and extract it, hide it. The varying parts: type of walls, doors, rooms varies, The stable parts: e.g. the fact that rooms are connected by doors. (What if this varies too? )
Intent: Intent: Separate the construction complex object from its Separate the construction ofof aa complex object from its representation so that the same construction process can create representation so that the same construction process can create different representations. different representations.
17
18
the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled the construction process must allow different representations for the object that's constructed
19
(TextConverter)
20
Collaborations
21
Consequences
The Builder object provides the director with an abstract interface for constructing the product. The Builder pattern improves modularity by encapsulating the way a complex object is constructed and represented. The Builder pattern constructs the product step by step under the director's control.
22
Implementation
Builders construct their products in step-by-step fashion. Therefore the Builder class interface must be general enough to allow the construction of products for all kinds of concrete builders.
23
MazeBuilder Example
class MazeBuilder { public: virtual void BuildMaze() { } virtual void BuildRoom(int room) { } virtual void BuildDoor(int roomFrom, int roomTo) { } virtual Maze* GetMaze() { return 0; } protected: MazeBuilder(); };
Maze* MazeGame::CreateMaze (MazeBuilder& builder) { builder.BuildMaze(); builder.BuildRoom(1); builder.BuildRoom(2); builder.BuildDoor(1, 2); return builder.GetMaze(); }
24
MazeBuilder Example
class StandardMazeBuilder : public MazeBuilder { public: StandardMazeBuilder(); virtual void BuildMaze(); virtual void BuildRoom(int); virtual void BuildDoor(int, int); virtual Maze* GetMaze(); private: Direction CommonWall(Room*, Room*); Maze* _currentMaze; };
StandardMazeBuilder::StandardMazeBuilder () { _currentMaze = 0; } void StandardMazeBuilder::BuildMaze () { _currentMaze = new Maze; } Maze* StandardMazeBuilder::GetMaze () { _currentMaze; }
return
25
MazeBuilder Example
void StandardMazeBuilder::BuildDoor (int n1, int n2) { Room* r1 = _currentMaze->RoomNo(n1); Room* r2 = _currentMaze->RoomNo(n2); Door* d = new Door(r1, r2); r1->SetSide(CommonWall(r1,r2), d); r2->SetSide(CommonWall(r2,r1), d); }
void StandardMazeBuilder::BuildRoom (int n) { if (!_currentMaze->RoomNo(n)) { Room* room = new Room(n); _currentMaze->AddRoom(room); room->SetSide(North, new Wall); room->SetSide(South, new Wall); room->SetSide(East, new Wall); room->SetSide(West, new Wall); } }
26
CountingMazeBuilder Example
CountingMazeBuilder::CountingMazeBuilder () { _rooms = _doors = 0; } void CountingMazeBuilder::BuildRoom (int) { _rooms++; } void CountingMazeBuilder::BuildDoor (int, int) { _doors++; } void CountingMazeBuilder::GetCounts ( int& rooms, int& doors ) const { rooms = _rooms; doors = _doors; }
class CountingMazeBuilder : public MazeBuilder { public: CountingMazeBuilder(); virtual void BuildMaze(); virtual void BuildRoom(int); virtual void BuildDoor(int, int); virtual void AddWall(int, Direction); void GetCounts(int&, int&) const; private: int _doors; int _rooms; };
int rooms, doors; MazeGame game; CountingMazeBuilder builder; game.CreateMaze(builder); builder.GetCounts(rooms, doors); cout << "The maze has " << rooms << " rooms and " 27 << doors << " doors" << endl;
What is the difference between Abstract Factory pattern and Builder pattern?
28
The concrete builder SimpleMazeBuilder is an implementation that builds simple mazes. Lets take a look at its code:
Maze myMaze; Maze getMaze() { return myMaze; } void buildMaze() { myMaze = new Maze(); } void buildRoom (int i) { r = new Room(i): myMaze.addRoom(r); // all room-construction code }
SimpleMazeBuilder
FactoryMazeBuilder
SpellDoor BombedDoor
EnchantedWall BombedWall
EnchantedRoom BombedRoom 1
Maze
BombedMazeFactory +Maze makeMaze() +Wall makeWall() +Room makeRoom() +Door makeDoor() MazeBuilder
+Room buildRoom() +Door buildDoor()
uses
FactoryMazeBuilder uses
+Room buildRoom() +Door buildDoor()
uses MazeGameCreator
+Maze createMaze(in MazeFactory Factory)
SimpleMazeBuilder
+Room buildRoom() +Door buildDoor()
Creational patterns
Creational patterns involve object instantiation and all provide a way to decouple a client from objects it needs to instantiate Some members of this group:
Intent
The intent of Factory Method is to allow a class to defer instantiation to its subclasses The intent of Abstract Factory is to create families of related objects without explicitly tying the code on their concrete classes The intent of Builder is to encapsulate the construction of composite structures
There are cases in which you need one object of a certain type
One and only one Shared globally across the system Abstraction for accessing a single resource (e.g. log file) A load balancer object A centralized Factory A dialog box that is used across the GUI
Examples:
Requirements
Enforce uniqueness:
make sure nobody can create further instances use information hiding mechanisms you dont want the clients of that object to be responsible for checking uniqueness and for usage policy encapsulate that responsibility within the class of that object
Responsibility:
Multi-threading
What if multiple clients try to obtain the handle to the singleton at the same time?
Is uniqueness guaranteed?
The Singleton Pattern ensures a class has only one instance, and provides a global point of access to it.
Singleton
instance is a private class attribute instance() is a class (static) operation. Creates and maintains its own unique instance.
Classification:
Context: You want to have only one object of a class, but no global object controls the instantiation of this object. You want to ensure all clients reference this object without passing a handle to all of them Problem: several clients need regulated access to the same thing, and you want to ensure uniqueness Solution: guarantees one and only on instance Consequences:
Clients do not need to care about existence or uniqueness of singleton instance They do not need to pass around references
Pattern team-up
Singleton + Factory Method How do they work together? The product created by the Factory Method can be a Singleton
Singleton variant
Why one and only one? What about N and no more than N? Can you leverage the Singleton pattern to manage N objects that can be re-used?
vs.
When you instantiate an object with the new operator you create it out of thin air:
e.g. in Java , the JVM goes through a complex process of loading, memory allocation, initialization
Especially when the object is complex and/or large
Prototype pattern
A prototype is an object that packs in advance all the necessary info for further instances Instead of instantiating new instances, clients clone the prototype Notice that cloning carries over the full state of the prototype
Examples:
Object contains data that is complex to compute Object contains lots of data Object contains data that are loaded from a slow medium You want to give the same data to multiple workers
Prototype - structure
Client +Operation() Prototype +Clone()