Skip to content

Commit f1d4d34

Browse files
committed
more patterns
1 parent 26bcea1 commit f1d4d34

22 files changed

+626
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package comportamentali.observer;
2+
3+
public class Client {
4+
5+
public static void main(String[] args) {
6+
7+
RssFeed feed = new DesktopRssFeed();
8+
9+
RssProvider provider = new SportRssProvider();
10+
provider.attach(feed);
11+
12+
provider.sendNotification();
13+
}
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package comportamentali.observer;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
7+
public class DesktopRssFeed extends RssFeed {
8+
9+
final private int maxEntries = 10;
10+
List<Entry> entries = new LinkedList<>();
11+
12+
13+
@Override
14+
public void update() {
15+
for (Entry entry : rssProvider.getData()) {
16+
addOne(entry);
17+
}
18+
}
19+
20+
void addOne(Entry entry) {
21+
if (entries.size() >= maxEntries) {
22+
entries.removeFirst();
23+
}
24+
entries.add(entry);
25+
}
26+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package comportamentali.observer;
2+
3+
import java.util.ArrayList;
4+
import java.util.Date;
5+
import java.util.List;
6+
7+
public class EconomicsRssProvider extends RssProvider {
8+
9+
List<Article> articles = new ArrayList<>();
10+
11+
public class Article {
12+
Date date;
13+
String title;
14+
String text;
15+
16+
public Article(Date date, String title, String text) {
17+
this.date = date;
18+
this.title = title;
19+
this.text = text;
20+
}
21+
22+
public Date getDate() {
23+
return date;
24+
}
25+
26+
public String getText() {
27+
return text;
28+
}
29+
30+
public String getTitle() {
31+
return title;
32+
}
33+
}
34+
35+
public List<Entry> getData() {
36+
List<Entry> newEntries = new ArrayList<>();
37+
for (EconomicsRssProvider.Article article : articles) {
38+
Entry entry = new Entry(
39+
article.getTitle(),
40+
article.getText()
41+
);
42+
newEntries.add(entry);
43+
}
44+
return newEntries;
45+
}
46+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package comportamentali.observer;
2+
3+
public class Entry {
4+
private String title;
5+
private String text;
6+
7+
public Entry(String title, String text) {
8+
this.title = title;
9+
this.text = text;
10+
}
11+
12+
public String getTitle() {
13+
return title;
14+
}
15+
16+
public String getText() {
17+
return text;
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package comportamentali.observer;
2+
3+
public abstract class RssFeed {
4+
5+
protected RssProvider rssProvider;
6+
7+
public void setRssProvider(RssProvider provider) {
8+
this.rssProvider = provider;
9+
}
10+
11+
abstract void update();
12+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package comportamentali.observer;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public abstract class RssProvider {
7+
8+
private List<RssFeed> observers;
9+
10+
public RssProvider() {
11+
this.observers = new ArrayList<>();
12+
}
13+
14+
public void attach(RssFeed feed) {
15+
observers.add(feed);
16+
feed.setRssProvider(this);
17+
}
18+
19+
public void detach(RssFeed feed) {
20+
observers.remove(feed);
21+
}
22+
23+
public void sendNotification() {
24+
for (RssFeed feed : observers) {
25+
feed.update();
26+
}
27+
}
28+
29+
abstract List<Entry> getData();
30+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package comportamentali.observer;
2+
3+
import java.util.ArrayList;
4+
import java.util.Date;
5+
import java.util.List;
6+
7+
public class SportRssProvider extends RssProvider {
8+
9+
List<Game> games = new ArrayList<>();
10+
11+
public class Game {
12+
Date date;
13+
String club1;
14+
String club2;
15+
int score1;
16+
int score2;
17+
18+
public Game(Date date, String club1, String club2, int score1, int score2) {
19+
this.date = date;
20+
this.club1 = club1;
21+
this.club2 = club2;
22+
this.score1 = score1;
23+
this.score2 = score2;
24+
}
25+
26+
public Date getDate() {
27+
return date;
28+
}
29+
30+
public int getScore1() {
31+
return score1;
32+
}
33+
34+
public int getScore2() {
35+
return score2;
36+
}
37+
38+
public String getClub1() {
39+
return club1;
40+
}
41+
42+
public String getClub2() {
43+
return club2;
44+
}
45+
}
46+
47+
public List<Entry> getData() {
48+
List<Entry> newEntries = new ArrayList<>();
49+
for (SportRssProvider.Game game : games) {
50+
Entry entry = new Entry(
51+
String.format("%s vs %s", game.getClub1(), game.getClub2()),
52+
String.format("On %s the game ended %s %d - %s %d.", game.getDate(), game.getClub1(), game.getScore1(), game.getClub2(), game.getScore2())
53+
);
54+
newEntries.add(entry);
55+
}
56+
return newEntries;
57+
}
58+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package comportamentali.state;
2+
3+
import comportamentali.state.states.Forno;
4+
import comportamentali.state.states.FornoState;
5+
6+
public class Client {
7+
8+
public static void main(String[] args) {
9+
Forno forno = new Forno();
10+
11+
forno.apri();
12+
forno.chiudi();
13+
14+
forno.start();
15+
16+
// interrompi bruscamente
17+
forno.apri();
18+
forno.chiudi();
19+
20+
// riprendi
21+
forno.start();
22+
23+
// estendi
24+
forno.start();
25+
26+
// interrompi normalmente
27+
forno.stop();
28+
29+
// riparti
30+
forno.start();
31+
32+
forno.apri();
33+
}
34+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package comportamentali.state.states;
2+
3+
public class CotturaEstesa extends FornoState {
4+
5+
public CotturaEstesa(Forno forno) {
6+
super(forno);
7+
forno.addSecondsRemaining(60);
8+
}
9+
10+
@Override
11+
public void start() {
12+
forno.changeState(new CotturaEstesa(forno));
13+
}
14+
15+
@Override
16+
public void stop() {
17+
18+
}
19+
20+
@Override
21+
public void apri() {
22+
forno.changeState(new CotturaInterrotta(forno));
23+
}
24+
25+
@Override
26+
public void chiudi() {
27+
28+
}
29+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package comportamentali.state.states;
2+
3+
public class CotturaInterrotta extends FornoState {
4+
5+
6+
public CotturaInterrotta(Forno forno) {
7+
super(forno);
8+
forno.setTubo(false);
9+
forno.setSecondsRemaining(0);
10+
}
11+
12+
@Override
13+
public void start() {
14+
15+
}
16+
17+
@Override
18+
public void stop() {
19+
20+
}
21+
22+
@Override
23+
public void apri() {
24+
25+
}
26+
27+
@Override
28+
public void chiudi() {
29+
forno.changeState(new PortaChiusa(forno));
30+
}
31+
}

0 commit comments

Comments
 (0)