我在C#中制作了一个列表员工数据应用程序,其中包含AddEmployee,DeleteEmployee,SearchEmployee等功能。我想制作相同的程序,但使用单例设计模式。我知道 Singleton 的结构,但我搞砸了制作列表,然后将实际员工添加到列表中。
这是我程序的一部分:
class Student : IComparable<Student>
{
//public int id;
public string name;
public decimal salary;
public string position;
public int intern;
public Student(string name, string position, int intern, decimal salary)
{
this.name = name;
this.position = position;
this.intern = intern;
this.salary = salary;
}
public string Name { get; set; }
public decimal Salary { get; set; }
public string Position { get; set; }
public int Intern { get; set; }
public void CalculateSalary()
{
salary = 1000 * intern;
}
public override string ToString()
{
return "Name: " + name + "t Position: " + position + "t Intern: " + intern + "t Salary: " + salary;
}
/// <summary>
/// Gets student information from the user and adds a new student to the list
/// </summary>
/// <param name="existingStudents">The list of students to add to</param>
private static void AddStudent(List<Student> existingStudents)
{
string ans = "";
do{
// Initialize the list if it's null
if (existingStudents == null) existingStudents = new List<Student>();
int tempInt;
// Get student information from the user
Console.WriteLine("Enter new student information");
Console.Write(" 2. Enter student Name: ");
var name = Console.ReadLine();
Console.Write(" 3. Enter student Job Title: ");
var jobTitle = Console.ReadLine();
do
{
Console.Write(" 4. Enter student years of service: ");
} while (!int.TryParse(Console.ReadLine(), out tempInt));
var yrsOfService = tempInt;
var salar = tempInt;
// Add the new student to the list
existingStudents.Add(new Student(name, jobTitle, yrsOfService, salar));
foreach (Student stud in existingStudents)
{
stud.CalculateSalary();
Console.WriteLine(stud.ToString());
}
Console.WriteLine("Do you want to add another Student? y/n");
ans = Console.ReadLine();
}
while (ans != "n");
}
在主程序中,我有一个列表并调用 addStudent 函数:
List<Student> st = new List<Student>();
int answer = 1;
do
{
Console.WriteLine("============================");
Console.WriteLine("2. Read From File");
Console.WriteLine("3. Add Student...");
answer = Convert.ToInt32(Console.ReadLine());
switch (answer)
{
case 2:
JustReadFromFile(st, filePath);
break;
case 3:
AddStudent(st);
break;
case 4:
Console.WriteLine("Exit Menu...");
break;
}
} while (answer != 4);
列表 st = 新列表((;
我知道我必须创建一个私有静态实例。
private static Student st;
另外,应该有一个私人的学生构造函数,而不是我的上面。
private Student() { }
这就是创造我的新学生的方法。我也认为我需要类似的方法来创建我的列表,但我不确定。
public static Student getInstance()
{
if (st == null)
st = new Student();
return st;
}
你能给出一些方向和建议吗?谢谢
Student
类:
public class Student
{
public string Name { get; set; }
public decimal Salary { get; set; }
public string Position { get; set; }
public int Intern { get; set; }
}
StudentData
单例(您也可以将List<Student
设为私有,并在 Singleton 类本身中创建方法来与列表交互并向其获取/添加数据,但为了示例的简单性,我将其公开(:
public class StudentData
{
private static readonly StudentData _studentData = new StudentData();
public List<Student> Students { get; set; }
private StudentData()
{
Students = new List<Student>();
}
public static StudentData GetInstance()
{
return _studentData;
}
}
用法如下:
var studentData = StudentData.GetInstance();
//Get all students in the singleton
var students = studentData.Students;
//Add new student
studentData.Students.Add(new Student());
studentData.Students.Add(new Student
{
Intern = 1,
Name = "SomeName"
});
希望这会让你走向正确的方向。
public class Student
{
private static Student instance = null;
private static readonly object locker = new object();
public static Student Instance
{
get
{
lock (locker)
{
if (instance == null)
{
instance = new Student();
}
return instance;
}
}
}
public void SomeMethod()
{
}
}
然后,您可以像这样访问学生实例: Student.Instance.SomeMethod((;