您的位置:首页 > 职场人生

剑指Offer----面试题二:实现Singleton模式

2016-05-18 22:26 417 查看
转载请注明出处</article/7845923.html>

题目:设计一个类,我们只能生成该类的一个实例。

详解请参考More Effective C++ 第26款:限制某个类所能产生的对象的数量

在这里我将给出三个实例:

实例一:一个类只能产生一个对象

#include<iostream>
#include<string>

//类的声明
namespace MyStrSpace{
class MyStr{
public:
void print(){ std::cout << str << std::endl; }
friend MyStr & creatStr();//返回指向对象的引用

private:
MyStr();
MyStr(std::string p);
MyStr(const MyStr & p);

std::string str;
};
}

//类的定义
namespace MyStrSpace{
MyStr::MyStr(){};

MyStr::MyStr(std::string p)
{
str = p;
}

MyStr::MyStr(const MyStr & p)
{
str = p.str;
}

MyStr& creatStr()
{
std::string str = "Offer";
static MyStr p(str);
return p;
}
}

int main1()
{
MyStrSpace::creatStr().print();

system("pause");
return 0;
}


实例二:一个类可以产生有限个对象

#include<iostream>
#include<string>
#include<memory>

//声明
namespace MyStr2Space
{
class MyStr
{
public:
class TooManyObjects{};

//伪构造函数
static MyStr * makeMyStr();
static MyStr * makeMyStr(std::string s);
static MyStr * makeMyStr(const MyStr &p);

void print() const{ std::cout << str << std::endl; }

private:
MyStr();
MyStr(std::string s);
MyStr(const MyStr & p);

std::string str;
static size_t count;
const static size_t MAX;	//最多创建十个对象
};
}

//定义
namespace MyStr2Space
{
size_t MyStr::count = 1;
const size_t MyStr::MAX = 2;

MyStr::MyStr()
{
if (count > MAX)
throw TooManyObjects{};
count++;
str = "";
}

MyStr::MyStr(std::string s)
{
if (count > MAX)
throw TooManyObjects{};
count++;
str = s;
}

MyStr::MyStr(const MyStr & p)
{
if (count > MAX)
throw TooManyObjects{};
count++;
str = p.str;
}

MyStr * MyStr::makeMyStr()
{
return new MyStr;
}

MyStr * MyStr::makeMyStr(std::string s)
{
return new MyStr(s);
}

MyStr * MyStr::makeMyStr(const MyStr & p)
{
return new MyStr(p);
}
}

int main2()
{
MyStr2Space::MyStr * p1 = MyStr2Space::MyStr::makeMyStr();
p1->print();

std::string str = "Offer";
MyStr2Space::MyStr * p2 = MyStr2Space::MyStr::makeMyStr(str);
p2->print();

MyStr2Space::MyStr * p3 = MyStr2Space::MyStr::makeMyStr(*p2);
p3->print();

system("pause");
return 0;
}


实例三:创建基类限制对象生成个数

#include<iostream>
#include<string>

namespace baseSpace
{
template<class BeingCounted>
class Counted {
public:
class TooManyObjects{};                     // 用来抛出异常
static int objectCount() { return numObjects; }

protected:
Counted();
Counted(const Counted& rhs);
~Counted() { --numObjects; }
private:
static size_t numObjects;
static const size_t maxObjects;
void init();                                // 避免构造函数的
};                                            // 代码重复

template<class BeingCounted>
Counted<BeingCounted>::Counted()
{
init();
}
template<class BeingCounted>
Counted<BeingCounted>::Counted(const Counted<BeingCounted>&)
{
init();
}
template<class BeingCounted>
void Counted<BeingCounted>::init()
{
if (numObjects >= maxObjects) throw TooManyObjects();
++numObjects;
}

template<class BeingCounted>
size_t Counted<BeingCounted>::numObjects = 0;
}

namespace printSpace
{
class Printer : private baseSpace::Counted<Printer> {
public:
// 伪构造函数
static Printer * makePrinter();
static Printer * makePrinter(const Printer& rhs);
~Printer();
void print() const
{
std::cout << str << std::endl;
}

void set(std::string s)
{
str = s;
}

using Counted<Printer>::objectCount;
using Counted<Printer>::TooManyObjects;
private:
Printer();
Printer(const Printer& rhs);
std::string str;
};

Printer::Printer()//自动调用基类构造函数
{
str = "";
}

Printer::Printer(const Printer & rhs)
{
str = rhs.str;
}

Printer * Printer::makePrinter()
{
return new Printer();//返回类型

}

Printer * Printer::makePrinter(const Printer & rhs)
{
return new Printer(rhs);
}
}

//允许创建两个对象

const size_t baseSpace::Counted<printSpace::Printer>::maxObjects = 2;

int main()
{
printSpace::Printer *p1 = printSpace::Printer::makePrinter();
p1->print();
std::string str = "剑指Offer";
p1->set(str);
p1->print();

printSpace::Printer *p2 = printSpace::Printer::makePrinter(*p1);
p2->print();

system("pause");
return 0;
}


转载请注明出处</article/7845923.html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: