HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Implementing Design Patterns in Java"
Vol. 2, Issue 5, p. 12

	

Listing 1: Inheriting from the Employee abstract class.

class Hourly_Employee extends Employee {
    double compute_pay() {
      	// implementation code goes here
    }
}

class Salaried_Employee extends Employee {
    double compute_pay() {
   	// implementation code goes here
    }
}

Listing 2. Definition of the abstract Shape class.

abstract class Shape {
  abstract public void draw(Graphics g);
  abstract public void move(int offsetX, int offsetY);
  abstract public void erase(); // paint with the background color
  abstract public boolean pick(int offsetX, int offsetY);
}

Listing 3: Simple implementation of the Circle class.

class Circle extends Shape {
    public int X, Y, Radius;

    public Circle(int x, int y, int r) { // constructor
       X = x;
       Y = y;
       Radius = r;
  }

    public void draw(Graphics g) {
        g.drawOval(X-Radius; Y-Radius; 2*Radius; 2*Radius);
  }

    public void move(int oldx, int oldy) {
       X = X+oldx;
       Y = Y+oldy;
  }

    public void erase() {
       // repaint the region with the background color
  }

    public boolean pick(int x, int y) {
       // mouse pointer is inside or on the border
       return X<=x && x<=X+Radius && Y<=y && y<=Y+Radius;
  }
}

Listing 4: Simple implementation of Triangle and Rectangle Classes.s

class Triangle extends Shape {
    // provide implementation for the different Shape's methods here
}
   
class Rectangle extends Shape {
    // provide implementation for the different Shape's methods here
}

Listing 5: Simple implementation of a Factory Method.

class DrawShapeFactory {
    private Shape shape;

    public DrawShapeFactory(int x, int y, int r) {
       shape = new Circle(x, y, r);
  }
    
    public DrawShapeFactory(int x, int y, int w, int h) {
       shape = new Rectangle(x, y, w, h);
  }

    public DrawShapeFactory(int x, int y) {
       shape = new Triangle(x, y);
  }

    public void draw(Graphics g) {
       shape.draw(g);
  }

    public void move(int x, int y) {
       shape.move(x, y);
  }
    
    public void erase() {
       shape.erase();
  }

    public void pick(int x, int y) {
       return shape.pick(x, y);
  }
}

Listing 6: Checking Shape Number in order to generate different shapes.

// save all the shapes in a vector, so we can refer back to them later!
  Vector shapes = new Vector(100);

// handle mouse events
  public boolean mouseDown(Event e, int x, int y) {
      last_x = x;
      last_y = y;

      if (ShapeNum == 1) { // Circle
         DrawShapeFactory g;
         g = new DrawShapeFactory(last_x, last_y, 70);
          // draw the circle in different colors
          // ...
          // call other methods such as painting the screen...etc
          //
          shapes.addElement(new Circle(last_x, last_y, 70);
    }
      else if (ShapeNum == 2) { // Triangle
         // call DrawShapeFactory with different arguments.
        // ...
          shapes.addElement(new Triangle(last_x, last_y);
    }
      else { // Rectangle
         // call DrawShapeFactory with different arguments.
       // ...
         shapes.addElement(new Rectangle(last_x, last_y, 90, 120);
    }
  }


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.