Projet

Général

Profil

SpeADL Dynamic Tutorial » Historique » Version 34

Anonyme, 31/10/2014 15:25

1 3 Anonyme
h1. SpeADL for Dynamic Architectures Tutorial
2 2 Anonyme
3 7 Anonyme
{{>toc}}
4
5 1 Anonyme
This is a tutorial for SpeADL: understanding ecosystems and species, defining a simple ecosystem, composing species together with uses.
6 3 Anonyme
7
h2. Objectives
8
9 23 Anonyme
The objective of this tutorial to understand the abstractions of species and uses, how to build architecture with dynamically created components and how to exploit uses to separate concerns in a type-safe way.
10 17 Anonyme
We won't focus on the workflow and mostly present already defined architectures and implementation without explaining how we arrived to that solution.
11 3 Anonyme
12
We will create a Logging ecosystem that contains Logger species.
13
Then we will create a Banking ecosystem that contains Account species that need to log what happens.
14 16 Anonyme
After, we will create a Bank ecosystem that compose Account species with Logger species thanks to the use abstraction.
15
And finally we will add a GUI that will instantiate species and visualise them.
16 3 Anonyme
17
h2. Prerequisites
18
19
It is needed to understand the content of the [[SpeADL Minus Tutorial|SpeADL⁻ tutorial]] before doing this one.
20
21
h2. Creating a New Project and Organisation
22
23
Create a Java project.
24
We will use again an organisation of the package and namespaces as explained in [[MAY Best Practices#Project Organisation|this best practice]].
25
26
h2. The Logging Ecosystem
27
28 32 Anonyme
Logging will be an ecosystem, and its particularity is that it will be able to create Loggers: while logging is the subsystem responsible of all logging, each logger will be responsible of logging one particular aspect of the system. Such an aspect will be identified by a _name_.
29 3 Anonyme
We will have multiple implementation for the Logging ecosystem:
30
* One with only one file where to log with each line prepended with the name.
31
* One with one file per Logger all in the same directory.
32
33 9 Anonyme
In any case, a port will be provided by Logging to create standalone instances of Logger.
34
35 3 Anonyme
h3. Defining the Ecosystem and the Species
36 1 Anonyme
37 8 Anonyme
Create a SpeADL file named _logging.speadl_ in the package _tutorial2.logging_.
38 3 Anonyme
39
Define in it the ecosystem and the species in the right namespace as well as the needed interface:
40
<pre>
41 8 Anonyme
import tutorial2.logging.interfaces.ILog
42 3 Anonyme
43
namespace tutorial2.logging {
44 1 Anonyme
	
45 9 Anonyme
	ecosystem Logging {		
46
		provides create: ICreateLogger
47
48 3 Anonyme
		species Logger(name: String) {
49
			provides log: ILog
50
		}
51
	}
52
	
53
}
54
</pre>
55
56
<pre>
57 8 Anonyme
package tutorial2.logging.interfaces;
58 3 Anonyme
59
public interface ILog {
60
61
	public void addLine(String line);
62 1 Anonyme
}
63 9 Anonyme
64
public interface ICreateLogger {
65
66
	public Logging.Logger.Component createStandaloneLogger(String name);
67
}
68 3 Anonyme
</pre>
69
70 24 Anonyme
h3. Implementing the Ecosystem and the Species, Logging in One File
71 3 Anonyme
72 26 Anonyme
Create a new Java class in _tutorial2.logging.impl_ named _LoggingImplOneFile_ that extends _Logging_, that takes a _File_ as a parameter to the constructor to store the logs, and resolve the error with the Quick Fixes of Eclipse:
73 3 Anonyme
<pre>
74
package tutorial2.logging.impl;
75
76
import java.io.File;
77
import java.io.FileNotFoundException;
78 5 Anonyme
import java.io.FileWriter;
79 3 Anonyme
import java.io.IOException;
80
import java.io.PrintWriter;
81
82 1 Anonyme
import tutorial2.logging.Logging;
83 9 Anonyme
import tutorial2.logging.interfaces.ICreateLogger;
84 3 Anonyme
85 12 Anonyme
public class LoggingImplOneFile extends Logging {
86 3 Anonyme
87
	private PrintWriter logWriter;
88
	
89 12 Anonyme
	public LoggingImplOneFile(File logFile) {
90 3 Anonyme
		// if an exception happens, nothing can be done about it
91
		// we just let logStream be null and
92
		// the operations of logging won't be done
93
		try {
94 5 Anonyme
			this.logWriter = new PrintWriter(new FileWriter(logFile), true);
95 3 Anonyme
		} catch (FileNotFoundException e) {
96 5 Anonyme
			System.err.println("An error happened with the file, nothing will be logged.");
97 1 Anonyme
			this.logWriter = null;
98 3 Anonyme
		} catch (IOException e) {
99 5 Anonyme
			System.err.println("An error happened with the file, nothing will be logged.");
100 3 Anonyme
			this.logWriter = null;
101
		}
102 1 Anonyme
	}
103 9 Anonyme
104
	@Override
105
	protected ICreateLogger make_create() {
106
		// TODO Auto-generated method stub
107
		return null;
108
	}
109 3 Anonyme
	
110
	@Override
111
	protected Logger make_Logger(String name) {
112
		// TODO Auto-generated method stub
113
		return null;
114
	}
115
}
116
</pre>
117
118
As we can see, the species we defined needs to be implemented and this implementation needs to be returned by the method _Logger make_Logger(String name)_.
119
120 27 Anonyme
Let's define an inner class inside _LoggingImplOneFile_ to do that, it will take as a parameter to the constructor the _name_ of the Logger and will exploit the _logWriter_ to do the actual logging.
121 3 Anonyme
<pre>
122 12 Anonyme
public class LoggingImplOneFile extends Logging {
123 3 Anonyme
124
	private PrintWriter logWriter;
125
	
126
	// ...
127
	
128
	@Override
129
	protected Logger make_Logger(String name) {
130
		return new LoggerImpl(name);
131
	}
132
	
133
	private class LoggerImpl extends Logger implements ILog {
134
135
		private final String name;
136
		
137
		public LoggerImpl(String name) {
138
			this.name = name;
139
		}
140
		
141
		@Override
142
		protected ILog make_log() {
143
			return this;
144 1 Anonyme
		}
145
146
		@Override
147 5 Anonyme
		public void addLine(String line) {
148 1 Anonyme
			if (logWriter != null) {
149 5 Anonyme
				logWriter.println("["+name+"] "+ line);
150
			}
151
		}
152
	}
153
}
154
</pre>
155 1 Anonyme
156 29 Anonyme
Then let's implement the create port by exploiting the method _newLogger(String name)_ present in the extended class and that instantiates the species:
157 9 Anonyme
<pre>
158 12 Anonyme
public class LoggingImplOneFile extends Logging {
159 9 Anonyme
160
	// ...
161
	
162
	@Override
163
	protected ICreateLogger make_create() {
164
		return new ICreateLogger() {
165
			@Override
166
			public Logger.Component createStandaloneLogger(String name) {
167
				return newLogger(name);
168
			}
169
		};
170
	}
171
172
	// ...
173
}
174 10 Anonyme
</pre>
175 9 Anonyme
176 5 Anonyme
We now have a complete implementation for the Logging ecosystem.
177 9 Anonyme
Let's test it with a very simple program that we put in the package _tutorial2.logging.tests_*:
178 5 Anonyme
<pre>
179
package tutorial2.logging.tests;
180
181 1 Anonyme
import java.io.File;
182
183 9 Anonyme
import tutorial2.logging.Logging;
184
import tutorial2.logging.Logging.Logger;
185 12 Anonyme
import tutorial2.logging.impl.LoggingImplOneFile;
186 5 Anonyme
187 9 Anonyme
public class LoggingTest {
188
	public static void main(String[] args) {
189 5 Anonyme
190 12 Anonyme
		Logging.Component logging = new LoggingImplOneFile(new File("/tmp/tutorial2-logging-test.txt")).newComponent();
191 5 Anonyme
192
		// create new Loggers
193 9 Anonyme
		Logger.Component l1 = logging.create().createStandaloneLogger("a");
194
		Logger.Component l2 = logging.create().createStandaloneLogger("b");
195
		Logger.Component l3 = logging.create().createStandaloneLogger("c");
196
197 5 Anonyme
		// log things to them
198
		l1.log().addLine("1 a says hi");
199
		l1.log().addLine("2 test test");
200
		l2.log().addLine("3 b says hoy");
201
		l1.log().addLine("4 blabla");
202
		l3.log().addLine("5 hop");
203 6 Anonyme
	}
204 5 Anonyme
}
205
</pre>
206
207
Execute it and confirm that the file _/tmp/tutorial2-logging-test.txt_ contains the following:
208
<pre>
209
[a] 1 a says hi
210 3 Anonyme
[a] 2 test test
211
[b] 3 b says hoy
212 1 Anonyme
[a] 4 blabla
213
[c] 5 hop
214 6 Anonyme
</pre>
215
216 25 Anonyme
h3. Implementing the Ecosystem and the Species, Logging in Many Files
217 6 Anonyme
218
We can now define a second implementation that stores the logs of each Logger in its own file:
219
<pre>
220
package tutorial2.logging.impl;
221
222
import java.io.File;
223
import java.io.FileNotFoundException;
224
import java.io.FileWriter;
225
import java.io.IOException;
226
import java.io.PrintWriter;
227
228
import tutorial2.logging.Logging;
229 9 Anonyme
import tutorial2.logging.interfaces.ICreateLogger;
230 1 Anonyme
import tutorial2.logging.interfaces.ILog;
231
232 12 Anonyme
public class LoggingImplDirectory extends Logging {
233 1 Anonyme
234
	private final File logDir;
235
236 12 Anonyme
	public LoggingImplDirectory(File logDir) {
237 1 Anonyme
		this.logDir = logDir;
238
		this.logDir.mkdirs();
239 6 Anonyme
	}
240
	
241
	@Override
242
	protected Logger make_Logger(String name) {
243
		return new LoggerImpl(new File(logDir, name+".txt"));
244
	}
245 9 Anonyme
246
	@Override
247
	protected ICreateLogger make_create() {
248
		return new ICreateLogger() {
249
			@Override
250
			public Logger.Component createStandaloneLogger(String name) {
251
				return newLogger(name);
252
			}
253
		};
254
	}
255 6 Anonyme
	
256
	private class LoggerImpl extends Logger implements ILog {
257
258
		private PrintWriter logWriter;
259
		
260
		public LoggerImpl(File logFile) {
261
			// if an exception happens, nothing can be done about it
262
			// we just let logStream be null and
263
			// the operations of logging won't be done
264
			try {
265
				this.logWriter = new PrintWriter(new FileWriter(logFile), true);
266
			} catch (FileNotFoundException e) {
267
				System.err.println("An error happened with the file, nothing will be logged.");
268
				this.logWriter = null;
269
			} catch (IOException e) {
270
				System.err.println("An error happened with the file, nothing will be logged.");
271
				this.logWriter = null;
272
			}
273 1 Anonyme
		}
274
		
275
		@Override
276
		protected ILog make_log() {
277
			return this;
278
		}
279
280
		@Override
281
		public void addLine(String line) {
282
			if (logWriter != null) {
283
				logWriter.println(line);
284
			}
285
		}
286
	}
287
}
288
</pre>
289
290 9 Anonyme
It can be tested with the previous class by changing the first line instantiating the component:
291 1 Anonyme
<pre>
292 12 Anonyme
Logging.Component logging = new LoggingImplDirectory(new File("/tmp/tutorial2-logging-test/")).newComponent();
293 9 Anonyme
</pre>
294 1 Anonyme
295 9 Anonyme
Execute it and confirm that the _/tmp/tutorial2-logging-test/_ directory contains one file per Logger with the correct content.
296 1 Anonyme
297 9 Anonyme
h2. The Banking Ecosystem 
298 1 Anonyme
299 9 Anonyme
Banking will also be an ecosystem, it will contain Account that are species with a particularity: they have required ports!
300 33 Anonyme
Because of that, they can't simply be created by themselves but must be composed with other components or species to be usable.
301 1 Anonyme
302 9 Anonyme
h3. Defining the Ecosystem and the Species
303
304
Create a SpeADL file named _banking.speadl_ in the package _tutorial2.banking_ and define a _Banking_ ecosystem in _tutorial2.banking_ namespace:
305
<pre>
306
import tutorial2.banking.interfaces.IAccountOperations
307
import tutorial2.banking.interfaces.IAccountStatus
308
import tutorial2.logging.interfaces.ILog
309
310
namespace tutorial2.banking {
311 1 Anonyme
	
312 9 Anonyme
	ecosystem Banking {
313
		
314
		species Account(owner: String) {
315
			
316
			provides operations: IAccountOperations
317
			provides status: IAccountStatus
318
			
319
			requires log: ILog
320
		}
321 1 Anonyme
	}
322
}
323
</pre>
324
325 9 Anonyme
Then the interfaces in _tutorial2.banking.interfaces_:
326
<pre>
327
package tutorial2.banking.interfaces;
328 1 Anonyme
329 9 Anonyme
public interface IAccountOperations {
330 1 Anonyme
331 9 Anonyme
	public void deposit(int value);
332
	public void withdraw(int value);
333
}
334 34 Anonyme
</pre>
335
<pre>
336
package tutorial2.banking.interfaces;
337 1 Anonyme
338 9 Anonyme
public interface IAccountStatus {
339
340
	public int getBalance();
341
}
342
</pre>
343
344
h3. Implementing the Ecosystem and the Species
345
346
We now  can define an implementation named _BankingImpl_ in the package _tutorial2.banking.impl_.
347
It exploits the provided ports and the required ports of the species, but also the required port of the ecosystem:
348
<pre>
349
package tutorial2.banking.impl;
350
351
import java.util.concurrent.atomic.AtomicInteger;
352
353
import tutorial2.banking.Banking;
354
import tutorial2.banking.interfaces.IAccountOperations;
355
import tutorial2.banking.interfaces.IAccountStatus;
356
357
public class BankingImpl extends Banking {
358
	
359
	@Override
360
	protected Account make_Account(final String owner) {
361
		return new Account() {
362
			
363
			private final AtomicInteger balance = new AtomicInteger();
364
			
365
			@Override
366
			protected void start() {
367
				eco_requires().elog().addLine("Added a new account for "+owner);
368
			}
369
			
370
			@Override
371
			protected IAccountOperations make_operations() {
372
				return new IAccountOperations() {
373
					
374
					@Override
375
					public void withdraw(int value) {
376
						provides().operations().deposit(-value);
377
						requires().log().addLine(value+" were withdrawn, current balance: "+provides().status().getBalance());
378
					}
379
					
380
					@Override
381
					public void deposit(int value) {
382
						balance.addAndGet(value);
383
						requires().log().addLine(value+" were deposited, current balance: "+provides().status().getBalance());
384
					}
385
				};
386
			}
387
388
			@Override
389
			protected IAccountStatus make_status() {
390
				return new IAccountStatus() {
391
					@Override
392
					public int getBalance() {
393
						return balance.get();
394
					}
395
				};
396
			}
397
		};
398
	}
399
}
400 1 Anonyme
</pre>
401
402 11 Anonyme
Notice the calls to *provides()* and *requires()*.
403
404
Now let's add a bit more log for when a species is instantiated: it must be logged in a file specific to the ecosystem and not to the species.
405
Let's add a required port to the ecosystem _Banking_:
406
<pre>
407
requires elog: ILog
408
</pre>
409
410
Then let's add a *start()* method to the implementation of  _Account_ in _BankingImpl_:
411
<pre>
412
	@Override
413
	protected void start() {
414 1 Anonyme
		eco_requires().elog().addLine("Added a new account for "+owner);
415
	}
416 11 Anonyme
</pre>
417
418
Notice the use of  *eco_requires()* from within the species to access the ports of the ecosystem.
419
420
h2. Composing with Uses into a Bank Application
421
422
Now that we have the two functionality we need, we can build our application to compose them together.
423
In order to do that, we need to define an ecosystem with a species that "uses" the other two.
424
425 12 Anonyme
h3. Defining the Ecosystem and the Species
426
427 11 Anonyme
Let's define the ecosystem _Bank_ in a SpeADL file named _bank.speadl_ in the package and namespace _tutorial2.bank_:
428
<pre>
429
import tutorial2.banking.Banking
430
import tutorial2.banking.interfaces.IAccountOperations
431
import tutorial2.banking.interfaces.IAccountStatus
432
import tutorial2.logging.Logging
433
import tutorial2.logging.interfaces.ILog
434
435
namespace tutorial2.bank {
436
	
437
	ecosystem Bank {
438
		
439
		provides elog: ILog
440
		
441
		part l: Logging
442
		
443
		part b: Banking {
444
			bind elog to elog
445
		}
446
		
447 19 Anonyme
		species BankAccount(owner: String) {
448 11 Anonyme
			
449 1 Anonyme
			use ll: l.Logger(owner)
450
			use ba: b.Account(owner) {
451
				bind log to ll.log
452
			}
453
		}
454
	}
455
}
456
</pre>
457
458 12 Anonyme
Notice the following points:
459 19 Anonyme
* Each of the ecosystem that are needed are declared as parts of the _Bank_ ecosystem, and the species we defined in them are declared as uses of the species _BankAccount_.
460 12 Anonyme
* In order to bind the required port of _b: Banking_, the containing ecosystem needs to provide a port that will be used in the binding. This port is not meant to be used from the exterior but from inside the ecosystem.
461
* The required ports of the _Account_ species are provided by the _Logger_ species.
462
463 19 Anonyme
The idea behind such a composition is that when an instance of _BankAccount_ is created, then it implies that an instance of _Logger_ and an instance of _Account_ are created at the same time and used as parts of _BankAccount_.
464 12 Anonyme
Each of the species retains its links to its own ecosystem without leaking it to the outside.
465
466
h3. Implementing the Ecosystem and the Species
467
468
Implementing the ecosystem is quite straightforward as there is not so much things to add in the implementation.
469
Let's create _BankImpl_ in the package _tutorial2.bank.impl_:
470
<pre>
471
package tutorial2.bank.impl;
472
473
import tutorial2.bank.Bank;
474
import tutorial2.banking.Banking;
475
import tutorial2.logging.Logging;
476
import tutorial2.logging.interfaces.ILog;
477
478
public class BankImpl extends Bank {
479
480
	@Override
481
	protected ILog make_elog() {
482
		// TODO Auto-generated method stub
483
		return null;
484
	}
485
486
	@Override
487
	protected Logging make_l() {
488
		// TODO Auto-generated method stub
489
		return null;
490
	}
491
492
	@Override
493
	protected Banking make_b() {
494
		// TODO Auto-generated method stub
495
		return null;
496
	}
497
}
498
</pre>
499
500 31 Anonyme
Notice that the _make_BankAccount(String owner)_ method that should normally be overridden does not need to because the species does not have any provided port to implement nor parts. 
501
502 12 Anonyme
First, let's fill the methods for the parts:
503
<pre>
504
public class BankImpl extends Bank {
505
506
	private final File logDir;
507
508
	public BankImpl(File logDir) {
509
		this.logDir = logDir;
510
	}
511
512
	@Override
513
	protected Logging make_l() {
514
		return new LoggingImplDirectory(logDir);
515
	}
516
517
	@Override
518 19 Anonyme
	protected Banking make_b() {
519
		return new BankingImpl();
520 12 Anonyme
	}
521
522
	// ...
523
}
524
</pre>
525
526
Finally, let's use the _Logging_ component to get a special _Logger_ for the whole ecosystem:
527
<pre>
528
public class BankImpl extends Bank {
529
530
	// ...
531
532 1 Anonyme
	@Override
533 12 Anonyme
	protected ILog make_elog() {
534 30 Anonyme
		Logging.Logger.Component eLogger = parts().l().create().createStandaloneLogger("BANK");
535
		return eLogger.log();
536 12 Anonyme
	}
537
538
	// ...
539
}
540
</pre>
541 13 Anonyme
542 30 Anonyme
Notice that because provided ports are initialised after parts (see the [[SpeADL_Minus_Java_Reference#Lifecycle-of-Component-Initialisation-at-Instantiation|lifecycles of components initialisation]]), it is possible to directly call the _create_ port of the _l_ part to instantiate a _Logger_ and return directly the port of the instance.
543 13 Anonyme
544
h2. Adding a GUI to the Bank Ecosystem
545
546 19 Anonyme
In order for the _Bank_ ecosystem to be usable, we need to be able to create new _BankAccount_ and interact with them.
547 13 Anonyme
We could add provided ports and interact with it, but here we are going to define another ecosystem encapsulating a GUI for managing the accounts.
548
This GUI could have been directly added to the _Bank_ ecosystem as it is quite empty, but for the sake of pedagogy, we create another ecosystem in order to show more examples and idioms.
549
550
h3. Preparing the Other Ecosystems
551
552
We need first to add a method to _IAccountStatus_ to be able to get the owner of an account:
553
<pre>
554
	public String getOwner();
555
</pre>
556
557
The implementation in the species of _BankingImpl_ just returns the owner:
558
<pre>
559
	@Override
560
	public String getOwner() {
561
		return owner;
562
	}
563
</pre>
564
565
566
Then we need to add a way to instantiate a new account in the _Bank_ ecosystem, let's add a port for that in the ecosystem:
567
<pre>
568
ecosystem Bank {		
569
	provides manage: IBankManage
570
	// ...
571
}
572
</pre>
573
574
<pre>
575
package tutorial2.bank.interfaces;
576
577
public interface IBankManage {
578
	public void instantiateAccount(String owner);
579
}
580
</pre>
581
582 19 Anonyme
Its implementation in _BankImpl_ simply calls _newBankAccount(owner)_:
583 13 Anonyme
<pre>
584
	@Override
585
	protected IBankManage make_manage() {
586
		return new IBankManage() {
587
			@Override
588
			public void instantiateAccount(String owner) {
589 19 Anonyme
				newBankAccount(owner);
590 13 Anonyme
			}
591
		};
592
	}
593
</pre>
594
595
Notice that we don't return the created instance because we won't need it in the GUI: everything will be done using bindings of ports between the uses.
596
597
h3. Defining the Ecosystem
598
599
Let's define a _BankGUI_ ecosystem in a SpeADL file named _gui.speadl_ in the package and namespace _tutorial2.gui_:
600 14 Anonyme
<pre>
601
import tutorial2.bank.interfaces.IBankManage
602
import tutorial2.banking.interfaces.IAccountOperations
603
import tutorial2.banking.interfaces.IAccountStatus
604
605
namespace tutorial2.gui {
606
	
607
	ecosystem BankGUI {
608
		
609
		requires manage: IBankManage
610
		
611
		species AccountGUI {
612
			requires operations: IAccountOperations
613
			requires status: IAccountStatus
614
		}
615
	}
616
}
617
</pre>
618
619
The species _AccountGUI_ is responsible of the part of the GUI related to one account, while the ecosystem itself is responsible of the whole frame.
620
In order to manipulate the account, the species has some required ports, and in order to trigger the instantiation of the species, the ecosystem has also some required ports.
621
622
We then need to update the _Bank_ ecosystem as follows in order to exploit _BankGUI_:
623
<pre>
624
namespace tutorial2.bank {
625
	
626
	ecosystem Bank {
627
		
628
		provides manage: IBankManage
629
		provides elog: ILog
630
		
631
		part l: Logging
632
		
633
		part b: Banking {
634
			bind elog to elog
635
		}
636
		
637
		part gui: BankGUI {
638
			bind manage to manage
639
		}
640
		
641 19 Anonyme
		species BankAccount(owner: String) {
642 14 Anonyme
			
643
			use ll: l.Logger(owner)
644
			use ba: b.Account(owner) {
645
				bind log to ll.log
646
			}
647
			use ga: gui.AccountGUI {
648
				bind operations to ba.operations
649
				bind status to ba.status
650
			}
651
		}
652
	}
653
}
654
</pre>
655
656 19 Anonyme
Now, when a new _BankAccount_ is instantiated, the species _AccountGUI_ will also be along the other ones.
657 14 Anonyme
We can exploit that to define in the implementation what happens at that point.
658
659
h3. Implementing the Ecosystem and Species
660
661
The implementation relies on all these ports to instantiate the species when the user clicks on a button.
662
This instantiation will trigger the instantiation of the _AccountGUI_ species, and we will add a row in our GUI for this account.
663
Using the required ports, we can fill the row and call the operations on the account.
664
665
<pre>
666
package tutorial2.gui.impl;
667
668
import java.awt.BorderLayout;
669
import java.awt.event.ActionEvent;
670
import java.awt.event.ActionListener;
671
672
import javax.swing.BoxLayout;
673
import javax.swing.JButton;
674
import javax.swing.JFormattedTextField;
675
import javax.swing.JFrame;
676
import javax.swing.JLabel;
677
import javax.swing.JPanel;
678
import javax.swing.JTextField;
679
import javax.swing.SwingUtilities;
680
681
import tutorial2.gui.BankGUI;
682
683
public class BankGUIImpl extends BankGUI {
684
685
	private final JFrame f = new JFrame();
686
	private final JPanel accPanel = new JPanel();
687
	
688
	@Override
689
	protected void start() {
690
		SwingUtilities.invokeLater(new Runnable() {
691
			@Override
692
			public void run() {				
693
				final JTextField tfAccName = new JTextField("OwnerName");
694
				tfAccName.setColumns(15);
695
				final JButton bAddAccount = new JButton("Add");
696
				final JPanel addAccPanel = new JPanel();
697
				addAccPanel.add(tfAccName);
698
				addAccPanel.add(bAddAccount);								
699
				bAddAccount.addActionListener(new ActionListener() {
700
					@Override
701
					public void actionPerformed(ActionEvent e) {
702
						if (!tfAccName.getText().isEmpty()) {
703
							requires().manage().instantiateAccount(tfAccName.getText());
704
							tfAccName.setText("");
705
						}
706
					}
707
				});
708
				f.getContentPane().add(addAccPanel, BorderLayout.PAGE_START);
709
				accPanel.setLayout(new BoxLayout(accPanel, BoxLayout.Y_AXIS));
710
				f.getContentPane().add(accPanel, BorderLayout.CENTER);
711
			    
712
			    f.pack();
713
			    f.setVisible(true);
714
			    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
715
			}
716
		});
717
	}
718
	
719
	@Override
720
	protected AccountGUI make_AccountGUI() {
721
		return new AccountGUI() {
722
			
723
			private final JPanel panel = new JPanel();
724
			private final JLabel balance = new JLabel();
725
			
726
			@Override
727
			protected void start() {
728
				panel.add(new JLabel(requires().status().getOwner()));
729
				panel.add(balance);
730
				final JFormattedTextField amount = new JFormattedTextField(new Integer(0));
731
				amount.setColumns(5);
732
				panel.add(amount);
733
				final JButton bPlus = new JButton("+");
734
				bPlus.addActionListener(new ActionListener() {
735
					@Override
736
					public void actionPerformed(ActionEvent e) {
737
						final int toAdd = ((Number) amount.getValue()).intValue();
738
						requires().operations().deposit(toAdd);
739
						updateBalance();
740
					}
741
				});
742
				panel.add(bPlus);
743
				final JButton bMinus = new JButton("-");
744
				bMinus.addActionListener(new ActionListener() {
745
					@Override
746
					public void actionPerformed(ActionEvent e) {
747
						final int toRem = ((Number) amount.getValue()).intValue();
748
						requires().operations().withdraw(toRem);
749
						updateBalance();
750
					}
751
				});
752
				panel.add(bMinus);
753
				final JButton bRem = new JButton("Remove");
754
				bRem.addActionListener(new ActionListener() {
755
					@Override
756
					public void actionPerformed(ActionEvent e) {
757
						accPanel.remove(panel);
758
						f.pack();
759
					}
760
				});
761
				panel.add(bRem);
762
				updateBalance();
763
				accPanel.add(panel);
764
				f.pack();
765
			}
766
			
767
			private void updateBalance() {
768
				balance.setText(""+requires().status().getBalance());
769
			}
770
		};
771
	}
772
}
773
</pre>
774
775
We can notice that the remove button will simply remove the panel that contains references to the species.
776 19 Anonyme
Because the elements of the _BankAccount_ species and its sub-components are all related to each others but not at all with the exterior, the fact that this only reference between the GUI and the species disappear means  that the garbage collector of the JVM will remove the instance of the species and its content from the memory.
777 15 Anonyme
778
h3. Executing
779
780 20 Anonyme
We can finally define a class to execute all of that:
781
<pre>
782
package tutorial2;
783
784
import java.io.File;
785
786
public class Application {
787
788
	public static void main(String[] args) {
789
		new BankImpl(new File("/tmp/tutorial2-bank-logs/")).newComponent();
790
	}
791
}
792
</pre>
793 18 Anonyme
794
h2. Conclusion
795
796
This tutorial for SpeADL finishes here.
797
798
h1. Wiki Page Resources