Presentation is loading. Please wait.

Presentation is loading. Please wait.

Technieken van de Software Architectuur, VUB ‘98-’99, Part 41 Part 4: Design Patterns.

Similar presentations


Presentation on theme: "Technieken van de Software Architectuur, VUB ‘98-’99, Part 41 Part 4: Design Patterns."— Presentation transcript:

1 Technieken van de Software Architectuur, VUB ‘98-’99, Part 41 Part 4: Design Patterns

2 Technieken van de Software Architectuur, VUB ‘98-’99, Part 42 Background 4Designing reusable software is difficult »Finding good objects and abstractions »Flexibility, Modularity, Elegance »Takes time to emerge, trial and error 4Successful designs exist 4How to describe recurring structures »Deja-Vu feeling, don’t reinvent the wheel, don’t reinvent the flat tire. 4Design Patterns: Elements of Reusable Object Oriented Software »E. Gamma R. Helm R. Johnson J. Vlissides

3 Technieken van de Software Architectuur, VUB ‘98-’99, Part 43 Design Patterns 4Design patterns capture successful solutions to recurring problems that arise during software construction 4Design patterns help to improve key software quality factors 4Design patterns support reuse of software architectures

4 Technieken van de Software Architectuur, VUB ‘98-’99, Part 44 A Design Pattern 4Describes a recurring design structure »Used twice rule »Names, abstracts from concrete designs »Identifies classes, collaborations, responsibilities »Applicability, trade-offs, consequences, language issues 4Is discovered, not invented

5 Technieken van de Software Architectuur, VUB ‘98-’99, Part 45 Practical Experience 4Design Patterns are based on practical solutions found in main-stream applications implemented in Smalltalk and C++ »Windowing Systems »CAD »Banking »Persistent Objects »Distributed Systems »...

6 Technieken van de Software Architectuur, VUB ‘98-’99, Part 46 Describing Design Patterns 4Pattern name and classification 4Intent 4Also Known As 4Motivation 4Applicability 4Structure 4Participants 4Collaborations 4Consequences 4Implementation 4Sample Code 4Known Uses 4Related Patterns

7 Technieken van de Software Architectuur, VUB ‘98-’99, Part 47 Catalogue of Design Patterns 4Purpose »Creational »Structural »Behavioural 4Scope »Class »Object

8 Technieken van de Software Architectuur, VUB ‘98-’99, Part 48 Structural Patterns 4Adaptor 4Bridge 4Composite 4Decorator 4Facade 4Flyweight 4Proxy

9 Technieken van de Software Architectuur, VUB ‘98-’99, Part 49 Behavioural Patterns 4Chain of Responsibility 4Command 4Interpreter 4Iterator 4Mediator 4Memento 4Observer 4State 4Strategy 4Template Method 4Visitor

10 Technieken van de Software Architectuur, VUB ‘98-’99, Part 410 Design Coverage 4Large Portion of design covered by patterns »Most classes play role in some patterns »Improved understanding 4Seductively simple to implement patterns 4You still have to write functionality »Common mistake is to think design patterns solve your problems

11 Technieken van de Software Architectuur, VUB ‘98-’99, Part 411 Abstract Factory

12 Technieken van de Software Architectuur, VUB ‘98-’99, Part 412 Abstract Factory 4Object Creational 4Intent: Provide an interface for creating families of objects without specifying the concrete classes 4Motivation: Creation of line segments, points,... in a drawing framework

13 Technieken van de Software Architectuur, VUB ‘98-’99, Part 413 Abstract Factory: Motivation ComponentFactory newLine() {abstract} newPoint() {abstract} GrayComponentFactory newLine() newPoint() ColorComponentFactory newLine() newPoint() ClientLineColorLineGrayLinePoint ColorPoint GrayPoint

14 Technieken van de Software Architectuur, VUB ‘98-’99, Part 414 Abstract Factory: Applicability 4Use the Abstract Factory Pattern when: »A system has to be independent of how its objects are created, composed and represented »Configuration with families of products is necessary »A family of products is designed to work together

