Chapter 27: Proxy Design Pattern PDF

Summary

This document is a chapter from a computer science textbook. It details the proxy design pattern and its use in C++, alongside examples and implementation details. Topics include programming preliminaries and different versions of message server examples.

Full Transcript

Department of Computer Science Tackling Design Patterns Chapter 27: Proxy Design Pattern Copyright c 2016 by Linda Marshall and Vreda Pieterse. All rights reserved. Contents 26.1 Introduction....................................

Department of Computer Science Tackling Design Patterns Chapter 27: Proxy Design Pattern Copyright c 2016 by Linda Marshall and Vreda Pieterse. All rights reserved. Contents 26.1 Introduction................................. 2 26.2 Programming Preliminaries....................... 2 26.3 Proxy Design Pattern........................... 3 26.3.1 Identification................................. 3 26.3.2 Problem................................... 3 26.3.3 Structure................................... 3 26.3.4 Participants................................. 4 26.4 Proxy Pattern Explained......................... 4 26.4.1 Related Patterns............................... 5 26.5 Example.................................... 6 26.5.1 Message Server Example - Version 1.................... 6 26.5.2 Message Server Example - Version 2.................... 7 References....................................... 9 1 27.1 Introduction This lecture note introduces the Proxy design pattern. One of the most prevalent im- plementations of the proxy pattern in C++ is in the implementation of smart pointers. Therefore an overview of smart pointers is presented in the programming preliminaries section before introducing the pattern. Other than using the proxy to implement smart pointers, three other implementation strategies are presented. 27.2 Programming Preliminaries One of the uses of the Proxy design pattern is to manage smart references. In C++ there has been the notion of a Smart Pointer. As from C++11 this notion has been refined. A C++ smart pointer is an abstract data type that simulates a pointer while providing additional features intended to reduce bugs caused by the misuse of pointers while re- taining efficiency. Smart pointers are used to keep track of the objects they point to for the purpose of memory management. This includes bounds checking and automatic garbage collection. Many libraries exist that implement a type of smart pointer. The library under consid- eration in this section is the STL for C++11. The standard for C++11 was accepted in August 2011 and is being implemented in C++ compilers. In this standard, there are 3 kinds of smart pointers, unique ptr, share ptr and weak ptr. unique ptr can be re- placed with auto ptr as defined in the C++ standard prior to C++11. As with auto ptr, it is defined in the header. The latter two are based on the implementation of smart pointers in the Boost library. auto ptr - auto ptr is not implemented in C++11. It is however discussed here for the sake of completeness. Up till C++11 it was the only smart pointer implementation available in C++. In the way auto ptr is implemented, the copy constructor and assignment operators do not copy the stored pointer. They copy the pointer, leaving the copied or assigned object empty. This strategy effectively transfers ownership of the pointer. It however does not provide a solution when copy semantics are required. a u t o p t r v a l u e (new int ( 1 0 ) ) ; cout

Use Quizgecko on...
Browser
Browser