U
U
user_of_toster2021-03-24 13:01:09
OOP
user_of_toster, 2021-03-24 13:01:09

Does the "visitor" pattern violate the OCP principle?

There is an interface:

interface Visitor:
    visitA(a: A)
    visitB(b: B)
    visitC(c: C)


And some implementations:
class SlowVisitor: 
    visitA(a: A)
    visitB(b: B)
    visitC(c: C)
class FastVisitor: 
    visitA(a: A)
    visitB(b: B)
    visitC(c: C)


Suppose we add a new object D. We have to change the interface:
interface Visitor:
    visitA(a: A)
    visitB(b: B)
    visitC(c: C)
    visitD(d: D)

With a change in the interface, ALL visitors will have to change. Are we violating the OCP in this case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2021-03-24
@user_of_toster

Patterns are not dogma, like all principles. You weigh the pros and cons and choose a solution. It's like cutting off a leg in surgery is bad, but if it's gangrene, then it's necessary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question