Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Design patterns express ideas in a general sense and not a specific class hierarchy that should be used to implement the pattern. In C#, you wouldn't implement the idea using classes and interfaces (as for example in Java), because it provides a more straightforward solution. You can use <strong>events</strong> and <strong>delegates</strong> instead. Here is a nice article that you may want to check out:</p> <ul> <li><a href="http://blogs.msdn.com/b/bashmohandes/archive/2007/03/10/observer-pattern-in-c-events-delegates.aspx" rel="nofollow noreferrer">Observer pattern in C# = Events + delegates</a></li> </ul> <p>Note that observer isn't the only pattern that can be encoded far more elegantly in C#. For example the Strategy pattern can be implemented using (single-line) lambda expression in C#:</p> <ul> <li><a href="http://www.unboxedsolutions.com/sean/archive/2005/10/23/779.aspx" rel="nofollow noreferrer">Strategy Pattern: Today, Tomorrow, and C# 3.0</a> </li> </ul> <p>That said, I'm quite sceptical about design patterns in many ways, but they may be useful as a reference. However they shouldn't be used blindly. Some authors maybe think that following the pattern strictly is the only way to write quality "enterprise" software, but that's not the case!</p> <p><strong>EDIT</strong> Here is a succinct version of your Ruby code. I didn't read the C# version, because it is too complex (and I'd even say obfuscated):</p> <pre><code>class Employee { public Employee(string name, string address, int salary) { Name = name; Address = address; this.salary = salary; } private int salary; public event Action&lt;Employee&gt; SalaryChanged; public string Name { get; set; } public string Address { get; set; } public int Salary { get { return salary; } set { salary = value; if (SalaryChanged != null) SalaryChanged(this); } } var fred = new Employee(...); fred.SalaryChanged += (changed_employee) =&gt; Console.WriteLine("Send {0} a new tax bill!", changed_employee.Name); </code></pre> <p>This is a perfectly fine use of events &amp; delegates. C# 3.0 lambda functions make your example even simpler than in Ruby :-).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload