design patterns n.
Skip this Video
Loading SlideShow in 5 Seconds..
Design Patterns PowerPoint Presentation
Download Presentation
Design Patterns

Design Patterns

172 Views Download Presentation
Download Presentation

Design Patterns

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Design Patterns

  2. Adapter Pattern According to the Gang of Four, the intent of the Adapter pattern is to: Convert the interface of a class into another interface that the clients expect. Adapter lets classes work together that could not otherwise because of incompatible interfaces. Intent: create a new interface

  3. Example • Create classes for points, lines, and squares that have the behavior "display." • The client objects should not have to know whether they actually have a point, a line, or a square. They just want to know that they have one of these shapes. free the client object from knowing details

  4. Another Situation ... • You have wanted to use a subroutine or a method that some one else has written because it performs some function that you need. • You cannot incorporate the routine directly into your program. • The interface or the way of calling the code is not exactly equivalent to the way that its related objects need to use it.

  5. Coming back to our shapes example … • We want the client objects to think we have only shapes. • allows the client objects to deal with all these objects in the same way. • enables to add different kinds of shapes in the future without having to change the clients

  6. How to do this … Use derived classes polymorphically define the interface and then implement in derived classes • Specify behaviour of the Shapes • Set a shape's location. • Get a Shape's location. • Display a Shape. • Fill a Shape. • Set the color of a Shape. • Undisplay a Shape

  7. Adapter Pattern What if you want to add a new shape Circle? Write a new derived class and write all relevant methods

  8. What if I have a relevant class? • Somebody wrote easyCircles with the following methods: • displayIt • fillIt • undisplayIt • Cannot use easyCircles directly • Different names and parameter list • Cannot derive it • Adapt it

  9. How to implement? • Derive a new class Circle from Shapes • Circle contains easyCircles. • Circle passes requests made to the Circle object on through to the easyCircles object.

  10. Adapter Pattern

  11. Adapter Pattern class Circle extends Shape { private easyCircle pxc; public Circle () { pxc= new easyCircle(); } void public display() { pxc.displayIt(); } }

  12. Adapter Pattern: Key Features

  13. Adapter Pattern: Key Features

  14. Adapter vs Facade A Client object using another, pre-existing object with the wrong interface.

  15. Strategy Pattern

  16. Strategy Pattern Several software projects tend to ignore long-term issues such as ease of maintenance or ability to change

  17. Design for Change • "Program to an interface, not an implementation.“ • "Favor object [aggregation] over class inheritance." • "Consider what should be variable in your design. This approach is the opposite of focusing on the cause of redesign. Instead of considering what might force a change to a design, consider what you want to be able to change without redesign. The focus here is on encapsulating the concept that varies, a theme of many design patterns."

  18. E-Commerce Example • We consider an order-processing system for an international e-commerce company in the United States. • This system must be able to process sales orders in many different countries.

  19. System Features • The functions of SalesOrder include the following: • Allow for filling out the order with a GUI • Handle tax calculations • Process the order, and print a sales receipt

  20. New Requirements New requirements for taxation rules • Copy and paste • Switches or ifs on a variable specifying the case we have • Inheritance (make a derived class that does it the new way)

  21. What we should do? • Based on the guiding principles: • Find what varies and encapsulate it in a class of its own. • Contain this class in another class (favor aggregation).

  22. public class TaskController { public void process () { /* this code is an emulation of a processing task controller … figure out which country you are in*/ CalcTax myTax; myTax= getTaxRulesForCountry(); SalesOrder mySO= new SalesOrder(); mySO.process(myTax); } private CalcTax getTaxRulesForCountry() { /* In real life, get the tax rules based on country you are in. You may have the logic here or you may have it in a configuration file. Here, just return a USTax so this will compile. */ return new USTax(); } }

  23. public class SalesOrder { public void process (CalcTax taxToUse) { long itemNumber= 0; double price= 0; /* given the tax object to use. . calculate tax */ double tax= taxToUse.taxAmount( itemNumber, price); } }

  24. public abstract class CalcTax { • abstract public double taxAmount( long itemSold, double price); • } • public class CanTax extends CalcTax { • public double taxAmount( long itemSold, double price) • { • /* in real life, figure out tax according to the rules in Canada and return it here, return 0 so this will compile*/ • return 0.0; • } • }

  25. Advantages • Improves cohesion, aids flexibility • Easier to shift responsibilities

  26. Strategy Pattern • Intent: • Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. • Motivation • The Strategy pattern is based on a few principles: • Objects have responsibilities. • Different, specific implementations of these responsibilities are manifested through the use of polymorphism. • There is a need to manage several different implementations of what is, conceptually, the same algorithm.

  27. General Strategy Pattern

  28. Observer Pattern

  29. e-Commerce Application New requirement: Take actions for new customers • Send a welcome e-mail to the customer. • Verify the customer's address with the post office.

  30. If we knew in advance .. • Hard Code the behaviour The problem? Requirements always change

  31. The Observer Pattern Intent • the intent of the Observer pattern is to "define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. [GOF] Simply stated: • handling notification automatically

  32. A Common Pattern It also goes by the names Dependents and Publish-Subscribe Where you can find them • the notify process in COM • It is implemented in Java with the Observer interface and the Observable class • In rule-based, expert systems, they are often implemented with daemon rules.

  33. Applying Observable Pattern • Two things are varying • Different kinds of objects There is a list of objects that need to be notified of a change in state. These objects tend to belong to different classes. • Different interfaces Because they belong to different classes, they tend to have different interfaces.

  34. 1. Make the Observers behave in the same way 2. Have the observers register themselves • All observers have the same interface • In C# and Java implement this with an interface • In C++, use inheritance • Define two methods in the Subject • attach(Observer) adds the given observer to its list of observers • detach(Observer) removes the given observer from its list of observers 3. Notify the observers when the event occurs 4. Get the information from the subject • Each Observer implements a method called update(). • Subject implements a notify() method that goes through its list of Observers and calls this update() method for each of them • The update() method should contain the code to handle the event. You must also add method(s) to the Subject that allow the Observers to get whatever information they need

  35. Back to e-Commerce Example

  36. How things work • The Observers attach themselves to the Customer class when they are instantiated. • When a new Customer is added, the notify method calls these Observers. • Each Observer calls getState for information on the newly added Customer to see what it needs to do. Note: Typically, there would be several methods to get the needed information.

  37. Customer Class public class Customer { static private Vector myObs; static { myObs= new Vector(); } public static void attach(MyObserver o){ myObs.addElement(o); } public static void detach(MyObserver o){ myObs.remove(o); } public String getState () { /* have other methods that will give the required information Here, just return null so this will compile*/ return null; } public void notifyObs () { /*set arg to something that helps tell the Observers what happened */ for (Enumeration e = myObs.elements(); e.hasMoreElements() ;) { ((MyObserver) e).update(this); } } }

  38. interface MyObserver { void update (Customer myCust); } class AddrVerification implements MyObserver { public AddrVerification () { } public void update ( Customer myCust) { /* do Address verification stuff here can get more information about customer in question by using myCust*/ } } //end of class AddrVerification

  39. class WelcomeLetter implements MyObserver { public WelcomeLetter () { } public void update (Customer myCust) { /*do Welcome Letter stuff // here can get more information about customer // in question by using myCust */ } } //end of class WelcomeLetter

  40. Add new functionality …

  41. Key Features