首页 > 其他分享 >Tutorial 7_单例模式

Tutorial 7_单例模式

时间:2022-10-20 17:36:15浏览次数:50  
标签:StudentID 模式 ID instance static getID 单例 public Tutorial

学号的单一

仿照课堂的身份证的例子,实现每个同学仅有一个学号这一问题。

类图

 

 

代码

 

 

2.1    StudentID.java
package danli;

public class StudentID {

    private static StudentID instance=null;
    private String ID; 

    public String getID() 
    {
        return ID;
    }     

    public void setID(String iD) 
    {
        ID = iD;
    } 

    private StudentID()
    {
        
    }     

    public static StudentID getStudentID()
    {

        if(instance==null) {
            instance=new StudentID();
            instance.setID("20204044");
        }
        else
        {
            System.out.println("一个学生只能有一个学号");
        }

        return instance;

    }
    
}

2.2    Client.java
package danli;

public class Client {

    public static void main(String[] args) {

        StudentID stu1,stu2;

        stu1=StudentID.getStudentID();

        stu2=StudentID.getStudentID();

        String str1,str2;

        str1=stu1.getID();

        str2=stu2.getID();

        System.out.println("第一次学号:"+str1);

        System.out.println("第二次学号:"+str2);

        }
    
}

 

 

 

#include<iostream>
using namespace std; 

class Student
{

private:
 string ID;
 static Student* instance ;
 Student(){} 

public:
string getID() {
return ID;
}

void setID(string iD) {
ID = iD;
}

static Student* getInstance(){
if (instance==NULL){
instance = new Student();
cout << "入学分配学号" << endl;
instance->setID("20204044");
}
else
cout << "已经入学,学号唯一" << endl;
return instance;
}

};

Student* Student::instance=NULL;

int _tmain(int argc, _TCHAR* argv[])
{
Student* s, *s1;
string sID, s1ID;
 
s = Student::getInstance();
s1 = Student::getInstance();

cout << "s的学号为" << s->getID() << endl;
cout << "s1的学号为" << s1->getID() << endl;
cout << "s的地址为" << s << endl;
cout << "s1的地址为" << s1 << endl;

system("pause");

return 0;

}

 

标签:StudentID,模式,ID,instance,static,getID,单例,public,Tutorial
From: https://www.cnblogs.com/manmmm/p/16810608.html

相关文章

  • Tutorial 8_适配器模式
    双向适配器实现一个双向适配器,使得猫可以学狗叫,狗可以学猫抓老鼠。  代码  ICat.javapackageshipeiqi;publicinterfaceICat{publicvoideat()......
  • Tutorial 9_桥接模式
    两个维度的桥接模式用桥接模式实现在路上开车这个问题,其中,车可以是car或bus,路可以是水泥路或沥青路。类图  代码Vehicle.javapackageqiaojie;publicinterfa......
  • Collections 类中设计模式的应用
    装饰器模式Collections类是一个集合容器的工具类,提供了很多静态方法,用来创建各种集合容器,比如通过unmodifiableColletion()静态方法,来创建UnmodifiableCollection类......
  • Calendar 类中设计模式的应用
    包名:java.util.Calendar工厂模式Calendar类提供了大量跟日期相关的功能代码,同时,又提供了一个getInstance()工厂方法,用来根据不同的TimeZone和Locale创建不同的Ca......
  • 享元模式
    实验13:享元模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解享元模式的动机,掌握该模式的结构;2、能够利用享元模式解决实际问题。   ......
  • 9000字,唠唠架构中的设计模式
    1设计模式概述​ 软件设计模式(SoftwareDesignPattern),俗称设计模式,设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结。它描述了在软件设......
  • MybatisPlus对租户模式的支持(一)
    前言最近接到一个任务,要将现有的用户系统改成租户模式。改造成租户模式最简单的方式就是为需要进行数据隔离的表加上租户id字段,然后前端调接口查询数据时,根据当前用户的租......
  • 设计模式之UML类图
    UML图示简介在UML中,类使用包含类名、属性和操作且带有分割线的长方形来表示,如图所示,定义一个Student类,它包含属性name、age和id,以及操作modifyInfo()。其对应的......
  • .NET(C#) HttpClient单例(Singleton)和每次请求new HttpClient对比
    本文主要介绍.NET(C#)中,使用HttpClient执行求时,每次请求都执行newHttpClient创建一个实例和每次请求都使用同一个HttpClient(单例Singleton)分比区别。 1、每次请求创......
  • 【多线程那些事儿】如何使用C++写一个线程安全的单例模式?
    如何写一个线程安全的单例模式?单例模式的简单实现单例模式大概是流传最为广泛的设计模式之一了。一份简单的实现代码大概是下面这个样子的:classsingleton{public: s......