Factory in Java when concrete objects take different constructor parameters

后端 未结 4 617
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 20:12

I\'m trying to implement a Factory pattern in Java. I have a class called Shape which Circle and Triangle extends. The problem is that Shape constructor gets only 2 parameters w

4条回答
  •  执笔经年
    2021-01-30 21:04

    What you are trying to do is simply impossible. If the constructor arguments are different, then the client code will have to do different work for a Circle as for a Square and you can't solve this with uniform code. If there is other work the factory is doing besides handling the constructor arguments that you believe should happen in a factory, then you need to post this to your question and state the difficulty you are having in factoring out this common code-work.

提交回复
热议问题