Skip to content

Commit 6e449e8

Browse files
committed
Add "Prototype"
1 parent e45348b commit 6e449e8

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

Prototype/src/Main.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import framework.*;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
Manager manager = new Manager();
6+
UnderlinePen ulpen = new UnderlinePen("~");
7+
manager.register("strong message", ulpen);
8+
9+
Product p1 = manager.create("strong message");
10+
p1.use("Hello, World!");
11+
}
12+
}

Prototype/src/MessageBox.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import framework.*;
2+
3+
public class MessageBox implements Product {
4+
private char decochar;
5+
public MessageBox(char decochar) {
6+
this.decochar = decochar;
7+
}
8+
public void use(String s) {
9+
int length = s.getBytes().length;
10+
for (int i = 0; i < length + 4; i++) {
11+
System.out.print(decochar);
12+
}
13+
System.out.println("");
14+
System.out.println(decochar + " " + s + " " + decochar);
15+
for (int i = 0; i < length + 4; i++) {
16+
System.out.print(decochar);
17+
}
18+
System.out.println("");
19+
}
20+
public Product createClone() {
21+
Product p = null;
22+
try {
23+
p = (Product)this.clone();
24+
} catch (CloneNotSupportedException e) {
25+
e.printStackTrace();
26+
}
27+
return p;
28+
}
29+
}

Prototype/src/UnderlinePen.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import framework.*;
2+
3+
public class UnderlinePen implements Product {
4+
private char ulchar;
5+
public UnderlinePen(char ulchar) {
6+
this.ulchar = ulchar;
7+
}
8+
public void use(String s) {
9+
int length = s.getBytes().length;
10+
System.out.println("\"" + s + "\"");
11+
System.out.print(" ");
12+
for (int i = 0; i < length; i++) {
13+
System.out.print(ulchar);
14+
}
15+
System.out.println("");
16+
}
17+
private Product createClone() {
18+
Product p = null;
19+
try {
20+
p = (Product)this.clone();
21+
} catch (CloneNotSupportedException e) {
22+
e.printStackTrace();
23+
}
24+
return p;
25+
}
26+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package framework;
2+
import java.util.Hashtable;
3+
4+
public class Manager {
5+
private Hashtable showcase = new Hashtable();
6+
public void register(String name, Product proto) {
7+
showcase.put(name, proto);
8+
}
9+
public Product create(String protoname) {
10+
Product p = (Product)showcase.get(protoname);
11+
return p.createClone();
12+
}
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package framework;
2+
3+
public interface Product extends Cloneable {
4+
public abstract void use(String s);
5+
public abstract Product createClone();
6+
}

0 commit comments

Comments
 (0)