c# 享元模式简单例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 享元模式
{
    public abstract class abStudent
    {
        public string Name;
        public string schName;
        public string Sex;
        public abStudent()
        {
            schName = "昆明理工大学";
            Sex = "男";
        }

        public override string ToString()
        {
            return string.Format("我叫{0},性别{1},在读学校{2}", Name, Sex, schName);
        }
    }
    public class Student:abStudent
    {
        public Student(string name)
        {
            Name = name;
        }
    }


    public class School
    {
        private Dictionary StudentList;
        public School()
        {
            StudentList = new Dictionary();
            StudentList.Add(1, new Student("张三"));
            StudentList.Add(2, new Student("李四"));
        }
        public Student GetStudent(int num)
        {
            return StudentList[num] as Student;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            School school = new School();
            Student student = school.GetStudent(1);
            Console.WriteLine(student.ToString());
            student = school.GetStudent(2);
            Console.WriteLine(student.ToString());
            Console.ReadKey();
        }
    }
}

你可能感兴趣的:(程序设计模式)