Skip to content

Commit

Permalink
#984 #987 update readmes
Browse files Browse the repository at this point in the history
  • Loading branch information
iluwatar committed Nov 12, 2019
1 parent 33ea733 commit 3c57bf7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 33 deletions.
2 changes: 1 addition & 1 deletion chain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public class OrcKing {
Then it is used as follows

```java
OrcKing king = new OrcKing();
var king = new OrcKing();
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle")); // Orc commander handling request "defend castle"
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner")); // Orc officer handling request "torture prisoner"
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); // Orc soldier handling request "collect tax"
Expand Down
89 changes: 57 additions & 32 deletions composite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,53 +35,68 @@ Taking our sentence example from above. Here we have the base class and differen

```java
public abstract class LetterComposite {

private List<LetterComposite> children = new ArrayList<>();

public void add(LetterComposite letter) {
children.add(letter);
}

public int count() {
return children.size();
}
protected void printThisBefore() {}
protected void printThisAfter() {}

protected void printThisBefore() {
}

protected void printThisAfter() {
}

public void print() {
printThisBefore();
for (LetterComposite letter : children) {
letter.print();
}
children.forEach(LetterComposite::print);
printThisAfter();
}
}

public class Letter extends LetterComposite {
private char c;

private char character;

public Letter(char c) {
this.c = c;
this.character = c;
}

@Override
protected void printThisBefore() {
System.out.print(c);
System.out.print(character);
}
}

public class Word extends LetterComposite {

public Word(List<Letter> letters) {
for (Letter l : letters) {
this.add(l);
letters.forEach(this::add);
}

public Word(char... letters) {
for (char letter : letters) {
this.add(new Letter(letter));
}
}

@Override
protected void printThisBefore() {
System.out.print(" ");
}
}

public class Sentence extends LetterComposite {

public Sentence(List<Word> words) {
for (Word w : words) {
this.add(w);
}
words.forEach(this::add);
}

@Override
protected void printThisAfter() {
System.out.print(".");
Expand All @@ -93,39 +108,49 @@ Then we have a messenger to carry messages

```java
public class Messenger {

LetterComposite messageFromOrcs() {
List<Word> words = List.of(
new Word(List.of(new Letter('W'), new Letter('h'), new Letter('e'), new Letter('r'), new Letter('e'))),
new Word(List.of(new Letter('t'), new Letter('h'), new Letter('e'), new Letter('r'), new Letter('e'))),
new Word(List.of(new Letter('i'), new Letter('s'))),
new Word(List.of(new Letter('a'))),
new Word(List.of(new Letter('w'), new Letter('h'), new Letter('i'), new Letter('p'))),
new Word(List.of(new Letter('t'), new Letter('h'), new Letter('e'), new Letter('r'), new Letter('e'))),
new Word(List.of(new Letter('i'), new Letter('s'))),
new Word(List.of(new Letter('a'))),
new Word(List.of(new Letter('w'), new Letter('a'), new Letter('y'))));

var words = List.of(
new Word('W', 'h', 'e', 'r', 'e'),
new Word('t', 'h', 'e', 'r', 'e'),
new Word('i', 's'),
new Word('a'),
new Word('w', 'h', 'i', 'p'),
new Word('t', 'h', 'e', 'r', 'e'),
new Word('i', 's'),
new Word('a'),
new Word('w', 'a', 'y')
);

return new Sentence(words);

}

LetterComposite messageFromElves() {
List<Word> words = List.of(
new Word(List.of(new Letter('M'), new Letter('u'), new Letter('c'), new Letter('h'))),
new Word(List.of(new Letter('w'), new Letter('i'), new Letter('n'), new Letter('d'))),
new Word(List.of(new Letter('p'), new Letter('o'), new Letter('u'), new Letter('r'), new Letter('s'))),
new Word(List.of(new Letter('f'), new Letter('r'), new Letter('o'), new Letter('m'))),
new Word(List.of(new Letter('y'), new Letter('o'), new Letter('u'), new Letter('r'))),
new Word(List.of(new Letter('m'), new Letter('o'), new Letter('u'), new Letter('t'), new Letter('h'))));

var words = List.of(
new Word('M', 'u', 'c', 'h'),
new Word('w', 'i', 'n', 'd'),
new Word('p', 'o', 'u', 'r', 's'),
new Word('f', 'r', 'o', 'm'),
new Word('y', 'o', 'u', 'r'),
new Word('m', 'o', 'u', 't', 'h')
);

return new Sentence(words);

}

}
```

And then it can be used as

```java
LetterComposite orcMessage = new Messenger().messageFromOrcs();
var orcMessage = new Messenger().messageFromOrcs();
orcMessage.print(); // Where there is a whip there is a way.
LetterComposite elfMessage = new Messenger().messageFromElves();
var elfMessage = new Messenger().messageFromElves();
elfMessage.print(); // Much wind pours from your mouth.
```

Expand Down

0 comments on commit 3c57bf7

Please sign in to comment.