15 Technieken van de Software Architectuur, VUB ‘98-’99, Part 415 Abstract Factory: Participants 4AbstractFactory (e.g. ComponentFactory) 4ConcreteFactory (e.g. ColorComponentFactory) 4AbstractProduct (e.g. Point) 4ConcreteProduct (e.g. ColorPoint) 4Client

16 Technieken van de Software Architectuur, VUB ‘98-’99, Part 416 Abstract Factory: Consequences 4It isolates clients from concrete classes 4Makes exchanging of product families easier 4It promotes consistency amongst products 4Supporting new kinds of products is difficult

17 Technieken van de Software Architectuur, VUB ‘98-’99, Part 417 Abstract Factory: Implementation 4Factories are best implemented as Singletons 4Creating the products »(1) Use a Factory Method for each product »(2) Initialize the concrete factory with a prototypical instance that can be copied 4Defining extensible factories »In AbstractFactory provide a parameter to identify the product to be created

18 Technieken van de Software Architectuur, VUB ‘98-’99, Part 418 Bridge

19 Technieken van de Software Architectuur, VUB ‘98-’99, Part 419 Example: Variations in storage InformixProgram... findPrograms... Program duration() productCode() broadcastitle (title: Title) static findPrograms (s:searchObject) : ProgramList... IngresProgram... findPrograms OracleProgram... findPrograms

20 Technieken van de Software Architectuur, VUB ‘98-’99, Part 420 The Original Persistency Layer 4Allow non DB experts to work on the application 4Migration to other DB's 4Be ready for OO databases Domain Model Persistency Layer Store Retrieve Hematomas

21 Technieken van de Software Architectuur, VUB ‘98-’99, Part 421 bcPIDProgramIDcontractPID Example Program contractPeriod broadcastPeriod Period from: 01/01/96, 00h00 to: 01/02/96, 00h00 from: 07/01/96, 20h00 to: 07/01/96, 22h00 Period ProgramTable: PeriodTable: PeriodIDfromDatefromHourtoDatetoHour foreign key

22 Technieken van de Software Architectuur, VUB ‘98-’99, Part 422 Solution: Bridge Pattern 4Intent: Decouple an abstraction from its implementation so that the two can vary independently 4Motivation: Window Implementations

23 Technieken van de Software Architectuur, VUB ‘98-’99, Part 423 Bridge: Motivation Window DrawText() DrawRect WindowImp DrawText() DrawRect TransientWindow DrawCloseBox IconWindow DrawBorder() Window95Imp DrawText() DrawRect XWindowImp DrawText() DrawRect Imp

24 Technieken van de Software Architectuur, VUB ‘98-’99, Part 424 Bridge: Applicability 4Use the Bridge Pattern to »Avoid a permanent binding between an abstraction and its implementation »Both the abstractions and their implementations should be extensible by subclassing »To avoid proliferation of classes Separation of Concerns

25 Technieken van de Software Architectuur, VUB ‘98-’99, Part 425 Bridge: Structure Abstraction Operation() Implementor OperationImp() imp RefinedAbstraction ConcreteImpB OperationImp() ConcreteImpA OperationImp() Client Bridge Participants

26 Technieken van de Software Architectuur, VUB ‘98-’99, Part 426 Bridge: Consequences 4Decoupling interface and implementation 4Improved extensibility 4Hiding implementation details from clients

27 Technieken van de Software Architectuur, VUB ‘98-’99, Part 427 Implementation Object Knows how to map itself to a relational database Implementation Object Knows how to map itself to a relational database The Bridge Architecture A B C+D Conceptual Object Contains business rules

28 Technieken van de Software Architectuur, VUB ‘98-’99, Part 428 DomainObject store() initialize() storage () The Bridge Architecture StorageObject... Program title(t:Title) BasicStorageAdaptorStorage FlattenedStorage HierarchyStorage Mapping Strategies storagedomain

