例36 形状工厂

1.问题描述

实现一个形状工厂ShapeFactory创建不同形状,假设只有三角形、正方形和矩形3种形状。

2.问题示例

输入:

ShapeFactory sf=new ShapeFactory();

Shape shape=sf.getShape("Square");

shape.draw();

输出:

输入:

ShapeFactory sf=new ShapeFactory();

shape=sf.getShape("Triangle");

shape.draw();

输出:

输入:

ShapeFactory sf=new ShapeFactory();

shape=sf.getShape("Rectangle");

shape.draw();

输出:

3.代码实现

4.运行结果

输入:type=Triangle

输出:

输入:type=Rectangle

输出:

输入:type=Square

输出: