⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 proxy_lab.cpp

📁 设计模式
💻 CPP
字号:
// Purpose.  Proxy design pattern lab (reference counting)// // Discussion.  One of the four common situations in which the Proxy pattern is// applicable is as a "smart reference" - a replacement for a bare pointer that// performs additional actions when an object is referenced.  Here, that addi-// tional action is "reference counting".  The original String class becomes// the inner "body" class.  A "count" data member is added to the body class.// A new outer "handle" class is created that contains a pointer to a body// instance.  Body instances common to multiple handle instances are shared by// simply incrementing the reference count.  As these shared references go away,// the reference count is decremented.  When the count goes to zero, the body// instance is freed.  This mechanism allows the "reference counted class" to// perform simple-minded garbage collection on itself.// // Assignment.// o Change the class String to an inner body StringRep.// o Add a count_ private data member to StringRep.  Initialize count_ to 1 in//   the default ctor and the 1-arg ctor.// o Create a new outer handle class String. Make String a friend of StringRep.// o String has one private data member - a pointer to a StringRep (rep_).// o The String default ctor initializes rep_ with 'new StringRep("")'.// o The String 1-arg ctor initializes rep_ with 'new StringRep(arg)'.// o The String copy ctor initializes rep_ with the rep_ of the initializing//   String.  It also increments the count_ of that rep_.// o The String dtor decrements the count_ of rep_ and deletes rep_ if count_//   is 0.
// o The String operator=(): increments the count_ of the "rhs" rep_,
//   decrements the count_ of its own rep_ and deletes rep_ if count_ is 0,
//   assigns the "rhs" rep_ to its own rep_, and returns *this.
// o Add cout statements to String to generate the target output.
// o Make the operator<<() function a friend of both StringRep and String.//   Modify it as necessary.#include <iostream.h>#include <string.h>
class String {
public:
	friend ostream& operator << ( ostream&, String& );
	String() { 
		rep = new StringRep(""); 
        cout << "     String ctor (def):" << endl;
	}
	String(const char * in) { 
		rep = new StringRep( in ) ; 
		cout << "     String ctor: " << in << '.' << endl;
	}
    String(const String& s)  { 
		rep = s.rep;
		s.rep->count++;
		cout << "     String (copy): " << s.rep->str_ << '.' << endl;
	}
	String& operator= ( String& rhs ) {
		if (this == &rhs)  return *this;
		rhs.rep->count++;
		rep->count--;
		if(0 == rep->count ) { if( rep ) delete rep; rep = NULL; }
		rep = rhs.rep;
		return *this;
	}
	~String() {
		rep->count--;
		if(rep->count + 1){
			cout << "      String dtor: " << rep->str_ << ", before decrement, count is " 
				<< rep->count + 1 << endl;			
		}
		if(!rep->count ) { if( rep ) delete rep;}
	}
	
	class StringRep {
	public:
		friend class String;
		//		friend ostream& operator << ( ostream&, String& );
		StringRep() {
			cout << "   StringRep ctor (def):" << endl;
			str_ = NULL;
			count = 1;
		}
		StringRep( const char* in ) {
			cout << "   StringRep ctor: " << in << '.' << endl;
			str_ = new char[strlen(in) + 1];
			strcpy( str_, in );
			count = 1;
		}
		StringRep( StringRep& str ) {
			cout << "   StringRep ctor (copy): " << str.str_ << '.' << endl;
			str_ = new char[strlen(str.str_) + 1];
			strcpy( str_, str.str_ );
			count = 1;
		}
		char *getString()    { return str_; }
		~StringRep() {
			cout << "   StringRep dtor: " << str_ << '.' << endl;
			delete str_;
		}
		StringRep& operator= ( StringRep& rhs ) {
			if (this == &rhs)  return *this;
			delete str_;
			str_ = new char[strlen(rhs.str_) + 1];
			strcpy( str_, rhs.str_ );
			return *this;
		}
	private:
		char*  str_;
		int count;
	};
	private:
		StringRep *rep;
};ostream& operator << ( ostream& os, String& str ) { return os << str.rep->getString(); }void main( void ) {	String  a( "hello" );
	String  b = "world";
	String  c( a );
	String  d = a;
	String  e;
	a = b;
	e = b;	cout << "a is " << a << '.' << endl;	cout << "b is " << b << '.' << endl;	cout << "c is " << c << '.' << endl;	cout << "d is " << d << '.' << endl;	cout << "e is " << e << '.' << endl;}/****** Current output ******///    String ctor: hello.//    String ctor: world.//    String ctor (copy): hello.//    String ctor (copy): hello.//    String ctor (def):// a is world.// b is world.// c is hello.// d is hello.// e is world.//    String dtor: world.//    String dtor: hello.//    String dtor: hello.//    String dtor: world.//    String dtor: world./****** Target output ******///    StringRep ctor: hello.// String ctor: hello.//    StringRep ctor: world.// String ctor: world.// String ctor (copy): hello.// String ctor (copy): hello.//    StringRep ctor: .// String ctor (def)://    StringRep dtor: .// a is world.// b is world.// c is hello.// d is hello.// e is world.// String dtor: world, before decrement, count is 3// String dtor: hello, before decrement, count is 2// String dtor: hello, before decrement, count is 1//    StringRep dtor: hello.// String dtor: world, before decrement, count is 2// String dtor: world, before decrement, count is 1//    StringRep dtor: world.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -