Try nullobject Mode

Source: Internet
Author: User

Using system;
Using system. collections;

Public class DB
{
Private Static arraylist list = new arraylist ();
Public static void addemp (string name)
{
List. Add (new employee (name ));
}
Public static iemployee searcheemployee (string empname)
{
Iemployee emptemp = nullemployee. getinstance ();
Foreach (employee EMP in List)
{
If (EMP. Name = empname)
{
Emptemp = EMP;
}
}
Return emptemp;
}
}

Public interface iemployee
{
Void sayhello ();
}

// Combined with Singleton

Public class nullemployee: iemployee
{
Private Static nullemployee EMP;
Private nullemployee (){}
Public static nullemployee getinstance ()
{
If (EMP = NULL)
EMP = new nullemployee ();
Return EMP;
}
Public void sayhello ()
{
Console. writeline ("null employee! ");
}
}

Public class employee: iemployee
{
Public Employee (string N)
{
Name = N;
}
Private string name;
Public string name
{
Get
{
Return name;
}
}
Public void sayhello ()
{
Console. writeline ("Hello! "+ Name );
}
Public override string tostring ()
{
Return "the employee name is:" + name;
}
}

Public class myclass
{
Public static void main ()
{
DB. addemp ("Randy ");
DB. addemp ("Edward ");
DB. addemp ("Flying ");
Console. writeline (db. searcheemployee ("Edward"). tostring ());
Console. writeline (db. searcheemployee ("fei"). tostring ());
DB. searcheemployee ("fei"). sayhello ();
Console. Read ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.