@@ -15,8 +15,9 @@ interface Shape {
1515
1616abstract class ShapeFactory {
1717 protected abstract Shape create ();
18- static Map factories = new HashMap ();
19- static Shape createShape (String id )
18+ static Map <String , ShapeFactory > factories =
19+ new HashMap <>();
20+ static Shape createShape (String id )
2021 throws BadShapeCreation {
2122 if (!factories .containsKey (id )) {
2223 try {
@@ -28,23 +29,22 @@ static Shape createShape(String id)
2829 if (!factories .containsKey (id ))
2930 throw new BadShapeCreation (id );
3031 }
31- return
32- ((ShapeFactory )factories .get (id )).create ();
32+ return factories .get (id ).create ();
3333 }
3434}
3535
3636class Circle implements Shape {
3737 private Circle () {}
38- public void draw () {
39- System .out .println ("Circle.draw" );
38+ public void draw () {
39+ System .out .println ("Circle.draw" );
4040 }
41- public void erase () {
41+ public void erase () {
4242 System .out .println ("Circle.erase" );
4343 }
4444 static class Factory extends ShapeFactory {
4545 @ Override
46- protected Shape create () {
47- return new Circle ();
46+ protected Shape create () {
47+ return new Circle ();
4848 }
4949 }
5050 static {
@@ -54,17 +54,17 @@ protected Shape create() {
5454}
5555
5656class Square implements Shape {
57- private Square () {}
58- public void draw () {
59- System .out .println ("Square.draw" );
57+ private Square () {}
58+ public void draw () {
59+ System .out .println ("Square.draw" );
6060 }
61- public void erase () {
62- System .out .println ("Square.erase" );
61+ public void erase () {
62+ System .out .println ("Square.erase" );
6363 }
6464 static class Factory extends ShapeFactory {
6565 @ Override
66- protected Shape create () {
67- return new Square ();
66+ protected Shape create () {
67+ return new Square ();
6868 }
6969 }
7070 static {
@@ -75,9 +75,9 @@ protected Shape create() {
7575
7676public class ShapeFactory2 {
7777 public static void main (String args []) {
78- String shlist [] = { "Circle" , "Square" ,
78+ String shlist [] = { "Circle" , "Square" ,
7979 "Square" , "Circle" , "Circle" , "Square" };
80- ArrayList shapes = new ArrayList ();
80+ ArrayList < Shape > shapes = new ArrayList <> ();
8181 try {
8282 for (String shlist1 : shlist ) {
8383 shapes .add (ShapeFactory .createShape (shlist1 ));
@@ -86,11 +86,11 @@ public static void main(String args[]) {
8686 e .printStackTrace ();
8787 return ;
8888 }
89- Iterator i = shapes .iterator ();
89+ Iterator < Shape > i = shapes .iterator ();
9090 while (i .hasNext ()) {
91- Shape s = ( Shape ) i .next ();
91+ Shape s = i .next ();
9292 s .draw ();
9393 s .erase ();
9494 }
95- }
95+ }
9696} ///:~
0 commit comments