Shape Factory

Description

Factory is a design pattern in common usage. Implement aShapeFactorythat can generate correct shape.

You can assume that we have only tree different shapes:Triangle,Square_and_Rectangle.

Example

Example 1:

Input:
ShapeFactory sf = new ShapeFactory();
Shape shape = sf.getShape("Square");
shape.draw();
Output:
  ----
 |    |
 |    |
  ----

Example 2:

Input:
ShapeFactory sf = new ShapeFactory();
shape = sf.getShape("Triangle");
shape.draw();
Output:
   /\
  /  \
 /____\

Example 3:

Solution

直接根据对应类型输出对应形状即可

Last updated

Was this helpful?