forked from WeihanLi/DesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
28 lines (25 loc) · 967 Bytes
/
Program.cs
File metadata and controls
28 lines (25 loc) · 967 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using Autofac;
namespace FactoryMethodPattern
{
public class Program
{
public static void Main(string[] args)
{
// 这里显示 new 对象的地方同样可以通过 配置+反射 和 依赖注入 的方式去做,可参考 抽象工厂模式
ILeifengFactory factory = new UndergraduteFactory();
var studentLeifeng = factory.CreateLeifeng();
studentLeifeng.BuyRice();
var studentLeifeng1 = factory.CreateLeifeng();
studentLeifeng1.Sweep();
// 依赖注入
var builder = new ContainerBuilder();
builder.RegisterType<VolunteerFactory>().As<ILeifengFactory>();
var container = builder.Build();
var leifengFactory = container.Resolve<ILeifengFactory>();
var volunteer = leifengFactory.CreateLeifeng();
volunteer.Wash();
Console.ReadLine();
}
}
}