Software engineering and GUI design Behavioral design patterns
Software engineering and. GUI design Behavioral design patterns Iterator, Chain of Responsibility Visitor Observer, Command, Mediator http: //users. nik. uni-obuda. hu/prog 4/ 1
Purpose Design pattern Short description Behavioral Iterator foreach () { } Chain of resp. Execute a process using multiple process executors Visitor Separate caller and called instance – multiple visitors/visitees : IVisitor. Visit(IVisitor. Acceptor) IVisitor. Accept(IVisitor) Command Encapsulate a request/method call into an instance Observer Separate caller and called instance (using events ) Mediator Fully separate caller and called instance (using central hub) State instances that control state transitions Interpreter Convert arbitrary input to arbitrary output Memento Restore previous state Strategy Full implementation/process in the descendants Depend on abstract base class in constructor Dependency injection (via Abstract) Template method Define individual steps in the descendants Usage of virtual methods and method polymorphism 2
Behavioral Design Patterns Purpose Scope Creational Structural Behavioral Class Factory method Adapter Interpreter Template method Object Abstract factory Builder Prototype Singleton Adapter Bridge Composite Decorator Facade Flyweight Proxy Chain of responsibility Command Iterator Mediator Memento Observer State Strategy Visitor 3
Iterator 4
Iterator • Main aims – The internal structure should not be visible from the outside – The internal additional types/methods should not be visible from the outside – A collection of any internal structure/rules should be processed the same way • Typical example of a design pattern that is already s olved in almost every programming language using a language construct – C#: IEnumerable + IEnumerable<> + yield return – Java: Iterable – C++: range-based-for + iterator – PHP: Traversable / Iterable 5
Iterator – C# 6
Linq Iterator • System. Linq. Enumerable+Where. Select. Enumerable. Iterator`2 [System. Xml. Linq. XElement, System. String] 7
Iterator – C# – yield return 8
Iterator – C# – Dot. Peek 9
10
Chain of Responsibility 11
Chain of Responsibility • Similar principle than with the Iterator, BUT – Instead of data, the emphasis is on a method call – The full processing and the chain is hidden – The number of processing/decision making units and the processing/decision path should not be visible from outside – From the outside: I ask the outmost/topmost item, to make a decision/do something I receive one answer • Typical use: change big switch statements into a foreach-like structure without exactly knowing the concrete branches – Transformation switch statements (universal converters) – Creational switch statements (factory!!!) – Decision making switch statements (universal validator) • Basic variations – Unconditional pass-through: logger chain, processing, Git. Stats 2 – Conditional pass-through: factory chain 12
Chain of Responsibility – C# 13
Chain of Responsibility, types – C# 14
Chain of Responsibility, descendant/member 15
Chain of Responsibility, NEVER LIKE THIS • Suppose: new descendant, that can answer YES/NO/DONTKNOW and also a MAYBE answer is possible … – We have to edit the enum CODE SMELL – We have to edit the base class SOLID „O” violation 16
Visitor • Pattern that implements called/caller separation, also capable of working with collections 17
Visitor • The visitor performs an operation on the acceptor, but it is the acceptor that decides whether it allows the operation or not • When we make the call, we don’t necessarily exactly know the operation performed • The Acceptor doesn’t know about the concrete visitor(s), but the Visitor knows (may know) about the acceptor(s) 18
Visitor – C# 19
Visitor – C# – Concrete. Acceptors 20
Visitor – C# – Concrete. Visitors 21
Visitor – C# – Client 22
Behavioral Design Patterns Purpose Scope Creational Structural Behavioral Class Factory method Adapter Interpreter Template method Object Abstract factory Builder Prototype Singleton Adapter Bridge Composite Decorator Facade Flyweight Proxy Chain of responsibility Command Iterator Mediator Memento Observer State Strategy Visitor 23
Observer • Aim: event handling • The proposed implementation : „obsolete”? 24
Observer 25
Observer – C# 26
Observer – C# 27
Observer – C#, as we usually do… • The delegate is much better at solving the same problem – No need for a reference for the whole subscriber, only for a method of ANY KIND a lot safer and more flexible – One type and its operators for subscribe, unsubscribe, event call a lot more simple • C# specific extras – Unsubscribe: OBLIGATORY, if the subscriber’s lifetime is shorter (normal IDisposable, otherwise: MEMORY LEAK!!!) – Weak Delegates (using the decorator design pattern): https: //www. codeproject. com/Articles/16182/Observable-property-pattern-memory-leaks-and-weak 28
2 9 . NET against memory leak: Weak Delegates
IObserver – NOT the pattern, much more! • The interfaces are similar, but they are part of a bigger framework • IEnumerable + extensions = LINQ ; IObservable + extensions = RX 30
Reactive Extensions • „Using Rx, developers represent asynchronous data streams with Observables , query asynchronous data streams using LINQ operators , and parameterize the concurrency in the asynchronous data streams using Schedulers. Simply put, Rx = Observables + LINQ + Schedulers. ” • MAY/2018: „everyone agreed upon almost instantaneously was that reactive programming on dotnet needed to have a stronger branding/marketing […] System. Reactive is part of. NET” https: //reactiveui. net/blog/2018/05/system-reactive-has-a-new-home-on-github • https: //github. com/petroemil/Rx. Book (Emil Petró) • Some. Observable. Event. Source. Throttle(Time. Span. From. Milliseconds(100 )). Distinct. Until. Changed (). Timeout(Time. Span. From. Milliseconds(250 )). Retry(3); 31
Command • Tries to solve the problem that with the Observer pattern the whole subscriber is required • In the Command pattern the method call is „packed” into a command instance, so the caller only sees : Command. Instance. Execute(); // Nothing from the called instance! • During the command call, additional operations can be performed 32
Command – C# • abstract class Command <TReceiver , TParameter , TResult > • WPF ICommand: void Execute( object parameter) 33
Command – C# 34
Command – C# • The creation of the receiver can be made by the command itself • Pro: hidden receiver ; Con: can’t share receivers between 35
Command – WPF 36
Mediator 37
Mediator 38
Mediator 39
Mediator 40
Mediator – C# • According to the pattern, the communicating parties should not have a reference to each other – practically sometimes that’s better 41
Mediator – Redux 42
Mediator – MVVM Light, PRISM 43
Mediator – ROS 44
4 5 Message Bus / Broker
Thank you for your attention! Behavioral design patterns Iterator, Chain of Responsibility Visitor Observer, Command, Mediator http: //users. nik. uni-obuda. hu/prog 4/ 46
- Slides: 46