Wrapping a collection class

The following is a wrapper design that contains an internal collection:

class Deck:
def __init__(self) -> None:
self._cards = [card(r + 1, s)
for r in range(13) for s in iter(Suit)]
random.shuffle(self._cards)

def pop(self) -> Card:
return self._cards.pop()

We've defined Deck so that the internal collection is a list object. The pop() method of Deck simply delegates to the wrapped list object.

We can then create a Hand instance with the following type of code:

d = Deck() 
hand = [d.pop(), d.pop()] 

Generally, a Facade design pattern or wrapper class contains methods that delegate the work to the underlying implementation class. This delegation can become wordy when a lot of features are provided. For a sophisticated collection, we may wind up delegating a large number of methods to the wrapped object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset