这是我的代码
public class FactoryPatternDemo {
public static void main(String[]args)
{
AbstractFactory shapeFactory=new ShapeFactory();
//tramite la fabbrica di figura geometrica disegno un rettangolo..
Shape shape1=shapeFactory.getShape("rEcTaNgLe");
shape1.draw();
System.out.println();
//..e un triangolo
Shape shape2=shapeFactory.getShape("triangle");
shape2.draw();
}
形状工厂:
public class ShapeFactory extends AbstractFactory{
public ShapeFactory(){
}
@Override
public Shape getShape(String shapeType)
{
if (shapeType==null)
return null;
if (shapeType.equalsIgnoreCase("RECTANGLE"))
return new Rectangle();
if (shapeType.equalsIgnoreCase("TRIANGLE"))
return new Triangle();
return null;
}
抽象工厂:
public abstract class AbstractFactory {
public abstract Shape getShape(String shapeType);}
抽象产品
public interface Shape {
void draw();}
混凝土产品#1
public class Rectangle implements Shape {
@Override
public void draw() {
for(int i=0; i<5; i++)
{
if(i==0 || i==4)
{
for(int j=0; j<10; j++)
{
System.out.print("*");
}
}
else
{
for(int j=0; j<10; j++)
{
if(j==0||j==9)
System.out.print("*");
else
System.out.print(" ");
}
}
System.out.print("\n");
}
}
我的问题是:这是实现抽象工厂模式的正确方法吗?客户端应该只能看到 FactoryPatternDemo 类中的抽象事物或接口,但是这行代码:
AbstractFactory shapeFactory=new ShapeFactory();
显示混凝土工厂的名称。这是一个错误吗?多谢你们