-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSWE20190918_Approf.tex
More file actions
502 lines (415 loc) · 13.7 KB
/
SWE20190918_Approf.tex
File metadata and controls
502 lines (415 loc) · 13.7 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{graphicx}
\graphicspath{ {./images/} }
\usepackage[a4paper, total={6in, 8in}]{geometry}
\title{Approfondimento}
\author{Filmon Arefayne}
\date{Settembre 2019}
\begin{document}
\maketitle
\section{Introduzione}
Il progetto scelto approfondisce il design pattern \textbf{Command} in particolare si è incentrato nello sviluppo di un applicazione con interfaccia grafica che simula una SmartHome. In questa SmartHome si hanno tre telecomandi che rappresentano tre stanze (\textbf{Bedroom},\textbf{Kitchen} e \textbf{LivingRoom}) e per ogni stanza si hanno due Receiver \textbf{Fan} e \textbf{Light} che devono poter rispondere ai comandi rispettivamente \textbf{Start/Stop} e \textbf{turnOn/turnOff}.
\\
Il problema consiste nella progettazione di una classe \textbf{SmartHomeRemote}, in grado di inoltrare richieste verso oggetti che saranno noti solo in fasi successive di sviluppo. Inoltre per l'implementazione della funzione \textbf{undo} è stato aggiunto uno oggetto \textbf{Stack} per mantenere in memoria le operazioni effettuate.
L'applicazione permette di selezionare la stanza e il comando da eseguire.L'ultimo comando eseguito viene mandato in output e una \textbf{JTextArea} permette di visualizzare lo status della SmartHome.Inoltre è possibile fare undo sulle ultime operazioni eseguite o resettare completamente l'applicazione.
\begin{figure}[ht]
\caption{L'applicazione in esecuzione}
\centering
\includegraphics[width=\textwidth]{images/Approfondimento1.PNG}
\end{figure}
\section{Codice}
\begin{lstlisting}[language=Java]
// Command.java
package com.filmon.businesslogic;
public interface Command {
public String execute();
public String undo();
}
// SmartHomeRemote.java
package com.filmon.businesslogic;
public class SmartHomeRemote {
private Command command;
public void setCommand(Command command) {
this.command = command;
}
public String buttonPressed() {
return command.execute();
}
public String undoPressed() {
return command.undo();
}
}
// Fan.java
package com.filmon.businesslogic;
import java.util.Stack;
public class Fan {
private boolean state = false;
private Stack<Boolean> previousStates = new Stack<Boolean>();
public String start() {
previousStates.add(state);
if(!state) {
state = true;
return status();
}else
return "Fan is unchanged";
}
public String stop() {
previousStates.add(state);
if(state) {
state = false;
return status();
}else
return "Fan is unchanged";
}
public String undo(){
boolean ps;
if(!previousStates.isEmpty()) {
ps = previousStates.pop();
if (ps == state) {
return "Undoing nothing";
} else {
state = ps;
return status();
}
}
return "Undoing nothing";
}
public String status(){
if(state){
return "Fan Started.";
}
return "Fan Stopped.";
}
}
// Light.java
package com.filmon.businesslogic;
import java.util.Stack;
public class Light {
private boolean state = false;
private Stack<Boolean> previousStates = new Stack<Boolean>();
public String turnOn() {
previousStates.add(state);
if(!state) {
state = true;
return status();
}else
return "Light is unchanged";
}
public String turnOff() {
previousStates.add(state);
if(state) {
state = false;
return status();
}else
return "Light is unchanged";
}
public String undo(){
boolean ps;
if(!previousStates.isEmpty()) {
ps = previousStates.pop();
if (ps == state) {
return "Undoing nothing";
} else {
state = ps;
return status();
}
}
return "Undoing nothing";
}
public String status(){
if(state){
return "Light is on.";
}
return "Light is off.";
}
}
// StartFanCommand.java
package com.filmon.businesslogic;
public class StartFanCommand implements Command {
private Fan fan;
public StartFanCommand(Fan fan) {
this.fan = fan;
}
@Override
public String execute() {
String temp;
temp = "Starting Fan...\n";
return temp + fan.start();
}
@Override
public String undo() {
String temp;
temp = "Undoing...\n";
return temp + fan.undo();
}
}
// StopFanCommand.java
package com.filmon.businesslogic;
public class StopFanCommand implements Command{
private Fan fan;
public StopFanCommand(Fan fan) {
this.fan = fan;
}
@Override
public String execute() {
String temp;
temp = "Stopping Fan...\n";
return temp + fan.stop();
}
@Override
public String undo() {
String temp;
temp = "Undoing...\n";
return temp + fan.undo();
}
}
// TurnOnLightCommand.java
package com.filmon.businesslogic;
public class TurnOnLightCommand implements Command{
private Light light;
public TurnOnLightCommand(Light light) {
this.light = light;
}
@Override
public String execute() {
String temp;
temp = "Turning on Light...\n";
return temp + light.turnOn();
}
@Override
public String undo() {
String temp;
temp = "Undoing...\n";
return temp + light.undo();
}
}
// TurnOffLightCommand.java
package com.filmon.businesslogic;
public class TurnOffLightCommand implements Command {
private Light light;
public TurnOffLightCommand(Light light) {
this.light = light;
}
@Override
public String execute() {
String temp;
temp = "Turning off Light...\n";
return temp + light.turnOff();
}
@Override
public String undo() {
String temp;
temp = "Undoing...\n";
return temp + light.undo();
}
}
// App.java
package com.filmon;
import com.filmon.businesslogic.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
public class App {
private JPanel panelMain;
private JButton buttonBed;
private JButton buttonKitchen;
private JButton buttonLiving;
private JComboBox<COMMAND> comboBoxBed;
private JComboBox<COMMAND> comboBoxKitchen;
private JComboBox<COMMAND> comboBoxLiving;
private JButton buttonUndo;
private JButton buttonReset;
private JTextArea textAreaOutput;
private JTextArea textAreaStatus;
private Stack<SmartHomeRemote> shrs;
private Light bedLight;
private Fan bedFan;
private Light kitchenLight;
private Fan kitchenFan;
private Light livingLight;
private Fan livingFan;
private SmartHomeRemote bedRemote;
private SmartHomeRemote kitchenRemote;
private SmartHomeRemote livingRemote;
private enum COMMAND {
StartLight,
StopLight,
StartFan,
StopFan
}
public App() {
reset();
buttonBed.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
textAreaOutput.setText(bedRemote.buttonPressed());
shrs.add(bedRemote);
update();
}
});
buttonKitchen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
textAreaOutput.setText(kitchenRemote.buttonPressed());
shrs.add(kitchenRemote);
update();
}
});
buttonLiving.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
textAreaOutput.setText(livingRemote.buttonPressed());
shrs.add(livingRemote);
update();
}
});
comboBoxBed.addItem(COMMAND.StartLight);
comboBoxBed.addItem(COMMAND.StopLight);
comboBoxBed.addItem(COMMAND.StartFan);
comboBoxBed.addItem(COMMAND.StopFan);
comboBoxKitchen.addItem(COMMAND.StartLight);
comboBoxKitchen.addItem(COMMAND.StopLight);
comboBoxKitchen.addItem(COMMAND.StartFan);
comboBoxKitchen.addItem(COMMAND.StopFan);
comboBoxLiving.addItem(COMMAND.StartLight);
comboBoxLiving.addItem(COMMAND.StopLight);
comboBoxLiving.addItem(COMMAND.StartFan);
comboBoxLiving.addItem(COMMAND.StopFan);
comboBoxBed.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Command command;
switch (comboBoxBed.getSelectedIndex()){
case 0:
command = new TurnOnLightCommand(bedLight);
break;
case 1:
command = new TurnOffLightCommand(bedLight);
break;
case 2:
command = new StartFanCommand(bedFan);
break;
default:
case 3:
command = new StopFanCommand(bedFan);
break;
}
bedRemote.setCommand(command);
}
});
comboBoxKitchen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Command command;
switch (comboBoxKitchen.getSelectedIndex()){
case 0:
command = new TurnOnLightCommand(kitchenLight);
break;
case 1:
command = new TurnOffLightCommand(kitchenLight);
break;
case 2:
command = new StartFanCommand(kitchenFan);
break;
default:
case 3:
command = new StopFanCommand(kitchenFan);
break;
}
kitchenRemote.setCommand(command);
}
});
comboBoxLiving.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Command command;
switch (comboBoxLiving.getSelectedIndex()){
case 0:
command = new TurnOnLightCommand(livingLight);
break;
case 1:
command = new TurnOffLightCommand(livingLight);
break;
case 2:
command = new StartFanCommand(livingFan);
break;
default:
case 3:
command = new StopFanCommand(livingFan);
break;
}
livingRemote.setCommand(command);
}
});
comboBoxBed.setSelectedItem(COMMAND.StartLight);
comboBoxKitchen.setSelectedItem(COMMAND.StartLight);
comboBoxLiving.setSelectedItem(COMMAND.StartLight);
textAreaOutput.setEnabled(false);
textAreaStatus.setEnabled(false);
buttonUndo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
SmartHomeRemote undoRemote;
if (!shrs.isEmpty()){
undoRemote = shrs.pop();
textAreaOutput.setText(undoRemote.undoPressed());
update();
}
}
});
buttonReset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
reset();
comboBoxBed.setSelectedItem(COMMAND.StartLight);
comboBoxKitchen.setSelectedItem(COMMAND.StartLight);
comboBoxLiving.setSelectedItem(COMMAND.StartLight);
}
});
}
private void reset(){
shrs = new Stack<>();
bedLight = new Light();
kitchenLight = new Light();
livingLight = new Light();
bedFan = new Fan();
kitchenFan = new Fan();
livingFan = new Fan();
bedRemote = new SmartHomeRemote();
kitchenRemote = new SmartHomeRemote();
livingRemote = new SmartHomeRemote();
textAreaOutput.setText("");
textAreaStatus.setText("");
}
private void update(){
textAreaStatus.setText(
"Bedroom: \n" +
"Light:"+bedLight.status()+"\n"+
"Fan:"+bedFan.status()+"\n\n"+
"Kitchen: \n" +
"Light:"+kitchenLight.status()+"\n"+
"Fan:"+kitchenFan.status()+"\n\n"+
"Living: \n" +
"Light:"+livingLight.status()+"\n"+
"Fan:"+livingFan.status()+"\n"
);
}
public static void main(String[] args) {
JFrame frame = new JFrame("App");
frame.setContentPane(new App().panelMain);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
}
}
\end{lstlisting}
\begin{figure}[ht]
\caption{Class Diagram dell'applicazione}
\centering
\includegraphics[width=\textwidth]{images/ApprofondimentoUML.PNG}
\end{figure}
\end{document}