Visual Basic: An Object Oriented Approach 11 – Patterns in object oriented programming
2 Patterns - Motivation Other disciplines have handbooks Descriptions of successful solutions to known problems e.g. Auto engineers do not start a design from the laws of physics In software, tendency is to build everything that is not an identifiable component from scratch Difficulty in identifying previously developed assemblies
3 What are Design Patterns OOP Gives us classes and objects Reuse possible at class/object level (components) Systems are seldom simple assemblies of these Patterns give us tried and tested ways of interconnecting objects Reuse of algorithms, structures and mechanisms for generating objects Nothing new about patterns Patterns codify experience Experts tend to work with mental models that are higher- order abstractions
4 Sources of Patterns Standard text Gamma, Helm, Jonson & Vlissides Proposes 23 commonly used patterns Examples in C++ and Smalltalk (none in VB, but that’s what this chapter is about) Many other texts now available Most extend the range beyond the original 23 Journals with useful articles on patterns JOOP, Java Report Communications of the ACM Dr. Dobbs Journal
5 Types of Patterns Creational methods for generating objects Structural methods for interconnecting software elements Behavioral methods of performing typical operations
6 Benefits of Patterns Promote reusability not necessarily reusable themselves, but... provide for maximizing polymorphism Well tried patterns are always well defined developed in response to common problems Clarify the core purpose of code high-level abstractions
7 Patterns are Designs - What about implementations? Patterns are general can be implemented in most languages aim is to define good programming practice OOP with inheritance preferred - interface inheritance is (almost) essential In VB, we can develop interfaces that implement the essential features of a pattern Every use of a pattern is different, but draws from the same source as others A Pattern Interface is simple an Interface class that does something needed for the pattern
8 Creational patterns Aim here is to separate the creation of objects from a specific application Factory Method Abstract Factory Prototypes Singleton Builder Each pattern promotes a specific set of design decisions
9 Example of a Creational Pattern: the Factory Method Factory Object Generate(ID) Product Interface Product Class 1Product Class n Application Select Case ID Case 1 Set Generate = New CProduct1 Case 2 Set Generate = New CProduct2 Case…. End Select Application calls the Generate method, and is returned an object that implements the product interface
10 Factory Method Private Function GetNewDrawShape(desc As String) _ As IShape Dim S As IShape Select Case desc Case "Rectangle" Set S = New CRectangle Case "Ellipse" Set S = New CEllipse End Select Set GetNewDrawShape = S End Function ‘desc’ is a string that identifies the required class
11 Structural Patterns Aim is to generalize interconnections between objects Composite Adapter Bridge Decorator Facade Flyweight Proxy Decorator
12 Example of a Structural Pattern: Composite A Drawing A LineA CircleA Group A LineA Circle A LineA Circle A Group IShape Add( ) Remove( ) Draw( ) GetChild( ) CPrimitive Add( ) Remove( ) Draw( ) GetChild( ) CGroup Add( ) Remove( ) Draw( ) GetChild( ) In CPrimitive class, Add, Remove and GetChild do nothing
13 Composite Pattern Interface ‘ IComposite Interface class Public Sub Draw(P As PictureBox) End Sub Public Sub Add(Item As IComposite) End Sub Public Sub Remove(Item As IComposite) End Sub Public Function GetChild(n As integer) As IComposite End Function
14 Behavioural Patterns Aim is to create abstract operations Observer Iterator Chain of Responsibility Command Interpretor Mediator Memento State Strategy Template Method Visitor
15 Example of a Behavioural Pattern CSubject AddObserver(O As CObjerver) Notify( ) Update CObserver For Each o In observers o.Update Next
16 Observer Classes ‘Private Observers As Collection Private Sub Notify() Dim f As Form For Each f In Observers ‘ Send the Update message to each.. f.Update Hour(Time), Minute(Time), Second(Time) Next End Sub Private Sub AddObserver(f As Form) Observers.Add f End Sub
17 Where from here Gamma et al is a good starting point find out how they work find out what their benefits are find out how much effort Try them out Most are simple to implement All have some benefits to them Investigate specialist patterns Real-time, Process scheduling & concurrency, Distributed systems
18 Summary Patterns for reuse well tried solutions best use of OOP/polymorphism Glib statement of the week Don’t write a program - reuse a pattern!