Guide to Behavioral Design Patterns in Software Engineering and GUI Design

undefined
 
Software engineering and 
GUI 
design
 
Behavioral 
design patterns
Interpreter, Memento, State
Stragety, Template method
 
1
 
http://users.nik.uni-obuda.hu/prog4/
 
2
 
Behavioral Design Patterns
 
3
 
Interpreter
 
4
 
expression ::= literal | alternation | sequence | repetition | '(' expression ')'
alternation ::= expression '|' expression
sequence ::= expression '&' expression
repetition ::= expression '*'
literal ::= 'a' | 'b' | 'c' | ... { 'a' | 'b' | 'c' | ... }*
 
Interpreter
 
5
 
raining & (dogs | cats) *
 
Interpreter
 
6
 
Interpreter
 
In principle
: 
using formal language/grammar rules of expressions,
convert arbitrary input to arbitrary output
Too generic, rarely useable, complicated pattern
 
Only useable with simple languages (with a complex grammar,
the class hierarchy can get extremely complex)
The class hierarchy is not optimal regarding memory and cpu
issues, either
For example, regular expressions are processed using much
more efficient and faster “state machine” methods instead of
slow and painful syntax trees
 
7
 
Interpreter – C#
 
8
CALL OR GET INFO
 
Interpreter – C#
 
9
Interpreter – C#
10
 
Interpreter – C#
 
11
 
Interpreter – C#
 
12
 
This is a more common 
use case: 
an 
Expression 
will not produce a
final output on its own, but rather multiple Expressions are called
after each other, and they gradually modify the output property
of the context
 
Interpreter – C#
 
13
Interpreter – C#
14
Interpreter – C#
15
Interpreter – C#
16
Memento
17
 
Save and restore previous state
Originator = 
original object
Memento = 
store a single state
 (OOP: private class?)
Caretaker = 
handle multiple states
 (OOP: private class?)
The Originator is responsible for saving its own state into a
Memento instance, and restore its state from a Memento
instance
Memento – C#
18
 
The 
Save()’s
 parameter
: 
the important data (the full instance is
rare, as most of the times, private datafields are also saved)
 
Memento – C#
 
19
 
Memento – C#
 
20
 
Memento – C#
 
21
 
State
 
22
 
„Memento” state = 
full object state, the important data
fields/properties
„State” state = 
one single data field, that stores the current status
of the instance
 
State
 
State chains can get complex, especially with many states / state
transitions
Without the pattern
: „spagetti-
code
”, 
same or similar chain of
complex checks in multiple business logic methods
 
23
State – 
Implementation alternatives
 
Common implementation rules
Context class, with a single State property
Every different state requires a different suitable class
The state-classes have state-changing methods
In the state-changing methods, we perform business logic
checks / conditions and then we change the state:
24
 
Variants
State-changing methods 
= 
business logic methods
E.g
. 
bug tickets
, 
Proposed, Open, Resolved, Closed
MoveForward(), MoveBackward(), Reject(), Reopen()
State-changing method
 = 
a single
 
state supervisor method
E.g
. 
bank accounts
, Bronze, Silver, Gold
Deposit(), Withdraw(), PayInterest()
State – C#, Tickets
25
State – C#, Tickets
26
State – variant 1
 
Typical modell
: State Transition Table
F(
currentState
, 
operation
) 
 
Conditions
 + 
New state
Pro
VERY
 
easy, clear structure
Easy to extend
Every code has its clear place
Con
Many states
, 
many BL 
 
MANY
2
 
methods
, 
mostly empty
(STT: 1 
cell 
= 1 
method
; UML State Diagram: 1 
possible
 
arrow
 =
1 
method
)
„S”OLID violation? 
Does not define logic, but knows about…
Can be good if you have few states/operations but lots of state
changes (implements them into a good structure
)
Disturbing if many states/operations 
(
LOTS OF empty methods
)
27
 
State – C#, bank account
 
28
 
State – C#, bank account
 
29
State – C#, bank account
30
State – variant 2
 
Typical model
: UML state diagram
Prettier
solution
Better suited for the 
SOLID 
principles
The state classes have ZERO business logic code
No unnecessary empty methods
However
: 
we extract the state changed from  the Context class,
BUT the Context must know when a state change might occur
this is not obviously nice
The 
EnsureState() 
method can get too complex
Every state in the UML diagram means one EnsureState method
With complex state systems, this method can get too big
Solution
: Chain of Responsibility, 
but this increases the number
of classes
31
 
