C++:单例模式例题解析

C++:单例模式例题解析

标签:单例模式 队列

by 小威威


这篇博文主要是讲解例题,如要深入了解单例模式,可以参考这篇文章:C++设计模式——单例模式,我个人觉得写得不错。

单例模式是C++设计模式中的一种,它保证一个类只有一个实例,并提供一个访问它的全局访问点。因为类对象的生成是通过构造函数实现的,为了保证不被随便调用并保存唯一实例,应将构造函数放到private标签内。单例模式还需要提供一个访问它的全局访问点,不难想到要在类中定义一个静态指针来指向唯一实例。

下面就上例题:(Author: 黎洋)
In this assignment, you are required to finish two classes’ definition according to their declaration respectly.
1. Two basic concepts are needed before you begin to code: 1. Singleton, 2. linked Queue.

SINGLETON: Singleton is a kind of frequently-used Design Pattern(but often be prone to misuse).
Ensure a class only has one instance, and provide a global point of access to it.
Use the Singleton pattern when
● there must be exactly one instance of a class, and it must be accessible to clients from a wellknown access point.
● when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

2. Class Job has:
(1)one static variable “number”, which indicates current object’s id. you shold increment it when you create an new object. Attention: when the object was destoyed, this variable do not subtract by 1;
(2)three memeber variables:

id means this object’s id. you need to set the current number to id when create an object.

priority, means the importance among all jobs.

nextJob, a Job pointer, which points to the next Job.
(3)function toString return a string, its format is like: [id:priority], for example: [0:234]..

3. Class JobManager only has one instance, JobManager can manage the jobs in the job queue.
JobManager manages the job queue according to the principle FIFO(First In, First Out), no matter what the priority is..
This Class has a member variable named “jobFrontPointer”( Job *), which points to the first Job in the queue(first add).
static variable instance is the only instance of this class. You need to implement two static function getInstance and deleteInstance. “getInstance” will return the only instance of this class; “deleteInstance” will delete the only instance, if delete successfully, return true; otherwise, return false.
6 member functions:
void addJob(int pri = 0); which adds one job(its priority is pri, default: 0) to the end of queue.
void finishOneJob(); which finish the job in the begin and pop it out of the queue.
void sortJob(); which sorts the current queue according to their priority (if same, then the smaller id is in front of the larger one).
void printJob(); print the queue’s job, like “[2:23432]->[3:1]->[4:5]->[5:0]->[6:6666]” with endl.
int getNumOfJob(); return the number of jobs in the queue.
void clear(); clear the queue.

这道题主要是要求实现Job.cpp、JobManager.cpp,main.cpp、Job.h, JobManager.h已经给出。

// main.cpp

#include "JobManager.h"
using namespace std;

int main() {
    JobManager *manager1 = JobManager::getInstance();
    JobManager *manager2 = JobManager::getInstance();
    int pris[5] = {
  234, 23, 23432, 1, 5};
    for (int i = 0; i < 5; i++) {
        manager1->addJob(pris[i]);
    }
    manager2->printJob();
    cout << "The number of job is: " << manager1->getNumOfJob() << endl;
    manager1->finishOneJob();
    cout << "The number of job is: " << manager1->getNumOfJob() << endl;
    manager2->finishOneJob();
    cout << "The number of job is: " << manager1->getNumOfJob() << endl;
    manager1->addJob();
    manager1->addJob(6666);
    manager1->printJob();
    cout << 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值