Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            //方法一客户端实现
            string type;

            Console.WriteLine("请输入要求面积的图形形状:");
            type = Console.ReadLine();
            SimpleShapeFactory factory = new SimpleShapeFactory();
            ShapeArea          a       = new ShapeArea(factory);

            a.getShapeArea(type);
            //方法二客户端实现
            ShapeFactory TriShapeFactory = new TriShapeFactory();
            Shape        TriShapeArea    = TriShapeFactory.CreatShape();

            TriShapeArea.area();
            ShapeFactory SquShapeFactory = new SquShapeFactory();
            Shape        SquShapeArea    = SquShapeFactory.CreatShape();

            SquShapeArea.area();
            ShapeFactory RecShapeFactory = new RecShapeFactory();
            Shape        RecShapeArea    = RecShapeFactory.CreatShape();

            RecShapeArea.area();
            ShapeFactory CirShapeFactory = new CirShapeFactory();
            Shape        CirShapeArea    = CirShapeFactory.CreatShape();

            CirShapeArea.area();
            //总结:对于这个作业,其实简单工厂模式很高效,用工厂模式反而复杂化了,工厂模式更多的应用于更复杂的问题:
            //举个例子,pizza,分地区,每个地区又有不同口味,这样,抽象工厂类,地区类继承工厂,地区类里creat方法选择口味,这样地区类和口味类就成了一个简单工厂模式
            //每个地区类继承父类,这样就是工厂模式了
            //还可以利用反射机制
        }
Ejemplo n.º 2
0
 public ShapeArea(SimpleShapeFactory factory)
 {
     this.factory = factory;
 }