using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ServiceLocator
{
class Program
{
static void Main(string[] args)
{
var conObj = (IConsoleIml)ServiceLocator.Loctory.GetServiceByType(typeof(IConsoleIml));
conObj.Console("");
Console.Read();
}
} public sealed class ServiceLocator
{
private static readonly ServiceLocator locator = new ServiceLocator();
private static readonly InitialContext initialContext = new InitialContext();
public object GetServiceByType(Type serviceType)
{
return initialContext.GetService(serviceType);
}
public static ServiceLocator Loctory { get { return locator; } }
}
public class InitialContext
{
public Dictionary<Type, ServiceFactoryBase> dic = new Dictionary<Type, ServiceFactoryBase>();
public InitialContext()
{
foreach (var item in this.GetType().Assembly.GetExportedTypes())
{
if (item.IsSubclassOf(typeof(ServiceFactoryBase)))
{
var factory = (ServiceFactoryBase)Activator.CreateInstance(item);
dic.Add(factory.ServiceType, factory);
}
}
} public object GetService(Type ServiceType)
{
var serviceFactory = dic[ServiceType];
if (serviceFactory != null)
return serviceFactory.GetService();
throw new InvalidOperationException();
}
}
public abstract class ServiceFactoryBase
{
private object serviceIml = null;
public object GetService()
{
if (serviceIml == null)
{
serviceIml = this.DoGetservice();
}
return serviceIml;
}
protected abstract object DoGetservice();
public abstract Type ServiceType { get; }
}
public class ConsoleFactory : ServiceFactoryBase
{
protected override object DoGetservice()
{
return new ConsoleIml();
} public override Type ServiceType
{
get { return typeof(IConsoleIml); }
}
}
public interface IConsoleIml
{
void Console(string content);
}
public class ConsoleIml : IConsoleIml
{
public void Console(string content)
{
System.Console.WriteLine(content);
}
} }

Service Locator 服务定位模式的更多相关文章

  1. PHP中应用Service Locator服务定位及单例模式

    单例模式将一个对象实例化后,放在静态变量中,供程序调用. 服务定位(ServiceLocator)就是对象工场Factory,调用者对象直接调用Service Locator,与被调用对象减轻了依赖关 ...

  2. Docker Kubernetes Service 网络服务代理模式详解

    Docker Kubernetes  Service 网络服务代理模式详解 Service service是实现kubernetes网络通信的一个服务 主要功能:负载均衡.网络规则分布到具体pod 注 ...

  3. 16_游戏编程模式ServiceLocator 服务定位

    ####简单说,就是某个系统作为一个服务,对全局系统可见. Service Locator (服务定位) ``` //简单粗暴的代码, 使用声音系统 // Use a static class? Au ...

  4. [Design Pattern] Service Locator Pattern 简单案例

    Service Locator Pattern,即服务定位模式,用于定位不同的服务.考虑到 InitialContext::lookup 的成本比较高,提供了 Cache 类缓存以定位到的服务. 代码 ...

  5. Service Locator 模式

    什么是Service Locator 模式? 服务定位模式(Service Locator Pattern)是一种软件开发中的设计模式,通过应用强大的抽象层,可对涉及尝试获取一个服务的过程进行封装.该 ...

  6. .NET 服务器定位模式(Service Locator Pattern)——Common Service Locator

    本文内容 场景 目标 解决方案 实现细节 思考 相关模式 更多信息 参考资料 Common Service Locator 代码很简单,它一般不会单独使用,而是作为一个单件模式,与像 .net Uni ...

  7. Service Locator Pattern 服务定位

    https://www.geeksforgeeks.org/service-locator-pattern/ Service Locator Pattern Last Updated: 06-03-2 ...

  8. 服务定位器(Service Locator)

    服务定位器(Service Locator) 跟DI容器类似,引入Service Locator目的也在于解耦.有许多成熟的设计模式也可用于解耦,但在Web应用上, Service Locator绝对 ...

  9. 避免在ASP.NET Core中使用服务定位器模式

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:服务定位器(Service Locator)作为一种反模式,一般情况下应该避免使用,在 ...

随机推荐

  1. 在Excel中使用SQL语句查询和筛选

    本文转自:http://blog.sina.com.cn/s/blog_5fc375650102e1g5.html 今天在微博上看到@数据分析精选 分享的一篇文章,是关于<在Excel中使用SQ ...

  2. Backbone 学习笔记

    Backbone 是一款基于模型-视图-控制器 MVC 模式的轻量级javascript 框架 ,可以用来帮助开发人员创建单页Web应用. 借助Backbone 我们可以使用REST的方式来最小化客户 ...

  3. 2015.4.21 实现一般免登陆,微博QQ分享,字体自适应等

    1.实现一般的登录验证和免登陆: 解决方法:node方法代码,nodeJS实现的session模块,不完整,但能用,仅供参考. 语言无所谓,session的机制都是一样的,实现不一样而已,:   2. ...

  4. JavaScript 快速排序(Quicksort)

    "快速排序"的思想很简单,整个排序过程只需要三步: (1)在数据集之中,选择一个元素作为"基准"(pivot). (2)所有小于"基准"的元 ...

  5. Linux进程间通信(四):命名管道 mkfifo()、open()、read()、close()

    在前一篇文章—— Linux进程间通信 -- 使用匿名管道 中,我们看到了如何使用匿名管道来在进程之间传递数据,同时也看到了这个方式的一个缺陷,就是这些进程都由一个共同的祖先进程启动,这给我们在不相关 ...

  6. JAVA中的线程安全与非线程安全

    原文:http://blog.csdn.net/xiao__gui/article/details/8934832 ArrayList和Vector有什么区别?HashMap和HashTable有什么 ...

  7. matplotlib绘制多组 散点连线图【用于对比】待实现

    绘制散点+连线图: http://www.cnblogs.com/aaronhoo/p/5150596.html http://zhidao.baidu.com/link?url=Q1b7NG8eEz ...

  8. Redis 3.0正式版发布,正式支持Redis集群

    Redis是一个开源.基于C语言.基于内存亦可持久化的高性能NoSQL数据库,同时,它还提供了多种语言的API.近日,Redis 3.0在经过6个RC版本后,其正式版终于发布了.Redis 3.0的最 ...

  9. ACM-ICPC退役选手的发言——满满的正能量(短视频)

    这是我在北京林业大学ACM-ICPC竞赛说明会上发言的录像 希望能激励大家在奋斗的道路上披荆斩棘,勇往直前!

  10. 1.go的Hello

    新建hello.go 内容: package main import ( "fmt" ) func main() { fmt.Println("Hello liuyao& ...