Software engineering and GUI design Structural design patterns

  • Slides: 78
Download presentation
Software engineering and. GUI design Structural design patterns Adapter, Bridge Composite, Flyweight Facade, Proxy,

Software engineering and. GUI design Structural design patterns Adapter, Bridge Composite, Flyweight Facade, Proxy, Decorator http: //users. nik. uni-obuda. hu/prog 4/ 1

Purpose Design pattern Short description Structural Adapter Use an existing class/interface in place of

Purpose Design pattern Short description Structural Adapter Use an existing class/interface in place of a different class/interface Wrapper class Bridge Split up a big hierarchy into two hierarchies and connect them using composition Dependency injection Composite A tree data structure where the same operations are possible with the leaves, the subtrees and the whole tree Flyweight Reduce memory usage with shared internal parameters, singleton references, on-the-fly property calculation and instance pooling Façade Front controller, hide internal classes, simplify calls Proxy Transparent Façade AND extra functions/checks before/after the existing ones Decorator Assemble arbitrary functions via composition (dynamic inheritance) 2

Structural Design Patterns Purpose Scope Creational Structural Behavioral Class Factory method Adapter Interpreter Template

Structural 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

Adapter Object adapter Class adapter 4

Adapter Object adapter Class adapter 4

Adapter – C# 5

Adapter – C# 5

Adapter – C# 6

Adapter – C# 6

Adapter – C# 7

Adapter – C# 7

Adapter – C# 8

Adapter – C# 8

Adapter – C# 9

Adapter – C# 9

Adapter –. NET • Not only functionality/method signature-adapters exist • The Data Adapter accepts

Adapter –. NET • Not only functionality/method signature-adapters exist • The Data Adapter accepts data from different data sources (SQL Server, Oracle, ODBC, OLE DB) and transforms it into the sourceindependent Dataset/Data. Table types – Sql. Data. Adapter – Odbc. Data. Adapter – Ole. Db. Data. Adapter 10

Adapter –. NET • Not only functionality/method signature-adapters exist • LINQ – Linq provider:

Adapter –. NET • Not only functionality/method signature-adapters exist • LINQ – Linq provider: IQueryable + IQuery. Provider – Executing the same LINQ expression tree on a different data structure • Entity Framework – Model Adapter: create Db. Context code from an arbitrary database (T 4 code generation , SQL-first method) – Entity LINQ Adapter: From a LINQ query tree, create an SQL query with the dialect of the current connection 11

Bridge • Generally speaking: a hierarchy that depends on two factors • The two

Bridge • Generally speaking: a hierarchy that depends on two factors • The two factors are usually not identified at the beginning, so we organize the classes into one hierarchy • Typically we only realize later in time, that we have a problem: when we have to insert two-three descendant classes for one modification this is a typical refactoring pattern! • Solution: identify independent factors, cut the hierarchy into two, then connect them using Dependency Injection 12

Bridge 13

Bridge 13

Bridge – Before 14

Bridge – Before 14

Bridge – Before 15

Bridge – Before 15

Bridge – After 16

Bridge – After 16

Bridge 17

Bridge 17

Bridge – C# 18

Bridge – C# 18

Bridge – C# 19

Bridge – C# 19

Bridge – C# • The call itself is the same as with the Strategy

Bridge – C# • The call itself is the same as with the Strategy pattern • Email. Sender + Encryption. Algorithm = one-sided bridge • Bridge = Dependency Injection (with base class) Bridge = Go. F = 1994 Dependency Injection = Martin Fowler = 2004 https: //martinfowler. com/articles/injection. html 20

Structural Design Patterns Purpose Scope Creational Structural Behavioral Class Factory method Adapter Interpreter Template

Structural 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 21

Composite 22

Composite 22

Composite – C# 23

Composite – C# 23

Composite – C# 24

Composite – C# 24

Composite – C# 25

Composite – C# 25

Composite – C# 26

Composite – C# 26

Flyweight – motivation 27

Flyweight – motivation 27

Flyweight 28

Flyweight 28

Flyweight – C# • Perception: all characters are treatable… Actually, only created/calculated when needed

Flyweight – C# • Perception: all characters are treatable… Actually, only created/calculated when needed • Optimization FOR MEMORY, don’t care about cpu… • You might not use the pattern as a whole, but the tricks are usable as individual tricks • Used a lot in low-memory / resource-intensive operations: Thread. Pool, Connection Pool, WPF Recycled+Virtualized Listbox, Scrolling UI controls (Android: Recycler. View) 29

Flyweight – C# – Trick#1: Singleton 30

Flyweight – C# – Trick#1: Singleton 30

Flyweight – C# – Trick#2 + Trick#3 31

Flyweight – C# – Trick#2 + Trick#3 31

Flyweight – C# – Trick#4: „Local” properties 32

Flyweight – C# – Trick#4: „Local” properties 32

Flyweight – C# – Trick#5: On-the-fly property 33

Flyweight – C# – Trick#5: On-the-fly property 33

Flyweight – C# – Trick#6: Object pooling • Object pooling/smart factories – limit=1: Get.

Flyweight – C# – Trick#6: Object pooling • Object pooling/smart factories – limit=1: Get. Instance() / Create. Instance() [Hidden Singleton Factory] – Without limit or limit>1: Get. Instance() + Release. Instance() [Object Pool Pattern] • . NET: Threadpool, EF Connection. Pool, Virtualized Listbox • Android: scrolling control/Recycler. View • . NET core: System. Buffers. Memory. Pool, System. Buffers. Array. Pool, Microsoft. Extensions. Object. Pool – var rented. Array = System. Buffers. Array. Pool<int>. Shared. Rent(10); – System. Buffers. Array. Pool<int>. Shared. Return(rented. Array); • Very common in resource-scarce scenarios 34

Flyweight – C# – Trick#6: Object pooling 35

Flyweight – C# – Trick#6: Object pooling 35

Flyweight – C# – Trick#6: Object pooling 36

Flyweight – C# – Trick#6: Object pooling 36

Flyweight – C# – All Tricks Used Together 37

Flyweight – C# – All Tricks Used Together 37

Flyweight –. NET/JAVA: String (. NET: Type) 38

Flyweight –. NET/JAVA: String (. NET: Type) 38

Structural Design Patterns Purpose Scope Creational Structural Behavioral Class Factory method Adapter Interpreter Template

Structural 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 39

Facade 40

Facade 40

Facade 41

Facade 41

Facade • We want to simplify the many internal dependencies, we want a single

Facade • We want to simplify the many internal dependencies, we want a single point of access from the outside • Con: easy to fall into extremes (God Object – Too Many Responsibilities) – Must find the middle ground – This is required in the inter-layer communications in the prog 3/prog 4 projects: ILogic, ICar. Logic, IMonster. Logic? • Composition technique – Proxy/Decorator as well same look, different logical meaning – The Facade class typically adds no extra operations/checks to the method calls – Might define calling order, but its main task is to simplify the method calls and the dependency network – The structure of the Facade class and its existence is KNOWN 42

Facade – C# 43

Facade – C# 43

Facade – VB. NET 44

Facade – VB. NET 44

Facade – Architectural pattern 45

Facade – Architectural pattern 45

Proxy 46

Proxy 46

Proxy • Aim: replace an existing instance with a different one, but all this

Proxy • Aim: replace an existing instance with a different one, but all this in a way that the existence of the proxy is NOT necessarily known • It is a very common design pattern – EF: Db. Context. Save. Changes() might save proxied entities – Linq: query results are iterator proxies (Move. Next+Current) • Types – Remote proxy : looks like a local reference, but the real instance is really in another location (Class? App? Network? ) – Virtual proxy: all properties seem like they are always set, but the expensive instances are only created when accessed (partial lazy load) – Protection proxy: controls the access to the instance – Smart Reference: an instance that performs extra operations when calling already existing methods (measurement , lazy load, lock, cache) 47

Proxy – EF / Moq 48

Proxy – EF / Moq 48

Proxy – WCF 49

Proxy – WCF 49

Proxy – WCF 50

