File tree Expand file tree Collapse file tree 5 files changed +86
-0
lines changed
Expand file tree Collapse file tree 5 files changed +86
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package framework ;
2+
3+ public interface Product extends Cloneable {
4+ public abstract void use (String s );
5+ public abstract Product createClone ();
6+ }
You can’t perform that action at this time.
0 commit comments