29 Technieken van de Software Architectuur, VUB ‘98-’99, Part 429 Bridge: Basic Mappings DomainObject store() initialize()... StorageObject Firm name(s:String)... BasicStorage FirmStorage name:String name (s:String)... name [name] name addr... PSIFirmTable

30 Technieken van de Software Architectuur, VUB ‘98-’99, Part 430 Bridge: Basic Mappings 4A commercially available framework is used for the basic mappings (Object Lens) 4The abstract classes contain behavior for querying, retrieval, etc. of data 4The basic mappings are database independent

31 Technieken van de Software Architectuur, VUB ‘98-’99, Part 431 Bridge: Mappings of Classes A B C C B A+B+C A FlattenedStorage HierarchyStorage

32 Technieken van de Software Architectuur, VUB ‘98-’99, Part 432 FlattenedStorage Bridge: FlattenedStorage DomainObject Contract SalesContractPurchaseContract StorageObject BasicStorage ContractStorage date:String date (s:String)... name addr classID

33 Technieken van de Software Architectuur, VUB ‘98-’99, Part 433 BProgramStorage Bridge: HierarchyStorage DomainObject Program Episode StorageObject BasicStorage BEpisodeStorage HierarchyStorage ProgramStorage EpisodeStorage programPart episodePart

34 Technieken van de Software Architectuur, VUB ‘98-’99, Part 434 Bridge: Adaptor Mapping Program broadcastPeriod contractPeriod Period from: 01/01/96, 00h00 to: 01/02/96, 00h00 from: 07/01/96, 20h00 to: 07/01/96, 22h00 Period ProgramTable bcFromDate bcFromTime bcToDate bcToTime cntrFromDate cntrFromTime... bcFromDate bcFromTime bcToDate bcToTime cntrFromDate cntrFromTime cntrToDate cntrToTime ProgramStorage Conceptual ObjectsImplementation Objects BCPeriodStorage Database toDate[toDate] toDate[bcToDate]

35 Technieken van de Software Architectuur, VUB ‘98-’99, Part 435 Bridge: Adaptor Mapping DomainObject store() initialize()... StorageObject Period from():Time to(): Time BasicStorage ProgramStorage bcFrom:Date bcTo:Date AdaptorStorage BCPeriodStorage from():Time to():Time Adaptor design pattern to [bcTo] adaptee

36 Technieken van de Software Architectuur, VUB ‘98-’99, Part 436 Adaptors Period to():Time from(): Time includesTime(t:Time):Boolean merge(p:Period):Period PeriodAdaptor PeriodImpl to:Time from: Time to():Time from(): Time Period to():Time from(): Time includesTime(t:Time):Boolean merge(p:Period):Period

37 Technieken van de Software Architectuur, VUB ‘98-’99, Part 437 Observer

38 Technieken van de Software Architectuur, VUB ‘98-’99, Part 438 Observer 4Intent: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. 15,20,40 4Motivation: Maintain consistency between objects without a tight coupling that reduces reusability.

39 Technieken van de Software Architectuur, VUB ‘98-’99, Part 439 Observer : Applicability 4Use Observer when »an abstraction has two aspects, one dependent on the other. Encapsulating these aspects in separate objects lets you vary and reuse them independently. »a change to one object requires changing others, and you don’t know how many objects need to be changed »an object should be able to notify other objects without making assumptions about who these objects are. Separation of Concerns

40 Technieken van de Software Architectuur, VUB ‘98-’99, Part 440 Observer : Structure Subject attach(Observer) dettach(Observer) Notify() Observer update() ConcreteObserver observerState update() state ConcreteSubject subjectState getState() setState() forall o in observers o update return subjectState observerState= subject subjectState Observer Participants

41 Technieken van de Software Architectuur, VUB ‘98-’99, Part 441 Observer : Consequences 4abstract coupling between subject and observer 4support for broadcast communication 4unexpected updates

