mardi 17 mars 2020

How to identify the most suitable Design Pattern for a scenario

I want to implement a GUI based drawing program. It should be able to draw circles,triangles and squares. And it should be able to expand the number of shapes in the system in future.

The program will give certain ‘abilities’ to these shapes, such as the ability to ‘move’, ‘expand’ , ‘rotate’ and of course ‘draw’ (to get the shape drawn in the GUI). Each of these methods needs a ‘Graphics Context’ passed in as as a parameter, in order to get them drawn.

I want use a design pattern for this program and since there will be methods differ from shape to shape I think strategy pattern will be useful.

I want to know the way which I have used this pattern is okay.

interface Shape{}

interface behavioral{
public void move(Graphics g){}
public void expand(Graphics g){}
public void draw(Graphics g){}
}

(There is an aggregation between shape and behavioral)

interface Rotatable{
public void rotate(Graphics g)
} 

interface RotatableToLeft extends Rotatable(){
public void rotate(Graphics g)
}

interface RotatableToRight extends Rotatable(){
public void rotate(Graphics g)
}

class circle implements Shape{
public void move(Shape g){--}
public void expand(Shape g){--}
public void draw(Shape g){--}

}

class Square implements Shape implements Rotatable{
public void move(Shape g){--}
public void expand(Shape g){--}
public void draw(Shape g){--}
public void rotatefree(RotatableToLeft g){--}
}

class Triangle implements Shape implements Rotatable{
public void move(Graphics g){--}
public void expand(Graphics g){--}
public void draw(Graphics g){--}
public void rotateLeft(RotatableToLeft g){--}
public void rotateRight(RotatableToRight g){--}

}

Aucun commentaire:

Enregistrer un commentaire