Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Main.Java
- public class Main {
- public static void main(String[] args) {
- ShapeFactory shapeFactory = new ShapeFactory();
- Shape shape1 = shapeFactory.getShape("TRIANGLE");
- shape1.draw();
- Shape shape2 = shapeFactory.getShape("RECTANGLE");
- shape2.draw();
- Shape shape3 = shapeFactory.getShape("SQUARE");
- shape3.draw();
- }
- }
- ===============================================================================================================
- ShapeFactory.Java
- public class ShapeFactory {
- public Shape getShape(String shapeType){
- if(shapeType == null){
- return null;
- }
- else if(shapeType.equalsIgnoreCase("RECTANGLE")){
- return new Rectangle();
- }
- else if(shapeType.equalsIgnoreCase("SQUARE")){
- return new Square();
- }
- else if(shapeType.equalsIgnoreCase("TRIANGLE")){
- return new Triangle();
- }
- return null;
- }
- }
- ===============================================================================================================
- Triangle.Java
- public class Triangle implements Shape{
- @Override
- public void draw() {
- System.out.println("This is draw in Triangle");
- }
- }
- ===============================================================================================================
- Square.Java
- public class Square implements Shape{
- @Override
- public void draw() {
- System.out.println("This is draw in Sqaure");
- }
- }
- ===============================================================================================================
- Rectangle.Java
- public class Rectangle implements Shape{
- @Override
- public void draw() {
- System.out.println("This is draw from Rectangle");
- }
- }
- ===============================================================================================================
- Shape.Java
- public interface Shape {
- public void draw();
- }
Add Comment
Please, Sign In to add comment