Saturday, 15 March 2014

c# - Did I break Encapsulation? -



c# - Did I break Encapsulation? -

class programme { static void main(string[] args) { b b = new b(); b.run(); console.read(); } } class { public event action onchanged; public void raise() { if (onchanged != null) onchanged(); } } class b { public void run() { a = new a(); a.onchanged += a_onchanged; a.raise(); } private void a_onchanged() { console.writeline("wow! invoked"); } }

i not able figure out valid points can justify broke encapsulation or may otherwise. per understanding breaking encapsulation private method getting called class, plenty justifying broke on laws of oop. need gather more inner concepts , descrption code above.

this depends on why have raise method in class a.

if there solely enabling access private member, reply be: yes, encapsulation has been compromised. onchanged event should occur when has changed , not when external class decides should.

however, if simple snapshot making point, , raise event method triggering event side effect action taken (something changing text in textbox , triggering ontextchanged) encapsulation still in tact.

note:

i breaking encapsulation private method getting called class

from wikipedia:

encapsulation used hide values or state of structured info object within class, preventing unauthorized parties' direct access them. publicly accessible methods provided in class (so-called getters , setters) access values, , other client classes phone call these methods retrieve , modify values within object.

it ok private method called public one. how else called? you, programmer, methods logic straight , create sure phone call appropriate methods.

c# oop events delegates

No comments:

Post a Comment