Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Design Patterns Delegates Visitor Pattern. 2 Observer Pattern Observer Pattern is an object-oriented design that simulates void-pointers in for instance.

Similar presentations


Presentation on theme: "1 Design Patterns Delegates Visitor Pattern. 2 Observer Pattern Observer Pattern is an object-oriented design that simulates void-pointers in for instance."— Presentation transcript:

1 1 Design Patterns Delegates Visitor Pattern

2 2 Observer Pattern Observer Pattern is an object-oriented design that simulates void-pointers in for instance C/C++ and higher order functions in for instance Pascal. In C# the language construct delegate provides some of the same possibilities.

3 Delegates A delegate is a language construct that facilitates passing methods as parameters to other methods. This can be done both for instance and class methods. This can be done across objects and classes. Delegates are object oriented and type safe. Actual, a delegate is an object which contains a collection of methods that may be invoked. 3

4 Observer Delegate These features can be used to implement call-back: View: code samplecode sample 4

5 Exercise Add a Realist to the example. A realist will act as a pessimist, if the bottle amount is less than 50 cl. and like an optimist otherwise. 5

6 Visitor Delegate Used for traversing a collection and changing elements without knowing anything about the internal structure of the collections. A sort of “mutator iterator” May be simulated using the Visitor Pattern (GoF 1995) 6 Collection y x z ModifyAll( ) public int Double(int x){ return 2*x; } View: code samplecode sample

7 Opgave Add methods to the Visitor example, so it is possible to increase all numbers greater than 20 in the container by 7. 7

8 Delegates are objects them selves. They inherit from System.Delegate via System.MulticastDelegate 8

9 Events Delegates are often used for event based systems The keyword event in C# makes this a little easier The mechanism behind is the same. A event is a sort of a delegate. Events are often used in object oriented GUI programs Events can be used with advantage in other areas such as simulation and real-time systems. 9


Download ppt "1 Design Patterns Delegates Visitor Pattern. 2 Observer Pattern Observer Pattern is an object-oriented design that simulates void-pointers in for instance."

Similar presentations


Ads by Google