工厂模式实例雷锋工厂

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

namespace clas
{
    interface IFactory
    {
        LeiFeng CreateLeiFeng();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace clas
{
    class LeiFeng
    {public void sweep()
        {
            Console.WriteLine("扫地");
           

        }
        public void Wash()
    {
        Console.WriteLine("洗衣");
    }
        public void BuyRice()
        {
            Console.WriteLine("买米");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace clas
{
    class UnderGraduate:LeiFeng
    {
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace clas
{
    class Volunteer:LeiFeng
    {
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace clas
{
    class UndeGraduateFactory:IFactory
        
    {public LeiFeng CreateLeiFeng()
        {
            return new UnderGraduate();

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

namespace clas
{
    class VolunteerFactory:IFactory
    {public LeiFeng CreateLeiFeng()
        {
            return new Volunteer();
    }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace clas
{
    class Program
    {
        static void Main(string[] args)
        {
            IFactory factory = new UndeGraduateFactory();
            LeiFeng student = factory.CreateLeiFeng();
            student.BuyRice();
            student.sweep();
            student.Wash();

        }
    }
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/89135570