42 Technieken van de Software Architectuur, VUB ‘98-’99, Part 442 State

43 Technieken van de Software Architectuur, VUB ‘98-’99, Part 443 State 4Object Behavioral 4Intent: Allow an object to alter its behavior when its internal state changes. The object will appear to change its class 4Motivation: Network connection

44 Technieken van de Software Architectuur, VUB ‘98-’99, Part 444 State: Motivation TCPConnection open () close() TCPState clone() TCPListen open() close() TCPClosed open() close() state TCPEstablished open() close() state close()

45 Technieken van de Software Architectuur, VUB ‘98-’99, Part 445 State : Applicability 4Use State »when an object’s behavior depends on its state and it must chaneg its behavior at runtime »when operations have large conditional statements that depend on the state of the objects

46 Technieken van de Software Architectuur, VUB ‘98-’99, Part 446 State : Participants 4Context (e.g. TCPConnection) 4State (e.g. TCPState) 4ConcreteState (subclasses of State)

47 Technieken van de Software Architectuur, VUB ‘98-’99, Part 447 State : Structure Context request() State handle() TCPListen handle() TCPClosed handle() state TCPEstablished handle() state handle()

48 Technieken van de Software Architectuur, VUB ‘98-’99, Part 448 State : Consequences 4separates state-specific behavior from general behavior 4Makes state-transitions explicit 4State objects can be shared

49 Technieken van de Software Architectuur, VUB ‘98-’99, Part 449 Composite

50 Technieken van de Software Architectuur, VUB ‘98-’99, Part 450 BUT? How to combine ? - multiple inheritance - mixin classes + Node LoggingNode WorkstationPrintserver Node Separate discussion (design patterns)

51 Technieken van de Software Architectuur, VUB ‘98-’99, Part Program duration() productCode() broadcastitle (title: Title) static findPrograms (s:searchObject) : ProgramList... Example: Variations in the domain Series... Episode...

52 Technieken van de Software Architectuur, VUB ‘98-’99, Part 452 Example: Variations in the domain ComposedProgram duration () Program duration() broadcastitle (title: Title) static findPrograms (s:searchObject) : ProgramList... ProgramSlice duration ()

53 Technieken van de Software Architectuur, VUB ‘98-’99, Part 453 Combining variations 4Duplication of behavior »Not a satisfying solution »Proliferation of versions 4Concentrate behaviour »Not a satisfying solution »Overfeaturing ProgramSlice Program Episode EpisodeSlice Program isSlice...

54 Technieken van de Software Architectuur, VUB ‘98-’99, Part 454 Composite Programs ComposedProgram duration () Program duration()... SeriesEpisode duration [duration]

55 Technieken van de Software Architectuur, VUB ‘98-’99, Part 455 A Solution Program duration()... SeriesEpisode Structure duration()... Composed duration() Atomic duration() Sliced duration() concretisation parts duration [duration] Separation of Concerns State design pattern Composite design pattern structure

56 Technieken van de Software Architectuur, VUB ‘98-’99, Part 456 Decorator

57 Technieken van de Software Architectuur, VUB ‘98-’99, Part 457 Decorator: Participants 4Component (e.g. VisualComponent) 4ConcreteComponent (e.g. TextView) 4Decorator (e.g. Decorator) 4ConcreteDecorator (e.g. BorderDecorator)

58 Technieken van de Software Architectuur, VUB ‘98-’99, Part 458 Decorator: Structure Component Operation() ConcreteComponent Operation() Decorator Operation() component ConcreteDecoratorA added state Operation() added behaviour ConcreteDecoratorB added state Operation() added behaviour component operation super operation added behaviour

59 Technieken van de Software Architectuur, VUB ‘98-’99, Part 459 Decorator: Consequences 4More flexible than static inheritance. 4Avoids classes with a lot of features »Refactoring method ! »Lots of little objects »Avoids duplication of features 4Object identity can be a problem

