超越开放-封闭原则 [英] Getting past Open-Closed Principle

查看:82
本文介绍了超越开放-封闭原则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序,可以根据用户提供的鼠标数据绘制几何图形. 我有一类处理鼠标跟踪的类(它获取带有鼠标移动历史的列表),另一类是处理鼠标跟踪的类. 抽象类称为Shape.从这个类中,我需要一些额外的Shapes,例如Circle,Rectangle等-并且它们中的每一个都将覆盖抽象的Draw()函数.

I have a simple program which draws geometrical figures based on mouse data provided by user. I've got one class which handles the mouse tracking (it gets the List with mouse movement history) and one abstract class called Shape. From this class I derieve some extra Shapes, like Circle, Rectangle, etc. - and every one of them overrides the abstract Draw() function.

一切正常,但是问题出在我希望用户能够切换所需的形状时 手动.我得到了鼠标数据,我知道应该绘制什么形状.问题是如何使代码知道"它应该创建并传递哪个对象 将适当的参数传递给构造函数.在这一点上,还不可能添加新的Shape导数,这显然是错误的.

It all works nicely, but the problem comes when I want the user to be able to switch desired Shape manually. I get the mouse data and i know what shape should I draw. The problem is how to make the code to "know" which object should it create and pass appropiate parameters to the constructor. It is also impossible at this point to add new Shape derivatives, which is obiously wrong.

我非常不想发布像这样的代码:

I obiously don't want to come out with code like:

List<Shape> Shapes = new List<Shape>();
// somwhere later 

if(CurrentShape == "polyline"){
    Shapes.Add(new Polyline(Points)); 
}
else if (CurrentShape == "rectangle"){
    Shapes.Add(new Rectangle(BeginPoint, EndPoint));
}
// and so on.

上面的代码显然违反了开放式原则.问题是我对如何克服它没有任何好主意.主要问题是形状不同 构造函数具有不同的参数,这会带来更多麻烦.

The code above clearly vilates the Open-Closed Principle. The problem is that I don't have any good idea how to get over it. The main problem is that different Shapes have constructors with different parameters, which makes it much more troublesome.

我很确定这是一个普遍的问题,但是我不知道该如何解决.你有主意吗?

I am pretty sure that this is a common problem, but I don't know how to get past it. Do you have ay idea?

推荐答案

它乞求一个工厂,而不只是一个工厂,而是一个有可注射工人的工厂.

It begs for a factory but not just the factory but factory with injectable workers.

public class Context {
   public Point BeginPoint;
   public Point EndPoint;
   public List Points;

   whatever else
}

public class ShapeFactory {

   List<FactoryWorker> workers;

   public Shape CreateShape( string ShapeName, Context context )
   {
      foreach ( FactoryWorker worker in workers )
         if ( worker.Accepts( ShapeName ) )
             return worker.CreateShape( context );
   }

   public void AddWorker( FactoryWorker worker ) {
      workers.Add( worker );
   }
 }

 public abstract class FactortWorker {
    public abstract bool Accepts( string ShapeName );
    puboic Shape CreateShape( Context context );
 }

 public class PolyLineFactoryWorker : FactoryWorker {

    public override bool Accepts( string ShapeName ) {
       return ShapeName == "polyline";
    }

    public Shape CreateShape( Context context ) { ... }

 }

通过这种方式可以对扩展代码开放-新的工厂工人可以自由创建并添加到工厂.

This way the code is open for extensions - new factory workers can be created freely and added to the factory.

这篇关于超越开放-封闭原则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