//IShape.cs 图形接口,实现设置中心,画图
using System;
namespace study.designPattern.simpleFactory
{
public interface IShape
{
void setCenter(int x, int y);
void draw();
}
}
//Circle.cs 圆形
using System;
namespace study.designPattern.simpleFactory
{
public class Circle : IShape
{
private int x;
private int y;
public void setCenter(int x, int y)
{
this.x = x;
this.y = y;
}
public void draw()
{
System.Web.HttpContext.Current.Response.Write("Circle center at (" + x + ", " + y + ")");
}
}
}
//ShapeFactory.cs 生产图形工厂
using System;
namespace study.designPattern.simpleFactory
{
public class ShapeFactory
{
public static IShape createShapeAt(string name, int x, int y)
{
try
{
                                    //这个地方不知道怎么写
return shape;
}
catch(Exception ex)
{
System.Web.HttpContext.Current.Response.Write(ex.Message);
return null;
}
}
}
}
//ShapeFactory.cs 生产图形工厂不知道怎么写,请指教

解决方案 »

  1.   

    if (name="circle")
    {
      return new Circle(x,y);
    }
    else if (name = "shape1")
    {
     reurn new Shape1(x,y);
    }
    类似这样的,当然返回的对象都要实现IShape接口
      

  2.   

    谢谢 Zoujinyucn(不会游泳的鱼)
    //java中是这样实现的
    IShape shape = (IShape) Class.forName(name).newInstance();
    shape.setCenter(x, y);
    return shape;
    //.net应该怎么实现呢?
      

  3.   

    ...
    //name应该定义为枚举
                                try
    {
                                    switch(name)
                                    {
                                         case "Circle":
                                              return new Circle();
                                         case "Rectangle":
                                              return new Rectange();
                                         //other sharp...
                                         default:
                                         //我觉得应该再定义一个基类的...
                                              return null;
                                    }    
    }
    catch(Exception ex)
    {
    System.Web.HttpContext.Current.Response.Write(ex.Message);
    return null;
    }
    ...
      

  4.   

    谢谢 Zoujinyucn(不会游泳的鱼)
    //java中是这样实现的
    IShape shape = (IShape) Class.forName(name).newInstance();
    shape.setCenter(x, y);
    return shape;.net
    里面也有反射,你可以看看PepShop3.0这个是我写的,
    /// <summary>
    /// 业务适配的工厂类,负责实例化业务适配,如果实例化失败,返回null
    /// </summary>
    public  class AdapterFactory
    {
    private static string RunLogPath=System.Configuration.ConfigurationSettings.AppSettings["RunLogPath"];
    public static IBusinessAdapter Create(string adapterName)
    {

    IBusinessAdapter adapter=null;
    if (adapterName==null||adapterName.Length==0)
    return adapter;
    try
    {
    string path = System.Configuration.ConfigurationSettings.AppSettings["BusinessAdapterPath"];
    string className = path+ "."+adapterName;
    path=path.Substring(path.LastIndexOf(".")+1);
    return (IBusinessAdapter) Assembly.Load(path).CreateInstance(className);

    }
    catch(Exception ex)
    {
    ExceptionManager.Publish(ex);
    VNetLog.WriteSysLog(RunLogPath,VNetLogSource.VNetPHSSelfSupport,VNetLogType.Exception
    ,"adapterName ="+adapterName
    ,"初始化业务适配发生异常"
    ,"",3);
    }
    return adapter;
    }
    }
      

  5.   

    ask: listhome(不想睡)(好好学习)
    string className = path+ "."+adapterName;
    classname是完整的namespace名字吗?