Skip to content

Latest commit

 

History

History
112 lines (70 loc) · 330 KB

Lecture 12.md

File metadata and controls

112 lines (70 loc) · 330 KB

Lecture 12

Strategy

1. What is the basic function (功能) of strategy pattern?

Strategy Pattern: define a family of algorithms, encapsulate each one, and make them interchangeable, which can reduce the number of classes and can reduce errors.

2. Please give an example using strategy pattern.

class Example {
public:
	Example();
	void setStrategy(Strategy* strategy_in);
private:
	Strategy* strategy;
};
class Strategy {
	//…
};
class Strategy1 : Strategy {
	//…
};

3. List some typical situations where strategy pattern is useful or necessary.

  • when algorithms of similar use have to be altered under different operating systems.
  • when different characters will use different implementations of the same movements in a game.
  • Have a family of algorithms to solve a problem and need to choose one of them easily.

4. Plot a typical UML class diagram with strategy pattern for a general situation

12-4

Template method

5. What is the basic function (功能) of template pattern?

Define skeleton (骨架) of an algorithm
Deferring (延迟) some steps to subclasses
Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure

6. Please give an example using template method design pattern.

12-6

7. List some typical situations where template pattern is useful or necessary.

  • when the same algorithm only has a little changes to different tasks. it’s often used alongside with Strategy method.
  • One algorithm is going to deal with data with different types.

8. Plot a typical UML class diagram with template pattern for a general situation.

12-8

Proxy

9. What is the basic function (功能) of proxy pattern?

  • Provide a surrogate or placeholder for another object to control access to it.
    Before and after providing access to the object, different tasks can be added for different purposes.

10. Please give an example using proxy pattern.

12-10

11. List some typical situations where proxy pattern is useful or necessary.

  • when access to certain object is need to be protected or controlled. (for example, creating expensive objects only when needed; avoid unauthorized access to certain objects.)
  • when additional functions are need to be added to original/existing objects. (for example, sending waiting messages to clients, do some extra work (like counting) when a certain object is called.)
  • When you want different perform in different situation of a given class.
  • you want to check whether the parameter is valid before send them into the given class.

12. Plot a typical UML class diagram with proxy pattern for a general situation

12-12

13. What is the main difference between proxy and adapter?

  • Proxy wraps one object to control it’s access; Adapter wraps one or more objects to adapt their interface to the user program
  • Proxy does not change interface; Adapter may change interface
  • Proxy often changes functionality; Adapter does not change functionality

Command

14. What is the basic function (功能) of command pattern?

  • It encapsulates an action as an object, which can further be easily logged and even undone.

15. How to implement Undo and Redo in command pattern?

  • use a command list to save all commands/several latest commands.
  • when undo is called, execute all commands before the latest command on an empty canvas once again.
  • when redo is called, execute the last command that has been omitted in the undo part on the canvas.