60 Technieken van de Software Architectuur, VUB ‘98-’99, Part 460 Decorator: Participants 4Component (e.g. VisualComponent) 4ConcreteComponent (e.g. TextView) 4Decorator (e.g. Decorator) 4ConcreteDecorator (e.g. BorderDecorator)

61 Technieken van de Software Architectuur, VUB ‘98-’99, Part 461 Decorator: Structure Component Operation() ConcreteComponent Operation() Decorator Operation() component ConcreteDecoratorA added state Operation() added behaviour ConcreteDecoratorB added state Operation() added behaviour component operation super operation added behaviour

62 Technieken van de Software Architectuur, VUB ‘98-’99, Part 462 Decorator: Consequences 4More flexible than static inheritance. 4Avoids classes with a lot of features »Refactoring method ! »Lots of little objects »Avoids duplication of features 4Object identity can be a problem

63 Technieken van de Software Architectuur, VUB ‘98-’99, Part 463 Design Patterns in Practice Architectural Layers Integration Redundancy Data Warehousing

64 Technieken van de Software Architectuur, VUB ‘98-’99, Part 464 Firstname Lastname Age Person Editor Person Reusable Objects/Components

65 Technieken van de Software Architectuur, VUB ‘98-’99, Part 465 Firstname Lastname Age Person Editor Person Separation of Concerns

66 Technieken van de Software Architectuur, VUB ‘98-’99, Part 466 Drawing Editor Firstname Lastname Age Person Editor License Brand Age Car Editor Person Car Drawing Layered Architecture

67 Technieken van de Software Architectuur, VUB ‘98-’99, Part 467 Bridge: Integration PSI PSI DB Other Application External DB Bridge

68 Technieken van de Software Architectuur, VUB ‘98-’99, Part 468 Controlling Redundancy FirmID Firm Name Title Program Editor StorageObject BasicStorage FirmStorage name:String name (s:String) id: Sring id (s:String) ProgramStorage firmName:String firmName (s:String) firmId: Sring firmId (s:String)... Updates programStorage if name or id changes

69 Technieken van de Software Architectuur, VUB ‘98-’99, Part 469 Data Warehousing: CASE Number Amount Owner Account Editor Application RDBMS Mainframe Application for account management CICS Transactions Mirrored DB...

70 Technieken van de Software Architectuur, VUB ‘98-’99, Part 470 AccountStorage amount(): Integer owner(): Owner... Data Warehousing: CASE Account amount(): Integer owner(): Owner... storage Bridge Number Amount Owner Account Editor

71 Technieken van de Software Architectuur, VUB ‘98-’99, Part 471 AccountStorage amount(): Integer owner(): Owner... Data Warehousing: CASE Account amount(): Integer owner(): Owner... storage Bridge Number Amount Owner Account Editor Transaction CICS CICS1234 CICS1235 Adaptor

72 Technieken van de Software Architectuur, VUB ‘98-’99, Part 472 AccountStorage amount(): Integer owner(): Owner... Data Warehousing: CASE Account amount(): Integer owner(): Owner... storage Bridge Number Amount Owner Account Editor Transaction CICS CICS1234 CICS1235 Adaptor CICSAccountAmount set(amount: Integer) get(): Integer Facade

73 Technieken van de Software Architectuur, VUB ‘98-’99, Part 473 Data Warehousing Account amount(): Integer owner(): Owner... storage AccountStorage amount(): Integer owner(): Owner... Transaction CICS CICS1234 CICS1235 CICSAccountAmount set(amount: Integer) get(): Integer Facade Adaptor AccountBasicStorage Bridge Number Amount Owner Account Editor

74 Technieken van de Software Architectuur, VUB ‘98-’99, Part 474 Bridge: Summary 4Provides a clean separation between domain and storage 4Allows easy integration with other systems 4Various mappings had to be included »NOT part of the Bridge Design Pattern


Download ppt "Technieken van de Software Architectuur, VUB ‘98-’99, Part 41 Part 4: Design Patterns."

Similar presentations


Ads by Google