It’s a long time that I didn’t do something meaningful. Maybe because I am in happiness now.
J
Many time I spend with my GF, a little decadence
J
Several days ago, I see the book about design pattern in JAVA. Now note one down. About iterator design.
Here is the UML:
The Aggregate.java
1
public
?
interface
?Aggregate
2
{
3
????
public
?Iterator?iterator();
4
}
5
?
The Book.java
?1
public
?
class
?Book
?2
{
?3
????
private
?String?name?
=
?
""
;
?4
????
?5
????
public
?Book(String?name)
?6
????
{
?7
????????
this
.name?
=
?name;
?8
????}
?9
????
public
?String?getName()
10
????
{
11
????????
return
?name;
12
????}
13
}
The BookShelf.java
?1
public
?
class
?BookShelf?
implements
?Aggregate
?2
{
?3
????
private
?Book[]?books;
?4
????
private
?
int
?last?
=
?
0
;
?5
????
private
?
int
?maxlength;
?6
????
public
?BookShelf(
int
?max)
?7
????
{
?8
????????books?
=
?
new
?Book[max];
?9
????????maxlength?
=
?max;
10
????}
11
????
12
????
public
?Book?getBookAt(
int
?index)
13
????
{
14
????????
if
(index
<
0
)
15
????????
{
16
????????????System.out.println(
"
The?index?is?below?zero!
"
);
17
????????????
return
?
null
;
18
????????}
19
????????
else
?
if
(index
>
maxlength)
20
????????
{
21
????????????System.out.println(
"
Ths?index?is?too?large!
"
);
22
????????????
return
?
null
;
23
????????}
24
????????
else
???
25
????????????
return
?books[index];
26
????}
27
????
28
????
public
?
void
?appendBook(Book?book)
29
????
{
30
????????books[last
++
]
=
book;
31
????}
32
????
public
?
int
?getLength()
33
????
{
34
????????
return
?last;
35
????}
36
????
37
????
public
?
int
?getMaxLength()
38
????
{
39
????????
return
?maxlength;
40
????}
41
????
public
?Iterator?iterator()
42
????
{
43
????????
return
?
new
?BookShelfIterator(
this
);
44
????}
45
}
46
????
47
????????
The BookShelfIterator.java
?1
public
?
class
?BookShelfIterator?
implements
?Iterator
?2
{
?3
????
private
?BookShelf?bookShelf;?
//
specific?which?shelf
?4
????
int
?index;?
//
select?the?shelf?and?then?the?index,which?book
?5
????
?6
????
public
?BookShelfIterator(BookShelf?bookShelf)
?7
????
{
?8
????????
this
.bookShelf?
=
?bookShelf;
?9
????????index?
=
?
0
;
10
????}
11
????
12
????
public
?
boolean
?hasNext()
13
????
{
14
????????
if
(index
<
bookShelf.getLength())
15
????????
{
16
????????????
return
?
true
;
17
????????}
18
????????
else
19
????????
{
20
????????????
return
?
false
;
21
????????}
22
????}
23
????
24
????
public
?Object?next()
25
????
{
26
????????
return
?bookShelf.getBookAt(index
++
);
27
????}
28
????
29
????
public
?Object?previous()
30
????
{
31
????????
return
?bookShelf.getBookAt(index
--
);
32
????}
33
}
34
????????
The Iterator.java
1
public
?
interface
?Iterator
2
{
3
????
public
?
boolean
?hasNext();
4
????
public
?Object?next();
5
????
public
?Object?previous();
6
}
MainBook.java
?1
public
?
class
?MainBook
?2
{
?3
????
public
?
static
?
void
?main(String[]?args)
?4
????
{
?5
????????String[]?newBooks?
=
?
{
"
GNU?Linux
"
,
"
GNU?CC
"
,
"
QT
"
}
;
//
,"Gnome"};
//
,"KDE"};???
//
for?test?larger,smaller?and?equal
?6
????????BookShelf?bookShelf?
=
?
new
?BookShelf(
4
);
?7
????????
boolean
?flag?
=
?
true
;
?8
????????
int
?i?
=
?
0
;????
?9
????????
10
????????
while
(flag)
11
????????
{
12
????????????bookShelf.appendBook(
new
?Book(newBooks[i
++
]));
13
????????????
if
(bookShelf.getLength()
==
bookShelf.getMaxLength())??
//
the?shelf?is?full
14
????????????
{
15
????????????????flag?
=
?
false
;
16
????????????????
break
;
17
????????????}
18
????????????
19
????????????
if
(i
==
newBooks.length)?????
//
new?books?traverse?all?
20
????????????
{
21
????????????????
break
;
22
????????????}
23
????????}
24
????????
if
(i
<
bookShelf.getMaxLength())?????????????
//
the?shelf?is?not?full??the?i?starts?from?1
25
????????
{
26
????????????System.out.println(
"
This?shelf?can?hold?
"
+
?bookShelf.getMaxLength()
+
"
?books?but?there?is?only?
"
+
newBooks.length
+
"
?new?incoming?books!
"
);
27
????????}
28
????????
if
(
!
flag)
29
????????
{
30
????????????System.out.println(
"
There?are?
"
+
newBooks.length?
+
"
?books?but?this?shelf?can?hold?
"
+
?bookShelf.getLength()
+
"
?books?now?it's?full!?OK!
"
);
31
????????}
32
????????
33
????????Iterator?it?
=
?bookShelf.iterator();
34
????????
while
(it.hasNext())
35
????????
{
36
????????????Book?book?
=
?(Book)it.next();
37
????????????System.out.println(
""
+
book.getName());
38
????????????
//
book?=?(Book)it.previous();??????????????????????
//
will?cause?dead?loop
39
????????????
//
System.out.println(""+book.getName());
40
????????}
41
????????
//
Book?book?=?(Book)it.previous();???????????????????
//
will?cause?error
42
????????
//
System.out.println(""+book.getName());
43
????}
44
}
45
All codes are here, I want to say the comments the first previous() will cause dead loop
The second previous will cause error, because the next() returns the current value and point to the next, then invoke the previous(), it returns the current (now the current points to the one after the last) so an error is caused.
Some words:
Don’t write program use only specific class, the abstract and the interface is good.
Good Luck :)