Another thing to point out is that the Add and Remove operations return new set
values. This is because set values, like .NET strings, are immutable. There is no way
to change their values. However, we can update a state variable with the new value
that results from adding or removing an element:
clients = clients.Add(client);
If you were to explore the model program shown above for the client/server
system, you would notice that the following two traces result in the same end
state:
Trace 1 Trace 2
AddClient("alice") AddClient("bob")
AddClient("bob") AddClient("alice")
Modeling complex state often needs mathematical data structures like sets. Using
sets whenever order doesn??™t matter can dramatically reduce the number of states you
will have to consider for design analysis and testing. Sets are the bread and butter
of experienced software modelers. They appear very often.
Creating sets
The easiest way to create a set is to list its elements in the constructor. The order in
which arguments appear doesn??™t matter and duplicates will be ignored:
4 When we use the word ???equals,??? we mean equality in the sense of the Object.Equals method.
Systems with Complex State 161
Set
cities = new Set("Athens", "Rome", "Athens",
"Paris", "New York", "Seattle");
Assert.AreEqual(cities.Count, 5);
You can also create a set from an IEnumerable object.
Pages:
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229