Understanding Design Patterns for Software Development

Slide Note
Embed
Share

Explore the world of design patterns in software development, including structural decomposition, organization of work, access control, and communication patterns. Learn the differences between design and architectural patterns, and how they influence software systems. Discover categories of design patterns and their applications in creating efficient and scalable software solutions.


Uploaded on Sep 14, 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. Engineered for Tomorrow Unit- 7 SOME DESIGN PATTERNS Engineered for Tomorrow Presented by Sushma Narasimhan Asst. Professor, Computer Science Department

  2. Engineered for Tomorrow Engineered for Tomorrow Unit- VII Structural Decomposition Whole-Part Organization of work Master-Slave Access Control Proxy

  3. Engineered for Tomorrow Definition A design pattern describes a commonly-recurring structure of communicating components that solve a general design problem in a particular context. Some of the design patterns are: Whole-Part Master- Slave Proxy Command Processor View Handler Forwarder-Receiver Client-Dispatcher-Server Publisher-Subscriber

  4. Engineered for Tomorrow Contd.. Q:What is the difference between Design pattern & Architectural pattern? A: Design patterns are medium-scale patterns. They are smaller in scale than architectural patterns, but are at a higher level than the programming language-specific idioms. The application of a design pattern has no effect on the fundamental structure of a software system. But may have a strong influence on the architecture of a subsystem.

  5. Engineered for Tomorrow Contd.. Categories of Design patterns i) Structural Decomposition - This category includes patterns that support a suitable decomposition of subsystems & complex components into co-operating parts. Example: Whole-Part pattern ii) Organization of Work - This category comprises patterns that define how components collaborate together to solve a complex problem. Example: Master-Slave pattern iii) Access Control - Consists of patterns that guard and control access to services or components. Example: Proxy pattern

  6. Engineered for Tomorrow Contd.. iv) Management - This category includes patterns for handling homogenous collections of objects, services and components in their entirety. Example: Command Processor pattern, View Handler pattern v) Communication - Patterns in this category help to organize communication between components. Example: Forwarder-Receiver pattern, Client- Dispatcher- Server pattern, Publisher-Subscriber pattern

  7. Engineered for Tomorrow Engineered for Tomorrow Unit- VII Structural Decomposition Whole-Part Organization of work Master-Slave Access Control Proxy

  8. Engineered for Tomorrow Structural De-composition Need for Structural De-composition Sub-systems and complex components are handled more easily if structured into smaller independent components, rather than remaining as monolithic blocks of code. Changes are easier to perform. Extensions are easier to integrate and design is much easier to understand. Patterns Here 2 design patterns come under this category: Whole-Part design pattern Composite design pattern

  9. Engineered for Tomorrow Engineered for Tomorrow Unit- VII Structural Decomposition Whole-Part Organization of work Master-Slave Access Control Proxy

  10. Engineered for Tomorrow Engineered for Tomorrow Whole-Part Pattern Pattern Definition Example Resolved Context Problem Solution Structure Dynamics Implementation Variants Known Uses Consequences

  11. Engineered for Tomorrow Pattern Definition The Whole-Part design pattern helps with the aggregation of components that together form a semantic unit. An aggregate component, the Whole , encapsulates its constituent components, the Parts, organizes their collaboration and provides a common interface to its functionality. Direct access to the Parts is not possible.

  12. Engineered for Tomorrow Example A computer-aided design (CAD) system for 2-D and 3-D modeling allows engineers to design graphical objects interactively. In such systems, most graphical objects are modeled as compositions of other objects. For example. a car object aggregates several smaller objects such as wheels and windows, which themselves may be composed of even smaller objects such as circles and polygons It is the responsibility of the car object to implement functionality that operates on the car as a whole, such as rotating or drawing.

  13. Engineered for Tomorrow Contd..

  14. Engineered for Tomorrow Context Implementing aggregate objects

  15. Engineered for Tomorrow Problem In almost every software system objects that are composed of other objects exist. For example, consider a molecule object in a chemical simulation system. In this example, a molecule object would have attributes such as its chemical properties, and methods, such as rotation. These attributes and methods refer to the molecule as a semantic unit, and not to the individual atoms of which it is composed. The combination of parts makes new behavior emerge known as emergent behavior.

  16. Engineered for Tomorrow Contd.. Need to balance the following forces when modeling such structures: i) A complex object should either be decomposed into smaller objects,or composed of existing objects, to support reusability, changeability & recombination of the constituent objects in other types of aggregate. ii) Clients should see the aggregate object as an atomic object that does not allow any direct access to its constituent parts.

  17. Engineered for Tomorrow Solution Introduce a component that encapsulates smaller objects & prevents clients from accessing these constituent parts directly. Define an interface for the aggregate that is the only means of access to the functionality of the encapsulated objects. Allow the aggregate to appear as a semantic unit.

  18. Engineered for Tomorrow Contd.. The general principle of the Whole-Part pattern is applicable to the organization of three types of relationship: i) An assembly-parts relationship - which differentiates between a product and its parts or sub-assemblies. ii) A container-contents relationship - the aggregated object represents a container. iii)The collection-members relationship - groups similar objects -such as an organization and its members.

  19. Engineered for Tomorrow Structure The Whole-Part pattern introduces two types of participant: A Whole object represents an aggregation of smaller objects, which we call Parts. It forms a semantic grouping of its Parts in that it co-ordinates and organizes their collaboration. Some methods of the Whole are defined for specific Part services. When such a method is invoked the Whole only calls the relevant Part service, and returns the result to the client. The Whole may implement complex strategies that build on several smaller services offered by Parts.

  20. Engineered for Tomorrow Contd.. Example: Consider zooming a group of 2-D objects. To complete the execution of the zoom method, the group object invokes the zoom operations of all its Parts. The Whole may additionally provide functionality that does not invoke any Part service at all. Example: Set objects offer functions like getSize ( ) for returning the current number of contained elements. Only the services of the Whole are visible to external clients. The Whole also acts as a wrapper around its constituent Parts and protects them from unauthorized access.

  21. Engineered for Tomorrow Contd..

  22. Engineered for Tomorrow Contd.. Fig: Static relationship between a Whole & its Parts

  23. Engineered for Tomorrow Dynamics Scenario Consider the two-dimensional rotation of a line within a CAD system. The line acts as a Whole object that contains two points p and q as Parts. A client asks the line object to rotate around the point c and passes the rotation angle as an argument. Since the rotation of a line can be based on the rotation of single points, it is sufficient for the line object to call the rotate methods of its endpoints. After rotation, the line redraws itself on the screen.

  24. Engineered for Tomorrow Contd.. The rotation of a point p around a center c with an angle a can be calculated using the following formula: Fig: rotation of the line given by the points p and q

  25. Engineered for Tomorrow Contd.. The scenario consists of four phases: A client invokes the rotate method of the line L and passes the angle a and the rotation center c as arguments. The line L calls the rotate method of the point p. The line L calls the rotate method of the point q. The line L redraws itself using the new positions of p and q asendpoints.

  26. Engineered for Tomorrow Contd.. Fig: Scenario

  27. Engineered for Tomorrow Implementation To implement a Whole-Part structure, apply the following steps: i) Design the public interface of the Whole Analyze the functionality the Whole must offer to its clients. Think of the Whole as an atomic component that is not structured into Parts. ii) Separate the Whole into Parts, or synthesize it from existing ones. There are two approaches for assembling the Parts : The bottom-up approach composes Wholes from loosely-coupled Parts that can be later reused. The top-down approach makes it is possible to cover all of the Whole's functionality.

  28. Engineered for Tomorrow Contd.. iii) If bottom-up approach is used, use existing Parts from component libraries or class libraries and specify their collaboration. iv) If top-down approach is used, partition the Whole's services into smaller collaborating services & and map these collaborating services to separate Parts. Example: In the Forwarder-Receiver design pattern, a forwarder component is decomposed into two Parts: one responsible for marshaling and another responsible for message delivery

  29. Engineered for Tomorrow Contd.. v) Specify the services of the Whole in terms of services of the Parts. There are two possible ways to call a Part service: If a client request is forwarded to a Part service, the Part does not use any knowledge about the execution context of the Whole. It relies on its own environment instead. A delegation approach requires the Whole to pass its own context information to the Part. Decide whether all Part services are called only by their Whole, or if Parts may also call each other.

  30. Engineered for Tomorrow Contd.. vi) Implement the Parts If the Parts are Whole-Part structures themselves, design them recursively starting with step 1. If not, reuse existing Parts from a library, or just implement them. vii) Implement the Whole. Implement the Whole's services based on the structure developed in the preceding steps. Implement services that depend on Part objects by invoking their services from the Whole.

  31. Engineered for Tomorrow Variants of Whole Pattern i) Shared Parts This variant relaxes the restriction that each Part must be associated with exactly one Whole by allowing several Wholes to share the same Part. The life-span of a shared Part is then decoupled from that of its Whole. ii) Assembly-Parts In this variant, the Whole may be an object that represents an assembly of smaller objects. Example: a CAD representation of a car might be assembled from wheels, windows, body panels and so on

  32. Engineered for Tomorrow Contd.. iii) Container-Contents In this variant a container is responsible for maintaining differing contents. Example: an electronic mail message may contain a header, the message body, and optional attachments. iv) Collection-Members The Part objects all have the same type. Parts are usually not coupled to or dependent on each other. This variant is used, when implementing collections such as sets, lists, maps, and arrays.

  33. Engineered for Tomorrow Contd.. v) Composite Applicable to Whole-Part hierarchies in which the Wholes and their Parts can be treated uniformly. Both implement the same abstract interface.

  34. Engineered for Tomorrow Uses/Applications i) Object-oriented applications The key abstractions of many object-oriented applications follow the Whole-Part pattern. Example: some graphical editors support the combination of different types of data to form multimedia documents. These are often implemented according to the Composite design pattern. ii) Object-oriented class libraries Provide collection classes such as lists. sets. and maps. These classes implement the Collection- Member and Container-Contents variants.

  35. Engineered for Tomorrow Contd.. iii) Graphical user interface toolkits Example: Fresco or ET++ use the Composite variant of the Whole-Part pattern.

  36. Engineered for Tomorrow Consequences Benefits of the Whole-Part pattern : i) Changeability of Parts The Whole encapsulates the Parts and thus conceals them from its clients. This makes it possible to modify the internal structure of the Whole without any impact on clients. ii) Separation of concerns Each concern is implemented by a separate Part. Therefore becomes easier to implement complex strategies by composing them from simpler services than to implement them as monolithic units.

  37. Engineered for Tomorrow Contd.. iii) Re-usability The Whole-Part pattern supports two aspects of reusability. Firstly, Parts of a Whole can be reused in other aggregate objects. Secondly, the encapsulation of Parts within a Whole prevents a client from 'scattering' the use of Part objects all over its source code

  38. Engineered for Tomorrow Contd.. Liabilities of the Whole-Part pattern i) Lower efficiency through indirection Since the Whole builds a wrapper around its Parts, it introduces an additional level of indirection between a client request and the Part that fulfils it. This may cause additional run-time overhead compared with monolithic structures. ii) Complexity of decomposition into Parts An appropriate composition of a Whole from different Parts is often hard to find, especially when a bottom-up approach is applied.

  39. Engineered for Tomorrow Engineered for Tomorrow Unit- VII Structural Decomposition Whole-Part Organization of work Master-Slave Access Control Proxy

  40. Engineered for Tomorrow Organization of work The implementation of complex services is often solved by several components in cooperation. To organize work optimally within such structures, several aspects need to be considered. Example: Each component should have a clearly-defined responsibility. The basic strategy for providing the service should not be spread over many different components.

  41. Engineered for Tomorrow Engineered for Tomorrow Unit- VII Structural Decomposition Whole-Part Organization of work Master-Slave Access Control Proxy

  42. Engineered for Tomorrow Engineered for Tomorrow Master-Slave Pattern Pattern Definition Example Resolved Context Problem Solution Structure Dynamics Implementation Variants Known Uses Consequences

  43. Engineered for Tomorrow Master-Slave Definition Supports computational accuracy. A master component distributes work to identical slave components and computes a final result from the results these slaves return. fault tolerance, parallel computation and

  44. Engineered for Tomorrow Example Consider the well-known traveling-salesman problem in graph theory. The task is to find an optimal round trip between a given set of locations, such as the shortest trip that visits each location exactly once. The solution to this problem is of high computational complexity. Generally, the solution to the traveling-salesman problem with n locations is the best of (n-l)! possible routes.

  45. Engineered for Tomorrow Contd.. Most existing implementations of the traveling-salesman problem approximate the optimal solution by only comparing a fixed number of routes. One of the simplest approaches is to select routes to compare at random, and hope that the best route found approximates the optimal route sufficiently.

  46. Engineered for Tomorrow Contd.. There are approximately 6.0828 * 1062 different trips that connect the state capitals of the United States.

  47. Engineered for Tomorrow Context Partitioning work into semantically-identical sub-tasks.

  48. Engineered for Tomorrow Problem Divide and conquer is a common principle for solving many kinds of problems. Work is partitioned into several equal sub-tasks that are processed independently. The result of the whole calculation is computed from the results provided by each partial process. Several forces arise when implementing such a structure: i) Clients should not be aware, that the calculation is based on the 'divide and conquer' principle.

  49. Engineered for Tomorrow Contd.. ii)Neither clients nor the processing of sub-tasks should depend on the algorithms for partitioning work and assembling the final result. iii) It can be helpful to use different but semantically- identical implementations for processing sub-tasks. Example : to increase computational accuracy iv)Processing of sub-tasks sometimes needs co-ordination Example: simulation element method applications using the finite

  50. Engineered for Tomorrow Solution Introduce a co-ordination instance between clients of the service and the processing of individual sub-tasks. A master component divides work into equal sub-tasks delegates these sub-tasks to several independent but semantically-identical slave components and computes a final result from the partial results the slaves return

Related