Skip to content

Commit 4024d01

Browse files
author
Viktor Reichert
committed
Template Method and Others
1 parent b3ea7bd commit 4024d01

File tree

8 files changed

+162
-1
lines changed

8 files changed

+162
-1
lines changed

src/ballpit/gui/BallPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void paint(Graphics g) {
8383
public void mousePressed(MouseEvent e) {
8484
Command command = null;
8585
switch (e.getButton()) {
86-
case MouseEvent.BUTTON1 : command = new AddBall(new Ball(), ballPit); break;
86+
case MouseEvent.BUTTON1 : command = new AddBall(new Ball().setX(e.getX()).setY(e.getY()), ballPit); break;
8787
case MouseEvent.BUTTON3 : command = new RemoveBall(ballPit); break;
8888
}
8989
if (command != null){

src/ballpit/internals/Ball.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,37 @@ public Ball() {
2020
this.speedX = ThreadLocalRandom.current().nextInt(-10, 11);
2121
this.speedY = ThreadLocalRandom.current().nextInt(-10, 11);
2222
}
23+
24+
public Ball setX(int x) {
25+
this.x = x;
26+
return this;
27+
}
28+
29+
public Ball setY(int y) {
30+
this.y = y;
31+
return this;
32+
}
33+
34+
public Ball setBallWidth(int ballWidth) {
35+
this.ballWidth = ballWidth;
36+
return this;
37+
}
38+
39+
public Ball setBallHeight(int ballHeight) {
40+
this.ballHeight = ballHeight;
41+
return this;
42+
}
43+
44+
public Ball setSpeedX(int speedX) {
45+
this.speedX = speedX;
46+
return this;
47+
}
48+
49+
public Ball setSpeedY(int speedY) {
50+
this.speedY = speedY;
51+
return this;
52+
}
53+
2354
public int getX() {
2455
return x;
2556
}

src/flyweight/verlag/Verlag.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package flyweight.verlag;
22

33
import java.util.Date;
4+
import java.util.HashMap;
5+
import java.util.Map;
46

57
public class Verlag {
68
private final String verlag;
79
private final Date verlagBeginningYear;
810
private final String verlagLocation;
11+
private static Map<String, Verlag> cache = new HashMap<>();
912

1013
protected Verlag(String verlag, Date verlagBeginningYear, String verlagLocation) {
1114
this.verlag = verlag;
@@ -24,4 +27,11 @@ public Date getVerlagBeginningYear() {
2427
public String getVerlagLocation() {
2528
return verlagLocation;
2629
}
30+
31+
public static Verlag getVerlag(String verlag, Date verlagBeginningYear, String verlagLocation) {
32+
if (!cache.containsKey(verlag)) {
33+
cache.put(verlag, new Verlag(verlag, verlagBeginningYear, verlagLocation));
34+
}
35+
return cache.get(verlag);
36+
}
2737
}

src/template_method/Demo.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package template_method;
2+
3+
import template_method.letter.Customer;
4+
import template_method.letter.PayUsBackLetter;
5+
6+
public class Demo {
7+
public static void main(String[] args) {
8+
Customer customer = new Customer(-300, "Viktor Reichert", "zu Hause");
9+
10+
System.out.println(new PayUsBackLetter(customer).create());
11+
}
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package template_method.letter;
2+
3+
public class Customer {
4+
private int money;
5+
private String name;
6+
private String address;
7+
8+
public Customer(int money, String name, String address) {
9+
this.money = money;
10+
this.name = name;
11+
this.address = address;
12+
}
13+
14+
public int getMoney() {
15+
return money;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public String getAddress() {
23+
return address;
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package template_method.letter;
2+
3+
public class Letter {
4+
5+
private final String text;
6+
private final String header;
7+
private final String address;
8+
9+
public Letter(String setAdress, String setHeader, String setText) {
10+
this.address = setAdress;
11+
this.header = setHeader;
12+
this.text = setText;
13+
}
14+
15+
public String beautify() {
16+
return address + "\n\n" + header + "\n\n" + text;
17+
}
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package template_method.letter;
2+
3+
public abstract class LetterCreator<M> {
4+
public M create() {
5+
if (checkLetterNecessity()) {
6+
// Erstelle Inhalt des Briefes
7+
Letter letter = new Letter(setAdress(), setHeader(), setText());
8+
//Speichere Brief
9+
return saveToMedium(letter);
10+
}
11+
return noLetterCreated();
12+
}
13+
14+
protected boolean checkLetterNecessity() {
15+
return true;
16+
}
17+
18+
protected abstract String setAdress();
19+
20+
protected abstract String setHeader();
21+
22+
protected abstract String setText();
23+
24+
protected abstract M saveToMedium(Letter letter);
25+
26+
protected M noLetterCreated() {
27+
return null;
28+
}
29+
30+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package template_method.letter;
2+
3+
public class PayUsBackLetter extends LetterCreator<String>{
4+
Customer customer;
5+
6+
public PayUsBackLetter(Customer customer) {
7+
this.customer = customer;
8+
}
9+
10+
@Override
11+
protected boolean checkLetterNecessity() {
12+
return customer.getMoney() < 0;
13+
}
14+
15+
@Override
16+
protected String setAdress() {
17+
return customer.getName() + " " + customer.getAddress();
18+
}
19+
20+
@Override
21+
protected String setHeader() {
22+
return "We wan't our money, NOW!";
23+
}
24+
25+
@Override
26+
protected String setText() {
27+
return "This is your first an final warning " + customer.getName()
28+
+". You owe us " +(-customer.getMoney())+". We know where your house lives." ;
29+
}
30+
31+
@Override
32+
protected String saveToMedium(Letter letter) {
33+
return letter.beautify();
34+
}
35+
}

0 commit comments

Comments
 (0)