AlgoMaster Logo
AlgoMasterDesign Shape Factoryeasy

Design Shape Factory

easy

A drawing tool can already measure and describe any Shape. What it should not do is scatter new Circle(...), new Rectangle(...), and new Triangle(...) throughout its workflow. The Factory Method pattern gives that construction decision a clear home.

The Shape products and the ShapeWorks context are already implemented in the starter code. Complete only the creator family:

  • ShapeCreator is the abstract creator. It must declare kind() and the factory method createShape().
  • build() creates a fresh shape through createShape() and increments this creator's count.
  • measure() builds a shape and returns its area.
  • describeShape() builds a shape and returns "<Name> with area: <area>", formatted to two decimal places.
  • builtCount() returns how many shapes that creator has built.
  • CircleCreator, RectangleCreator, and TriangleCreator supply the concrete factory methods and the lower-case names "circle", "rectangle", and "triangle".

Each concrete creator has one construction choice to make:

  • CircleCreator returns a circle with radius 5.
  • RectangleCreator returns a rectangle with width 4 and height 6.
  • TriangleCreator returns a triangle with base 3 and height 8.

The supplied ShapeWorks keeps one instance of each creator, starts with the circle creator active, and switches between them by name. Every call to area() or describe() asks the active creator to build a new shape, so repeated calls must keep increasing the total created count. An unknown creator name changes nothing and returns false.

The tests call only ShapeWorks; your creator classes make that pre-written workflow work without changing the products or the context.

Example 1:

Input:

Output:

Explanation: A new factory starts on the circle creator. The describe call builds one circle and formats its area, which is why the count moves to 1.

Example 2:

Input:

Output:

Explanation: Switching the creator changes what gets built without changing describe itself. Two calls means two shapes were made.

Constraints

  • kind is a lower-case word of at most 20 letters.
  • At most 100 calls in total are made across all methods.

How the design is graded

needs 7/10 to pass
  • Factory method hierarchy

    Full marks when ShapeCreator is abstract, declares createShape as the factory method, and CircleCreator, RectangleCreator, and TriangleCreator each construct only their matching product. Lose points heavily when concrete-product selection is moved into shared creator logic or the supplied ShapeWorks context.

  • Shared creation workflow

    Full marks when build is the single counted construction path and both measure and describeShape call it, producing a fresh shape for every request. Lose points when a product is cached, counts are maintained outside build, or subclasses repeat the shared measurement and formatting workflow.

  • Creator contract and exact behavior

    Full marks when creator kinds are circle, rectangle, and triangle; dimensions are 5, 4 by 6, and 3 by 8; builtCount is preserved across switches; and descriptions format the area to exactly two decimal places. Lose points for modifying the supplied products or ShapeWorks, or printing to stdout.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new ShapeWorks()null
currentCreator()"circle"
describe()"Circle with area: 78.54"
createdCount()1

A new factory starts on the circle creator. The `describe` call builds one circle and formats its area, which is why the count moves to 1.

Run checks these cases. Submit also runs a larger hidden set.