Design Patterns ECE 417/617: Elements of Software Engineering

Design Patterns ECE 417/617: Elements of Software Engineering

Design Patterns ECE 417/617: Elements of Software Engineering Stan Birchfield Clemson University What is a Design Pattern? A design pattern abstracts a recurring design structure comprises class and/or object

dependencies, structures, interactions, or conventions distills design experience Re-use Code re-use Dont reinvent the wheel Requires clean, elegant, understandable, general, stable code

leverage previous work Design re-use Dont reinvent the wheel Requires a precise understanding of common, recurring designs leverage previous work Some design patterns

Abstract factory Adapter Bridge Command Composite Faade Subject / Observer Proxy Strategy

Subject-observer [from Vlissides] Subject-observer (cont.) Subject Register(Observer) Unregister(Observer) NotifyAll() 1 * Observer

OnUpdate() for all o in observers { o.OnUpdate() } Subject-observer (cont.) Subject Register(Observer) Unregister(Observer) NotifyAll() ConcreteSubject

1 * Observer virtual OnUpdate() for all o in observers { o.OnUpdate() } ConcreteObserver virtual OnUpdate()

Model / view / controller (MVC) (displays data) View Controller Model Create() Main (mediates) }

(holds data) Register() Model { Model m; Controller c(&m); View v(&c); Create() Controller

Create() View calls Register() MVC (cont.) Subject Register(Observer) Unregister(Observer) NotifyAll() Controller

1 * Observer virtual OnUpdate() for all o in observers { o.OnUpdate() } View virtual OnUpdate()

MVC (cont.) class Observer { protected: virtual void OnUpdate(MsgId message_id) = 0; }; class Subject { public: enum MsgId {}; void RegisterObserver(Observer* obs); virtual void NotifyAllObservers(MsgId message_id) { for (int i=0 ; iOnUpdate(message_id); } } private: std::vector m_observers; }; MVC (cont.) class Controller : public Subject { Controller(Data* d) : m_data(d) {} const Data* GetData() const; void AddSphere(const Sphere& s) { m_data->AddSphere(s);

NotifyAllObservers(ADD_SPHERE); } private: Data* m_data; }; MVC (cont.) class MainWnd : public Observer { public: MainWnd(Controller* c) : m_controller(c) { c.Register(this); } virtual void OnUpdate(int message_id) {

switch (message_id) { case Subject::ADD_SPHERE: ... } } private: Controller* m_controller; }; Adapter You have legacy code current client

Adapter changes interface of legacy code so client can use it Adapter fills the gap b/w two interfaces No changes needed for either legacy code, or client Adapter (cont.) class NewTime { public: int GetTime() { return m_oldtime.get_time() * 1000 + 8; }

private: OldTime m_oldtime; }; Command You have commands that need to be executed, undone, or queued Command design pattern separates Receiver from Invoker from Commands All commands derive from Command and implement do(), undo(), and redo()

Facade You have a set of related classes want to shield the rest of the system from these details Facade provides a simplified interface Encapsulates a subsystem Composite You want uniformly to treat items (atomic elements), and groups (containing items or other groups)

Composite interface specifies operations that are shared between items and groups Examples: hierarchy of files and directories, groups of drawable elements Composite (cont.) Composite Item Group

Proxy You want to delay expensive computations, use memory only when needed, or check access before loading an object into memory Proxy has same interface as Real object stores subset of attributes does lazy evaluation Strategy You want to use different algorithms depending upon the context

avoid having to change the context or client Strategy decouples interface from implementation shields client from implementations Context is not aware which strategy is being used; Client configures the Context strategies can be substituted at runtime example: interface to wired and wireless networks Strategy (cont.) Client Policy

Context Strategy Concrete StrategyA Concrete StrategyB Bridge You have several different implementations need to choose one, possibly at run time

Bridge decouples interface from implementation shields client from implementations Abstraction creates and initializes the ConcreteImplementations Example: stub code, slow code, optimized code Bridge (cont.) Client Abstraction Implementor

Concrete ImplementorA Refined Abstraction Concrete ImplementorB Design pattern space [from Vlissides]

Recently Viewed Presentations

  • Organizational Behavior - Western Oregon University

    Organizational Behavior - Western Oregon University

    Organizational Behavior Organizational Citizenship Behavior ... Justice Input Structure Organizational Citizenship Behavior Altruism Conscientiousness Courtesy Sportsmanship Civic Virtue Determinants of OCB Big 5 Personality Traits Organizational Culture A system of shared meanings that result ...
  • CER JAI2 - cgchpf.chez.com

    CER JAI2 - cgchpf.chez.com

    Mais l'employeur ne peut pas toujours imposer ces modification. 1 er cas 2 éme cas Contacts et liens pour plus d'information S'il s'agit d'une modification essentielle (ou substancielle) du contrat de travail: La rémunération, la qualification, la fonction, la durée...
  • Life is like a Soap Opera - Sunday School Powerpoint

    Life is like a Soap Opera - Sunday School Powerpoint

    Your tongue cuts like a sharp razor; you're an expert at telling lies.3 You love evil more than good and lies more than truth. 4 You love to destroy others with your words, you liar!5 But God will strike you...
  • Sample Title Slide - Wooster Physics Snapshot

    Sample Title Slide - Wooster Physics Snapshot

    The block diagram contains this graphical source code. Front panel objects appear as terminals on the block diagram. Additionally, the block diagram contains functions and structures from built-in LabVIEW VI libraries. Wires connect each of the nodes on the block...
  • Springboard Activity 1.11 - campbell.k12.ky.us

    Springboard Activity 1.11 - campbell.k12.ky.us

    Developing Vocabulary: QHT Chart with the academic and literary vocab words. Q. H. T. Embedded Assessment #2. Creating an Illustrated Myth. Make a web including what you will need to know and add to make your myth complete. Turn to...
  • HySec Its strengths, your benefits EPDS  Indoor Apparatus

    HySec Its strengths, your benefits EPDS Indoor Apparatus

    Direct cable connection. Reliability. Efficiency. Productivity. Speed up your projects. Continuous operation. Easy to install ... Lower part of enclosure in stainless steel providing metallic segregation between cable and busbar compartments . Safety and protection. Protect your assets ...
  • Balkans Campaign - Elizabeth High School

    Balkans Campaign - Elizabeth High School

    Central powers casualties: 1.1 million Austro-Hungarian soldiers and 120,000 civilians died in the war. 87,000 Bulgarian soldiers lost their life and 150,000 left wounded. Germany lost 2.2 million soldiers and 4.2 million were wounded, but only about 1,000 german civilians...
  • Anatomy of the Nervous System - Buena Vista University

    Anatomy of the Nervous System - Buena Vista University

    Anatomy of the Nervous System ... within the spinal column The spinal cord communicates with the sense organs and muscles below the level of the head Bell-Magendie Law-the entering dorsal roots carry sensory information and the exiting ventral roots carry...