1 / 25

Week 8

Week 8. Midterm discussion Interaction / Sequence Diagrams Design Patterns RAII Lab 4. Sequence Diagrams. Also called interaction diagram and interaction sequence diagram Based on a use case instance Document the ways that objects co-operate Two dimensions:

brygid
Download Presentation

Week 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 8 • Midterm discussion • Interaction / Sequence Diagrams • Design Patterns • RAII • Lab 4 Kate Gregory

  2. Sequence Diagrams Also called interaction diagram and interaction sequence diagram Based on a use case instance Document the ways that objects co-operate Two dimensions: down represents time passing across represents different objects Kate Gregory

  3. Sample Sequence Diagram Kate Gregory

  4. Tips for Good Sequence Diagrams • Give your objects good names • especially when two objects of the same class are featured • Record the parameters and return values being sent • Refer to your use case often Kate Gregory

  5. Sequence Diagrams Clarify Responsibilities • Invoice, Customer, and Line Item co-operate to print an invoice: Kate Gregory

  6. Return arrows clarify meaning Kate Gregory

  7. Show the flow of information Kate Gregory

  8. Lifetimes can vary Kate Gregory

  9. Design Patterns • Patterns provide a mechanism for capturing and describing commonly recurring design ideas that solve a general design problem.

  10. Singleton • Classes (such as inventory or member list) must have one, and only one, object instance. • Must be able to call a method and get either the new object, or if one already exists, a pointer to it. Kate Gregory

  11. Singleton • Make the constructor of the class private • not accessible outside of the class structure • How do you create an object? • Have a public class method like Instance( ) • checks an internal class variable, _instance, of the class type. • if _instance is NULL, create new object and return it, else return _instance. Kate Gregory

  12. Singleton – Code Example Singleton* Singleton::_instance = 0; Singleton* Singleton::Instance() { if(_instance == 0) { _instance = new Singleton(); } return _instance; } class Singleton { public: static Singleton* Instance(); private: Singleton(); static Singleton* _instance; }; Kate Gregory

  13. Singleton-itis • Singleton is good in certain situations • Application settings, pools (eg db connections), inventory, etc • Almost impossible to write a thread-safe version • Can be overused • Unrecorded dependencies Kate Gregory

  14. Façade “Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.” - Design Patterns, Gamma et al.

  15. Façade • Hide the many interfaces of a detailed, complex subsystem by inserting a single interface (façade) between the high level classes and the subsystem. • Insulate the consumers of your system from all your details • Prevent other programmers from consuming your system “wrongly” Kate Gregory

  16. Façade example

  17. Composite • Perfect for unstructured hierarchies • Chains vary in length • Employee reports to another Employee, who reports to another … • A container holds items, some of which are actually containers, holding more items, which might be containers… Kate Gregory

  18. Composite Pattern

  19. RAII • Resource Acquisition is Initialization • Terrible name • C++ concept • Applicable in many other languages • Key is known lifetime of objects • And automatic cleanup Kate Gregory

  20. RAII Example • Imagine a FILE resource you can get from your operating system • Open(string filename) returns handle • Read(handle h) returns some bytes • Write(handle h, byte[] b) writes data into it • Close(handle h) closes it • You must remember to close the file Kate Gregory

  21. FileWrapper class • Constructor takes file name, calls Open • Keep handle in member variable • Read and Write are member functions • No need to pass handle • Destructor (in C++ - other names other languages) calls Close • Cannot be forgotten • Works even in the presence of exceptions Kate Gregory

  22. RAII in General • Constructor acquires a resource • Source of the name • Destructor frees the resource • Cannot forget • Works with exceptions • Incredibly powerful Kate Gregory

  23. Lab 4 • Draw a sequence diagram • One diagram • One path through one use case • Read the hints! • They are based on troubles students had in the past • Don’t be surprised if you think a LOT for a single page you hand in Kate Gregory

  24. Next Week • Modules and Packages, Metrics • Lab 4 due • Lab 5 will be handed out Kate Gregory

More Related