Behavioral Design Patterns
 
32
Strategy, Template method
 
Basic patterns describing the main 
OOP 
principles
Describes the meaningful use of virtual methods 
(S”OL”ID)
Dependency injection (
with abstract classes
: SOLI”D”)
Related 
(structural) 
patterns
: Adapter, Bridge
Used every day, even with the most simple programs
Strategy: 
a full operation is dependent on the descendant class
(DI with 
base
 class)
Template: 
the operation is defined by the base class, some steps
can be defined in the descendants (virtual methods, DI with
base class)
Adapter: 
How can we substitute a class/interface with a
different one 
(DI with interface)
Bridge: 
Decomposition of hierarchies
 (DI with base class)
Dependency Injection???
GoF(1994); Martin Fowler (2004)
33
 
Strategy
 
34
Strategy – C#
35
 
Template method
 
36
Template method – C#
37
Template method – C#
38
Strategy, Template method
 
Practically the most widely used design pattern
s
 in any system (of
any size!)
Strategy: „How to implement and use a library hierarchy?”
The base class 
(interface?) 
is fixed
We write code that uses the base class
Any descendant instance can be used later
Our code calls the hierarchy’s code
Template: „How to implement and use a framework?”
The base class defines the possibilities
The base class is used by a full business logic/process
We write code that utilizes the rules/possibilities of the base
class (usually only a fraction of those)
The externally defined
 business
 process will call our code
39
Strategy = ASP.NET Core Identity
A completely generic approach, does not care which descendant
(= login 
implementation
) 
is used
40
PROG3, PROG4: 
The Logic does not care which data storage
implementation is used
Template =
MVC
Framework
41
PROG4
During the render process, the 
WPF 
will SOMEWHERE call the
OnRender()
 method of the 
FrameworkElement 
descendants
WPF 
will SOMEHOW call the event handler/Command member
ASP.NET will SOMEHOW call the Action handler method
undefined
 
Thank you for your attention
 
Behavioral 
design patterns
Interpreter, Memento, State
Stragety, Template method
 
42
 
http://users.nik.uni-obuda.hu/prog4/
Slide Note
Embed
Share

Learn about key behavioral design patterns such as Interpreter, Memento, State, Strategy, and Template Method in software engineering and GUI design. Understand their purposes, implementation techniques, and when to use them effectively. Explore how these patterns enhance the design and functionality of software systems.

  • Software Engineering
  • GUI Design
  • Behavioral Patterns
  • Design Patterns

