More Related Content


Recently uploaded(20)


Software Design Patterns

  2. Introduction Often wondered, on large/complex projects if we were to change a small part, wouldn’t be tedious to change the entire code inorder to get the required results? Well, for such situations we have an adopted concept which is “Software Design Patterns”. When described in terms of software engineering, a design pattern is a general reusable solution for a commonly occurring problem within a given context of a software design.
  3. Object-oriented design patterns showcases the relationships and interactions between classes/objects, without specifying the final application classes or objects that are involved. Software Design Patterns is viewed as a structured approach for programming intermediate between the levels of a programming paradigm and a concrete algorithm.
  4. Categories Design patterns can be categorised into three fundamental groups, as follows:- 1. Structural Patterns 2. Creational Patterns 3. Behavioral Patterns Let's see in detail each of the patterns in the next slides.
  5. Structural Patterns • In Structural patterns, larger structures are formed from individual parts, which are generally of different classes. • The main concern associated with these patterns is with how classes and objects are composed to form larger structures. • In these patterns, inheritance is used to compose interfaces or implementations.
  6. • Consider a simple example, like how multiple inheritance mixes together two or more classes into one. The result would be a class that combines the properties of its parent classes. • The usefulness of this pattern is for making independently developed class libraries work together.
  7. Methods in Structural Patterns • Adapter: An adapter allows classes to work together which could not otherwise because of incompatible interfaces. • Bridge: Seprates or decouples an abstraction from its implementation allowing them to vary independently. • Composite: Composite divides objects into tree structures inorder to represent part-whole hierarchies.
  8. • Decorator: Adds additional responsibilities for an object dynamically while keeping the same interface. • Facade: Facade specifies a high-level interface that makes the subsystem easier to use. • Flyweight: Sharing is used to support large numbers of similar objects efficiently and effectively.
  9. • Front Controller: Usually related to the design of Web applications. • Module: Several related elements, like classes, singletons, methods, globally used, are grouped into a single conceptual entity. • Proxy: Provides a placeholder for a different object in order to control access to it.
  10. Creational Patterns • Creational patterns are generally used to create objects for suitable classes that serve as a solution for a problem,usually when instances of several different classes are available. • These patterns are particularly useful while taking advantage of polymorphism and also when there is a need to choose between different classes at run-time rather than compile time.
  11. Methods in Creational Patterns • Abstract Factory: This provides an interface for creating families of dependent objects without specifying their concrete classes. • Builder: The construction of a complex object from its representation, allowing the same construction process to create various representations. • Multiton: Makes sure a class has only named instances.
  12. • Factory Method: Defines an interface to create a single object, but also lets subclasses decide which classes to instantiate. • Lazy initialization: This is a tactic of delaying the creation of an object • Object pool: Recycles objects that are no longer in use.
  13. • Prototype: Specifies the objects for creating a prototypical instance, and also to create new objects by copying this prototype. • Singleton: Makes sure a class only has one instance, and thence provides a global point of access to it. • Design pattern object library: Wraps up object management together with factory interface including live and dead lists.
  14. Behavioral Patterns • These patterns usually deals with interactions between objects and focuses on how objects communicates with each other. • They can reduce the complex flow charts into mere interconnections between objects of various classes. • Behavioral patterns are also used to make the algorithm that a class uses simply another parameter that is adjustable at run-time.
  15. Methods in Behavioral Patterns • Chain of responsibility: Avoids coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. • Command: An object is encapsulated as a request. • Interpreter: Represents interpreted sentences into the corresponding language. • Iterator: Provides a solution to access the elements of an object sequentially,without exposing its underlying representation.
  16. • Mediator: Sets an object that encapsulates and describes how a set of objects interact. • Memento: This method allows to capture and externalize an object's internal state and also allowing it to be restored to this state later. • Null object: Default object is provided by avoiding null references. • Observer: Defines a one-to-many dependency between objects, where a state change in one of the object results in all its dependent objects being notified and updated automatically.
  17. • Servant: Defines the similar functionality for a group of classes. • Specification: Combines business logic within a boolean representation. • State: Allowing the objects to alter their behavior simultaneosly when its internal state changes. • Strategy: Sets a family of algorithms, encapsulate each one, and makes them interchangeable.
  18. • Template method: Skeleton of an algorithm is defined in an operation. • Visitor: Representing an operation to be performed on the elements of an object structure.
  19. Practice / Example A simple example involves using the Factory Method of the Creational Patterns. //Gallery.cfc interface { public any function getPhotos(); } An interface is used with a declaration of getPhotos() function.
  20. //UserGallery.cfc component implements="Gallery" { public any function getPhotos() { qry=new Query(sql="select * from usergallery",datasource="dk"); return qry.execute().getResult(); } } This file has a component which implements the above interface Gallery, and also defines the function getPhotos(). Within the function various data are selected from the usergallery table.
  21. //PicasaGallery.cfc component interface="Gallery" { public any function getPhotos() { qry=new Query("Select name from PicasaGallery",datasource="dk"); return qry.execute().getResult(); } } Within this file, again Gallery interface has been implemented together with the function getPhotos() definition. In this function the name is obtained from the PicasaGallery table.
  22. //AdminGallery.cfc component interface="Gallery" { public any function getPhotos() { qry=new Query("Select name from AdminGallery",datasource="dk"); return qry.execute().getResult(); } } In this file, the Gallery interface has also been implemented, and the definition of the getPhotos() function includes getting the name from AdminGallery table.
  23. //FactoryGallery.cfc component { public any function getGallery(name) { gallery=""; if(name=="User") { gallery=createObject("component", "Design_Patterns.UserGallery"); } else if(name=="Picasa") { gallery=createObject("component", "Design_Patterns.PicasaGallery"); } (...contd)
  24. else if(name=="Admin"){ gallery=createObject("component", "Design_Patterns.AdminGallery"); } return gallery; } } FactoryGallery.cfc creates a getGallery() function which is used to create objects of components UserGallery, PicassaGallery, and AdminGallery.
  25. //MyGallery.cfm <cfscript> my_gallery=createObject("component", "Design_Patterns.FactoryGallery"); my_photos=my_gallery.getGallery("User").getPhotos(); writeDump(my_photos); </cfscript> The final results can be viewed in this above file. The object of FactoryGallery is created and with it the getPhotos() function is accessed, and the results are dumped to view the results.
  26. So in short...... • If we want add additional galleries, we only need to do 2 things: – Create a component and implement it in the Gallery interface. – Add the new created component into the FactoryGallery.cfc, within the getGallery() function. • The advantage of using this factory method is that, changes are only made in two places: FactoryGallery and MyGallery. Usually the exact use is visible for complex projects.