ex04: anyone remember why OCP is violated in this exercise?

Moderatoren: pmueller, SE - Design and Construction

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

ex04: anyone remember why OCP is violated in this exercise?

Beitrag von AizazZaidee » 10. Jul 2018 19:30

Code: Alles auswählen

/*EXERCISE PRIVATE-PART*/@OCP(Compliancy.Violated)
public interface ExpressionVisitor<T> {

   T visit(Constant c);

   T visit(Var var);

   T visit(BinaryExpression b);

   T visit(UnaryExpression u);

}
Thanks.

VSchü
Windoof-User
Windoof-User
Beiträge: 26
Registriert: 5. Jul 2015 16:46

Re: ex04: anyone remember why OCP is violated in this exercise?

Beitrag von VSchü » 10. Jul 2018 20:00

To cite Design Patterns by Gamma et al., p. 336:
3. Adding new ConcreteElement classes is hard. The Visitor pattern makes it hard to add new subclasses of Element. Each new ConcreteElement gives rise to a new abstract operation on Visitor and a corresponding implementation in every ConcreteVisitor class. Sometimes a default implementation can be provided in Visitor that can be inherited by most of the ConcreteVisitors, but this is the exception rather than the rule.
So the key consideration in applying the Visitor pattern is whether you are mostly likely to change the algorithm applied over an object structure or the classes of objects that make up the structure. The Visitor class hierarchy can be difficult to maintain when new ConcreteElement classes are added frequently. In such cases, it's probably easier just to define operations on the classes that make up the structure. If the Element class hierarchy is stable, but you are continually adding operations or changing algorithms, then the Visitor pattern will help you manage the changes.
On the other hand, if I remember correctly, this class was considered OCP compliant at first, but the solution was changed after some discussion. (Refer to this announcement.) So it probably depends on the argumentation and as always on the dimenson of extension.

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Re: ex04: anyone remember why OCP is violated in this exercise?

Beitrag von AizazZaidee » 11. Jul 2018 11:06

thanks but then LSP will be violated "Sometimes a default implementation can be provided in Visitor that can be inherited by most of the ConcreteVisitors,"

Thanks for your reply.

VSchü
Windoof-User
Windoof-User
Beiträge: 26
Registriert: 5. Jul 2015 16:46

Re: ex04: anyone remember why OCP is violated in this exercise?

Beitrag von VSchü » 11. Jul 2018 12:37

I don't see how that would affect the LSP. But I guess if you abstain from default implementations, the OCP problem gets even worse as you have to change all the concrete visitors.

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Re: ex04: anyone remember why OCP is violated in this exercise?

Beitrag von AizazZaidee » 11. Jul 2018 13:31

Because you have to put instanceOf checks as some classes are overriding this method and some are not. (if you have to differentiate b/w which are overriding and which are not not.) it maybe a violation but not necessarily.

Antworten

Zurück zu „Software Engineering - Design and Construction“