forked from prmr/DesignBook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObservableDeck2.java
More file actions
179 lines (156 loc) · 3.74 KB
/
ObservableDeck2.java
File metadata and controls
179 lines (156 loc) · 3.74 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*******************************************************************************
* Companion code for the book "Introduction to Software Design with Java"
* by Martin P. Robillard.
*
* Copyright (C) 2019 by Martin P. Robillard
*
* This code is licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* See http://creativecommons.org/licenses/by-nc-nd/4.0/
*******************************************************************************/
package chapter8;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
/**
* Represents a deck of playing cards. In this version, the cards in the
* deck are stored in a list and the list of cards in the deck can
* be obtained by client code using an immutable wrapper object.
*/
public class ObservableDeck2 implements DeckView
{
private final List<Card> aCards = new ArrayList<>();
private final List<DeckObserver2> aObservers = new ArrayList<>();
private Optional<Card> aLastDrawn = Optional.empty();
/**
* Creates a new deck of 52 cards, shuffled.
*/
public ObservableDeck2()
{
shuffle();
}
public Card getLastDrawn()
{
return aLastDrawn.get();
}
public void addObserver(DeckObserver2 pObserver)
{
assert pObserver != null;
aObservers.add(pObserver);
}
/**
* Reinitializes the deck with all 52 cards, and shuffles them.
*/
public void shuffle()
{
aCards.clear();
for( Suit suit : Suit.values() )
{
for( Rank rank : Rank.values() )
{
aCards.add( Card.get( rank, suit ));
}
}
Collections.shuffle(aCards);
for(DeckObserver2 observer : aObservers)
{
observer.shuffled(this);
}
}
/**
* Places pCard on top of the deck.
* @param pCard The card to place on top
* of the deck.
* @pre pCard !=null
*/
public void push(Card pCard)
{
assert pCard != null;
aCards.add(pCard);
for(DeckObserver2 observer : aObservers)
{
observer.cardPushed(this);
}
}
/**
* Draws a card from the deck: removes the card from the top
* of the deck and returns it.
* @return The card drawn.
* @pre !isEmpty()
*/
public Card draw()
{
assert !isEmpty();
Card card = aCards.remove(aCards.size() - 1);
aLastDrawn = Optional.of(card);
for(DeckObserver2 observer : aObservers)
{
observer.cardDrawn(this);
}
return card;
}
/**
* @return True if and only if there are no cards in the deck.
*/
public boolean isEmpty()
{
return aCards.isEmpty();
}
/**
* @return An unmodifiable list of all the cards in the deck.
*/
public List<Card> getCards()
{
return Collections.unmodifiableList(aCards);
}
}
interface DeckObserver2
{
void shuffled(DeckView pDeckView);
void cardDrawn(DeckView pDeckView);
void cardPushed(DeckView pDeckView);
}
class DrawLogger2 implements DeckObserver2
{
@Override
public void shuffled(DeckView pDeckView)
{}
@Override
public void cardDrawn(DeckView pDeckView)
{
System.out.println(pDeckView.getLastDrawn() + " drawn");
}
@Override
public void cardPushed(DeckView pDeckView)
{}
}
class SizeStatus2 implements DeckObserver2
{
@Override
public void shuffled(DeckView pDeckView)
{
System.out.println(pDeckView.size());
}
@Override
public void cardDrawn(DeckView pDeckView)
{
System.out.println(pDeckView.size());
}
@Override
public void cardPushed(DeckView pDeckView)
{
System.out.println(pDeckView.size());
}
}
interface DeckView
{
List<Card> getCards();
boolean isEmpty();
Card getLastDrawn();
default int size()
{
return getCards().size();
}
}