SE450: Factory: Shapes [25/32] Previous pageContentsNext page

The simplest version exports the concrete class names.

file:Shape.java [source] [doc-public] [doc-private]
01
02
03
04
05
package factory.shape1;
import java.awt.Graphics;
public interface Shape {
  void paint(Graphics g);
}

file:Circle.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
package factory.shape1;
import java.awt.Graphics;
public class Circle implements Shape {
  public void paint(Graphics g) { /* ... */ }
  // ...
}

file:Square.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
package factory.shape1;
import java.awt.Graphics;
public class Square implements Shape {
  public void paint(Graphics g) { /* ... */ }
  // ...
}

file:Main.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
package factory.shape1.main;
import factory.shape1.Shape;
import factory.shape1.Square;
import factory.shape1.Circle;
public class Main {
  public static void main (String[] args) {
    Shape[] a = new Shape[2];
    a[0] = new Circle();
    a[1] = new Square();
  }
}

Previous pageContentsNext page