0% found this document useful (0 votes)
7 views1 page

Prototype Code

prototype pattern code

Uploaded by

aroobhamid123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Prototype Code

prototype pattern code

Uploaded by

aroobhamid123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// Prototype interface

interface Shape {
Shape clone(); // Make a copy of itself
void draw(); // Draw the shape
}

// Concrete prototype
class Circle implements Shape {
private String color;

// When you create a circle, you give it a color.


public Circle(String color) {
[Link] = color;
}

// This creates a copy of the circle.


@Override
public Shape clone() {
return new Circle([Link]);
}

// This is how a circle draws itself.


@Override
public void draw() {
[Link]("Drawing a " + color + " circle.");
}
}

// Client code
class ShapeClient {
private Shape shapePrototype;

// When you create a client, you give it a prototype (a shape).


public ShapeClient(Shape shapePrototype) {
[Link] = shapePrototype;
}

// This method creates a new shape using the prototype.


public Shape createShape() {
return [Link]();
}
}

// Main class
public class PrototypeExample {
public static void main(String[] args) {
// Create a concrete prototype (a red circle).
Shape circlePrototype = new Circle("red");

// Create a client and give it the prototype.


ShapeClient client = new ShapeClient(circlePrototype);

// Use the prototype to create a new shape (a red circle).


Shape redCircle = [Link]();

// Draw the newly created red circle.


[Link]();
}
}

You might also like