Uploaded on Sep 21, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Software engineering and GUI design Behavioral design patterns Interpreter, Memento, State Stragety, Template method http://users.nik.uni-obuda.hu/prog4/ 1

  2. Purpose Design pattern Short description Iterator Behavioral foreach () { } Chain of resp. Execute a process using multiple process executors Visitor Separate caller and called instance multiple visitors/visitees: IVisitor.Visit(IVisitorAcceptor) IVisitorAcceptor.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 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

  3. Behavioral Design Patterns Purpose Creational Structural Behavioral Factory method Adapter Interpreter Template method Scope Class Abstract factory Builder Prototype Singleton Adapter Bridge Composite Decorator Facade Flyweight Proxy Chain of responsibility Command Iterator Mediator Memento Observer State Strategy Visitor Object 3

  4. Interpreter expression ::= literal | alternation | sequence | repetition | '(' expression ')' alternation ::= expression '|' expression sequence ::= expression '&' expression repetition ::= expression '*' literal ::= 'a' | 'b' | 'c' | ... { 'a' | 'b' | 'c' | ... }* 4

  5. Interpreter raining & (dogs | cats) * 5

  6. Interpreter 6

  7. Interpreter In principle: using formal language/grammar rules of expressions, convert arbitrary input to arbitrary output Too generic, rarely useable, complicated pattern Only useable with simple languages (with a complex grammar, the class hierarchy can get extremely complex) The class hierarchy is not optimal regarding memory and cpu issues, either For example, regular expressions are processed using much more efficient and faster state machine methods instead of slow and painful syntax trees 7

  8. Interpreter C# 8

  9. Interpreter C# 9

  10. Interpreter C# 10

  11. Interpreter C# 11

  12. Interpreter C# This is a more common use case: an Expression will not produce a final output on its own, but rather multiple Expressions are called after each other, and they gradually modify the output property of the context 12

  13. Interpreter C# 13

  14. Interpreter C# 14

  15. Interpreter C# 15

  16. Interpreter C# 16

  17. Memento Save and restore previous state Originator = original object Memento = store a single state (OOP: private class?) Caretaker = handle multiple states (OOP: private class?) The Originator is responsible for saving its own state into a Memento instance, and restore its state from a Memento instance 17

  18. Memento C# The Save() s parameter: the important data (the full instance is rare, as most of the times, private datafields are also saved) 18

  19. Memento C# 19

  20. Memento C# 20

  21. Memento C# 21

  22. State Memento state = full object state, the important data fields/properties State state = one single data field, that stores the current status of the instance 22

  23. State State chains can get complex, especially with many states / state transitions Without the pattern: spagetti-code , same or similar chain of complex checks in multiple business logic methods 23

  24. State Implementation alternatives Common implementation rules Context class, with a single State property Every different state requires a different suitable class The state-classes have state-changing methods In the state-changing methods, we perform business logic checks / conditions and then we change the state: Variants State-changing methods = business logic methods E.g. bug tickets, Proposed, Open, Resolved, Closed MoveForward(), MoveBackward(), Reject(), Reopen() State-changing method = a single state supervisor method E.g. bank accounts, Bronze, Silver, Gold Deposit(), Withdraw(), PayInterest() 24

  25. State C#, Tickets 25

  26. State C#, Tickets 26

  27. State variant 1 Typical modell: State Transition Table F(currentState, operation) Conditions + New state Pro VERY easy, clear structure Easy to extend Every code has its clear place Con Many states, many BL MANY2 methods, mostly empty (STT: 1 cell = 1 method; UML State Diagram: 1 possible arrow = 1 method) S OLID violation? Does not define logic, but knows about Can be good if you have few states/operations but lots of state changes (implements them into a good structure) Disturbing if many states/operations (LOTS OF empty methods) 27

  28. State C#, bank account 28

  29. State C#, bank account 29

  30. State C#, bank account 30

  31. State variant 2 Typical model: UML state diagram Prettier solution Better suited for the SOLID principles The state classes have ZERO business logic code No unnecessary empty methods However: we extract the state changed from the Context class, BUT the Context must know when a state change might occur this is not obviously nice The EnsureState() method can get too complex Every state in the UML diagram means one EnsureState method With complex state systems, this method can get too big Solution: Chain of Responsibility, but this increases the number of classes 31

  32. Behavioral Design Patterns Purpose Creational Structural Behavioral Factory method Adapter Interpreter Template method Scope Class Abstract factory Builder Prototype Singleton Adapter Bridge Composite Decorator Facade Flyweight Proxy Chain of responsibility Command Iterator Mediator Memento Observer State Strategy Visitor Object 32

  33. Strategy, Template method Basic patterns describing the main OOP principles Describes the meaningful use of virtual methods (S OL ID) Dependency injection (with abstract classes: SOLI D ) Related (structural) patterns: Adapter, Bridge Used every day, even with the most simple programs Strategy: a full operation is dependent on the descendant class (DI with base class) Template: the operation is defined by the base class, some steps can be defined in the descendants (virtual methods, DI with base class) Adapter: How can we substitute a class/interface with a different one (DI with interface) Bridge: Decomposition of hierarchies (DI with base class) Dependency Injection??? GoF(1994); Martin Fowler (2004) 33

  34. Strategy 34

  35. Strategy C# 35

  36. Template method 36

  37. Template method C# 37

  38. Template method C# 38

  39. Strategy, Template method Practically the most widely used design patterns in any system (of any size!) Strategy: How to implement and use a library hierarchy? The base class (interface?) is fixed We write code that uses the base class Any descendant instance can be used later Our code calls the hierarchy s code Template: How to implement and use a framework? The base class defines the possibilities The base class is used by a full business logic/process We write code that utilizes the rules/possibilities of the base class (usually only a fraction of those) The externally defined business process will call our code 39

  40. Strategy = ASP.NET Core Identity A completely generic approach, does not care which descendant (= login implementation) is used PROG3, PROG4: The Logic does not care which data storage implementation is used 40

  41. Template = MVC Framework PROG4 During the render process, the WPF will SOMEWHERE call the OnRender() method of the FrameworkElement descendants WPF will SOMEHOW call the event handler/Command member ASP.NET will SOMEHOW call the Action handler method 41

  42. Thank you for your attention Behavioral design patterns Interpreter, Memento, State Stragety, Template method http://users.nik.uni-obuda.hu/prog4/ 42

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#