Proxy – WCF 50

Proxy – g. RPC proto 51

Proxy – g. RPC proto 51

Proxy – g. RPC Server + Client 52

Proxy – g. RPC Server + Client 52

Proxy – C# example 53

Proxy – C# example 53

Proxy – C# 54

Proxy – C# 54

Proxy – C# 55

Proxy – C# 55

Proxy – C# 56

Proxy – C# 56

Decorator 57

Decorator 57

Decorator • Composition (like the facade/proxy), the decorator class has the same base class/implements

Decorator • Composition (like the facade/proxy), the decorator class has the same base class/implements the same interface as the class hidden with the composition • Very flexible – With some functions, it can behave like a façade (pass-through) – Some functions can be extended, as with a proxy – It can add completely new functionality/methods as well • Consequences – Since same base class/interface, a decorator can be used to decorate an X class, or another decorator of the same type – Same base class/interface, so the class that doesn`t know about the decorator can use the decorated instance without problems – Add/remove responsibilities/functionality dynamically – Dynamic inheritance = huge flexibility 58

Facade vs Proxy vs Decorator Pattern Adds Logic? Can be stacked? NO Facade NO

Facade vs Proxy vs Decorator Pattern Adds Logic? Can be stacked? NO Facade NO (max. order) Proxy YES RARELY (only pre/post) RARELY Decorator YES (fully new) YES (But can work without) YES!!!! Known? YES 59

Decorator 60

Decorator 60

Decorator –. NET 61

Decorator –. NET 61

Decorator – WPF 62

Decorator – WPF 62

Decorator – C# 63

Decorator – C# 63

Decorator – C# 64

Decorator – C# 64

Decorator – C# 65

Decorator – C# 65

Decorator – C# 66

Decorator – C# 66

Decorator – C# 67

Decorator – C# 67

Decorator – C# 68

Decorator – C# 68

Decorator – C# 69

Decorator – C# 69

Decorator – C# 70

Decorator – C# 70

Decorator – C# 71

Decorator – C# 71

Decorator – C# 72

Decorator – C# 72

Composition with the structural patterns • Cannot be decided that which one is used,

Composition with the structural patterns • Cannot be decided that which one is used, just by looking at the code they all use composition, data field of a related class • Adapter convert to different signature/data format/representation • Bridge connect two hierarchies • Façade simplify calls/subsystem access • Proxy add new operations/checks to existing functions • Decorator add completely new functions 73

Go. F Design Patterns Purpose Scope Creational Structural Behavioral Class Factory method Adapter Interpreter

Go. F 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 74

Go. F patterns Purpose Design pattern Short description Creational Factory method Get. Instance(instance. Type)

Go. F patterns Purpose Design pattern Short description Creational Factory method Get. Instance(instance. Type) Abstract factory Multiple factories, Get. Instance(factory, instance. Type) or Get. Instance(instance. Type) Singleton Usage of a single instance (. Default /. Current) Prototype Creation of a Deep copy (. Clone()) Builder Creation of an instance using multiple steps/part/processes 75

Purpose Design pattern Short description Behavioral Iterator foreach () { } Chain of resp.

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 76

Purpose Design pattern Short description Structural Adapter Use an existing class/interface in place of

Purpose Design pattern Short description Structural Adapter Use an existing class/interface in place of a different class/interface Wrapper class Bridge Split up a big hierarchy into two hierarchies and connect them using composition Dependency injection Composite A tree data structure where the same operations are possible with the leaves, the subtrees and the whole tree Flyweight Reduce memory usage with shared internal parameters, singleton references, on-the-fly property calculation and instance pooling Façade Front controller, hide internal classes, simplify calls Proxy Transparent Façade AND extra functions/checks before/after the existing ones Decorator Assemble arbitrary functions via composition (dynamic inheritance) 77

Thank you for your attention Structural design patterns Adapter, Bridge Composite, Flyweight Facade, Proxy,

Thank you for your attention Structural design patterns Adapter, Bridge Composite, Flyweight Facade, Proxy, Decorator http: //users. nik. uni-obuda. hu/prog 4/ 78