computer science: Programming Patterns
1. [10] Draw a UML class diagram for the following class definitions. class A{ }; class B{ public: virtual int myfunc(); protected: int x_; private: A* a_; }; class C: public B{ public: int myfunc(); }; 2. [15] Provide code for the instance() function for one the implementations and explain the operation of this function. 3. [10] Given following code in main() A *p = new B; p->myfunc(): Complete below class definitions such that myfunc() invoked above is a feature of class B. Explain your code. class A{ }; class B: public A{ }; 4. [15] Given the code below, provide definition of the Adapter class for the class-based Adapter design pattern:state class inheritance and provide myFunc() implementation in terms of yourFunc(), the implementation is of your choice. Provide example of client usage of your pattern (invocation of the implemented function). class TargetInterface{ public: void myFunc(); }; class Adaptee{ public: void yourFunc(); } 5. [15] Given the code below, explain the relationship between changeState in class Context and class State. Provide outside (of class) implementations of both functions. class Context{ void changeState(State *s); private: state* s_; }; class State{ void changeState(Context *c, State *s); }; 6. [10] Given the following abstract class, provide a concrete implementation (define the clone() implementation) and provide an example of use by a client. class Prototype{ Public: virtual Prototype* clone(); }; 7. [15] Write code that decorates the below ChrsitmasTree with ornaments (modifies the stored string). Give example code that invokes show() to print out the decorated tree. class ChristmasTree{ public: ChristmasTree:tree_("_\|/_"){} // initializes the tree virtual string show() const{return tree_;} private: string tree_; };