CSCI 6234 Object Oriented Design: Frameworks and Design Patterns George Blankenship Frameworks and Design George Blankenship 1
Background A class is a mechanisms for encapsulation, it embodies a certain service providing the data and behavior that is useful in the context of some application. A single class is rarely the complete solution to some real problem There is a growing body of research in describing the manner in which collections of classes work together in the solution of problems. Application frameworks and design patterns are two ideas that became popular in this context Frameworks and Design George Blankenship 2
Application Frameworks An application framework is a set of classes that cooperate closely with each other and together embody a reusable design for a general category of problems Although one might abstract and discuss the design elements that lie behind the framework, the framework itself is a set of specific classes that are typically implemented only on a specific platform or limited set of platforms The framework dictates the overall structure and behavior of the application. It describes how responsibilities are partitioned between various components and how these components interact Frameworks and Design George Blankenship 3
A Framework as an Upside-down Library In a traditional application the application-specific code defines the overall flow of execution through the program occasionally invoking library-supplied code A framework reverts this relation: the flow of control is dictated by the framework and the creator of a new application merely changes some of the methods invoked by the framework Inheritance is often used as powerful mechanism for achieving this. Alternatively applicationspecific components that obey a specified interface are plugged in Frameworks and Design George Blankenship 4
GUI Application Framework GUI application frameworks simplify the creation of graphical user interfaces for software systems A GUI application framework implements the behavior expected from a graphical user interface windows, buttons, menu's, text fields move and resize windows handle mouse events on buttons and menus A new application is built by specifying and arranging the necessary elements and by redefining certain methods buttons, menu's, text fields responses to the mouse and key events Frameworks and Design George Blankenship 5
Simulation Application Framework Simulation frameworks simplify the creation of simulation style applications A simulation framework provides a general-purpose class for managing the types of objects in the simulation The heart of a simulation framework is a procedure that cycles through the list of objects introduced in the simulation asking each to update itself The framework knows nothing about the particular application it is going to be used for: billiard balls, fish in a tank, rabbits and wolves in an ecological game, etc. Frameworks and Design George Blankenship 6
Family GCB Application Framework Link list GUI Application Log Finite State Machine TCP/IP communications System Operational Parameters System startup Used to build a standard substructure for application Frameworks and Design George Blankenship 7
GCB Linked List Create linked lists Add/remove items Traverse a list Define an object as a list entry Add arbitrary object to a list List Head First entry Last entry Count of entries List entry Next entry Prior entry List entry Next entry Prior entry List entry Next entry Prior entry List entry Next entry Prior entry List entry Next entry Prior entry Frameworks and Design George Blankenship 8
GCB GUI Build/maintain main GUI Menu bar Scrolling text area Application specific widgets Input area Time display Build/manage Solicitation GUI Build application parameter panel Build application text validation widget Frameworks and Design George Blankenship 9
GCB Application Log Creation an display of program execution log entries Tagged with time and class that generated entry General entry Exception entry Display to GUI, file, command box Controlled by component <date time><component><text> Frameworks and Design George Blankenship 10
GCB Finite State Machine FSM creation and completion Transition table Event driven processing Missing event detection FSM execution Thread safe execution Get current state Execute state transition Trace FSM execution Frameworks and Design George Blankenship 11
GCB TCP/IP Communications Create TCP port Passive port (listen) Active port (connection) Send message Receive message Trace program execution Frameworks and Design George Blankenship 12
System Operational Parameters Repository (FileStore) Data storage System constants System properties Operating parameters subsystem Initial collection operating parameters Read/update of parameters Frameworks and Design George Blankenship 13
System startup Template for execution root class Class with main() Order the initialization of system components Standardize the initialization of the environment Standardize the management of the system Frameworks and Design George Blankenship 14
Design Patterns Design patterns are an attempt to collect and catalog the smallest recurring architectures in object oriented software systems. A design patterns typically captures the solution to a problem that has been observed in many different systems Design patterns are more abstract than a framework. Frameworks are partial implementations of (sub)systems, design patterns have no immediate implementation at all Design patterns are smaller architectural elements than frameworks, most applications (and frameworks) will use several patterns Design patterns are architectural level counterparts of programming idioms Frameworks and Design George Blankenship 15
Sorts of Design Patterns Creational Patterns: are concerned with the process of object creation Structural Patterns: are concerned with how classes and objects are composed to form larger structures purpose Behavioural Patterns: are concerned with algorithms and the assignment of responsibilities between objects Class Patterns deal with static relationships between classes and subclasses scope Object Patterns deal with object relationships which can be changed at run time Frameworks and Design George Blankenship 16
Creational Design Patterns Abstracts the instantiation process: Encapsulates knowledge about which concrete classes to use Hides how the instances of these classes are created and put together Gives a lot of flexibility in what gets created, who creates it, how it gets created, and when it it gets created A class creational pattern uses inheritance to vary the class that is instantiated An object creational pattern delegates instantiation to another object Frameworks and Design George Blankenship 17
Structural Design Patterns Structural design patterns are concerned with how classes and objects are composed to form larger structures A class structural pattern uses inheritance to compose interfaces or implementation; compositions are fixed at design time An object structural pattern describes ways to compose objects to realise new functionality; the added flexibility of object composition comes from the ability to change the composition at run-time Frameworks and Design George Blankenship 18
Behavioral Design Patterns Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects Behavioral class patterns use inheritance to distribute behavior between classes Behavioral object patterns use object composition rather than inheritance; some describe how a group of peer objects cooperate to perform a tasks no single object can carry out by itself; others are concerned with encapsulating behavior in an object and delegating request to it Frameworks and Design George Blankenship 19
CREATIONAL PATTERNS Singleton Abstract factory Factory Method Prototype Builder Frameworks and Design George Blankenship 20
The Singleton Control over the number of objects created A single instance is the only instance expected The object is really a serial service provider The object is a repository for other objects Limited number of objects that all share a common environment Frameworks and Design George Blankenship 21
The Abstract Factory Create an object or collection of objects tailored to a context The context may be imposed by outside conditions The context may be selected by parameters The actual collection is one of a set that has the proper behaviour for the context Each object in the set performs the same function Frameworks and Design George Blankenship 22
The Factory Method Create an object or collection of objects that meet a tailored response to a general problem Each collection performs the task in a different manner with a different set of results The requestor selects the type of results and the proper object set is created Frameworks and Design George Blankenship 23
The Prototype Create objects that are clones of a model Rapid creation of similar objects Clone Initialize Simple approach object generation Frameworks and Design George Blankenship 24
The Builder Create complex multifunctional objects performing a multistage task Create (assemble) a tool kit object that is adaptable to variations in a task Frameworks and Design George Blankenship 25
STRUCTURAL PATTERNS Composite Façade Proxy Flyweight Adapter Bridge Decorator Frameworks and Design George Blankenship 26
The Composite The structure of an object hides a hierarchically organized set of core objects The surface allows the user to access the core (traverse the core) without knowledge of the organizational elements (nodes and leaves) Frameworks and Design George Blankenship 27
The Façade The façade organizes a set of core objects (or external objects) by defining a solution oriented surface (external signature) The new objects provide a standard set of computational solutions by invoking the core objects in the proper sequence The core objects could be independently invoked to provide the same solution Frameworks and Design George Blankenship 28
The Proxy The surface of the object presents a virtual core (computation) The core reality is distinct from the virtual The complexity of the core computation is hidden by the surface to allow for completely different object representation, management, or access protection Frameworks and Design George Blankenship 29
The Flyweight The structure of the object is a large number of interior objects that would normally present a footprint problem The interior objects are made a light weight (flyweight) as possible to minimize the footprint of each object External objects (container objects) are used to add common attributes to the flyweight objects Frameworks and Design George Blankenship 30
The Adapter The structure of the object is a core of an existing class providing the computational solution The surface of the object (external signature) is mapped to the internal core Provides a new API for an existing class Allows legacy objects to be used in a new environment Frameworks and Design George Blankenship 31
The Bridge The structure of the object is a core selected from a class of similar cores The surface of the object (external signature) is a standard API that invokes the appropriate core methods The bridge allows multiple cores be bridged into new uses (environments) Frameworks and Design George Blankenship 32
The Decorator The structure of the object is a core of an existing class providing the computational solution Secondary capabilities are added as features to the core The surface of the object (external signature) is a combination of the core and the features giving the object extra capabilities Frameworks and Design George Blankenship 33
BEHAVIORAL PATTERNS Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor Frameworks and Design George Blankenship 34
The Chain of Responsibility The processing of a is spread across a set (chain) of objects The processing sequence is accomplished by moving the processing through the sequence Individual objects in the chain may/or may not perform a computation The sequence may be controlled (steered) by the individual objects Frameworks and Design George Blankenship 35
The Command Individual elements of a computation are implemented as objects The objects are then invoked to perform the computation The objects can be associated with other components (widgets) such that the invocation of the widget causes the computation to be performed Frameworks and Design George Blankenship 36
The Interpreter A language is created that can be used to define a computation The objects of the system are language elements and a computation can be performed by writing a program using the objects The language constrains the solution space (type of problems that can be addressed) Frameworks and Design George Blankenship 37
The Iterator Processing of aggregations can be accomplished by organizing the aggregations in a meaningful manner The surface of the organization object allows the access to the individual objects in a meaningful manner such as in order or by selection Frameworks and Design George Blankenship 38
The Mediator The computation of a system may be spread across multiple disparate objects that do not have surfaces that interlock Mediator objects are created to pass the processing between the computational entities by tailoring the interfaces and/or selecting the correct next required object in the computation Frameworks and Design George Blankenship 39
The Memento Processing may include the concept of stages The processing may progress forward from stage to stage keeping the prior stages as a history The processing my move backwards and forwards in the stage sequence The memento is used to capture prior stages for return Frameworks and Design George Blankenship 40
The Observer Processing may be keyed to the occurrence of events An observers object monitors for the occurrence of an event When an event occurs the observer coordinates the processing appropriate for the event Frameworks and Design George Blankenship 41
The State Processing may be defined using a Finite State Machine (FSM) The core object of the computation need to recognize their location on the FSM graph and change their processing to match their location on the graph Frameworks and Design George Blankenship 42
The Strategy The processing may include distinct computations An individual computation may have a family of algorithms that reflect different optimal characteristics Objects are defined for each algorithm and used to meet a particular (ad hoc) objective (or strategy) Frameworks and Design George Blankenship 43
The Template Method Processing may be defined by an algorithm that is defined by individual steps Each step is a computational entity (object) The algorithm may be reused for similar computations by tailoring the individual objects the define a step Frameworks and Design George Blankenship 44
The Visitor Processing of aggregates may include special processing of each object The special processing may adjust some of the attributes of each object or just verify that the attributes do not need adjustment There may be a number of such special processing requirements The Visitor is an object that would implement the processing and visit each object Frameworks and Design George Blankenship 45