250 likes | 451 Views
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:
E N D
Week 8 • Midterm discussion • Interaction / Sequence Diagrams • Design Patterns • RAII • Lab 4 Kate Gregory
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
Sample Sequence Diagram Kate Gregory
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
Sequence Diagrams Clarify Responsibilities • Invoice, Customer, and Line Item co-operate to print an invoice: Kate Gregory
Return arrows clarify meaning Kate Gregory
Show the flow of information Kate Gregory
Lifetimes can vary Kate Gregory
Design Patterns • Patterns provide a mechanism for capturing and describing commonly recurring design ideas that solve a general design problem.
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
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
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
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
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.
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
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
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
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
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
RAII in General • Constructor acquires a resource • Source of the name • Destructor frees the resource • Cannot forget • Works with exceptions • Incredibly powerful Kate Gregory
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
Next Week • Modules and Packages, Metrics • Lab 4 due • Lab 5 will be handed out Kate Gregory