-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractFactory.py
More file actions
61 lines (52 loc) · 2.43 KB
/
AbstractFactory.py
File metadata and controls
61 lines (52 loc) · 2.43 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Different classes denoting different types of objects having a common characteristic: Knight, Rook & Bishop
class Knight:
#One of many classes having a common characteristic
def directionOfMovement(self):
return "Ni horizontal ni vertical."
def stepsInMovement(self):
return "Dos y medio"
def __str__(self):
return "Caballero"
class Rook:
#One of many classes having a common characteristic'''
def directionOfMovement(self):
return "Horizontal o verticalmente."
def stepsInMovement(self):
return "Tantas como 7."
def __str__(self):
return "Fumar"
class Bishop:
#One of many classes having a common characteristic'''
def directionOfMovement(self):
return "Diagonalmente."
def stepsInMovement(self):
return "Tantas como 7."
def __str__(self):
return "Alfil"
# Concrete Factories having getter methods to return objects of above classes: KnightFactory, RookFactory & BishopFactory
class KnightFactory:
#Concrete Factory based on a class; returns an object of the corresponding class'''
def getPiece(self):
return Knight()
class RookFactory:
#Concrete Factory based on a class; returns an object of the corresponding class'''
def getPiece(self):
return Rook()
class BishopFactory:
#Concrete Factory based on a class; returns an object of the corresponding class'''
def getPiece(self):
return Bishop()
# Abstract Factory which takes a concrete factory object as input, obtains the object from the factory, and provides a method to expose details of the object: Piece Factory
class PieceFactory:
#An abstract factory which takes a concrete factory object as input, obtains the object from the factory, and provides a method to expose details of the object. '''
def __init__(self, pieceFactory):
self._pieceFactory = pieceFactory
def detailsOfChosenPiece(self):
#utility method to display details of object returned by the abstract factory'''
chosenPiece = self._pieceFactory.getPiece()
print("Pieza elegida:", chosenPiece)
print("Dirección de la pieza elegida:", chosenPiece.directionOfMovement())
print("Número de pasos que puede mover la pieza elegida:", chosenPiece.stepsInMovement())
objectOfConcreteFactory = KnightFactory()
objectOfAbstractFactory = PieceFactory(objectOfConcreteFactory)
objectOfAbstractFactory.detailsOfChosenPiece()