什么样的模式是这样的('提供')? [英] What kind of pattern is this ('Provider')?

查看:107
本文介绍了什么样的模式是这样的('提供')?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工作中我们使用的是'模式',我并没有真正找到在GoF的书(但可能是由于缺乏在这件事情的能力,我只是脱脂模式),并且我仍然怀疑有些

At work we are using a 'pattern' that I didn't really find in the GoF book (but that may be due to lack of competence in this matter, I just skimmed the patterns) and that I'm still doubting somewhat.

我说,如果我们有一个包含项目的数据访问的用于管理,良好的数据访问的多项目解决方案。然后,通常我看到有这样的结构吧:

Say, if we have a multi-project solution containing a project DataAccess that manages, well, the data access. Then usually I see it having a structure like this:

Providers (Folder)
 - DataAccessProvider.cs

Interfaces (Folder)
 - IFileLoader.cs

Implementors (Folder)
 - FileLoader.cs

在这里, FileLoader 将是内部实施接口 IFileLoader ,和供应商如下:

Here, FileLoader would be an internal implementation of the interface IFileLoader, and the provider looks like this:

public static class DataAccessProvider
{
  public static IFileLoader FileLoader
  {
    get { return new FileLoader(); }
  }
}



是什么样的设计模式,这(如果有的话),什么是除了屏蔽具体实施 IFileLoader 接口的它的真正用途?

What kind of design pattern is this (if any), and what are its real uses besides masking the specific implementation of the IFileLoader interface?

其次,是这真的是好作风?我想知道,例如,如果有喜欢的

And secondly, is this really 'good style'? I wonder, for example, what happens if there are many calls like

string content = DataAccessProvider.FileLoader.LoadContentFromFile("abc.txt");

这会叫新FileLoader()时它被使用。是不是有一个更优雅的方式做了类似的做法?

This would call new FileLoader() whenever it is used. Isn't there a more elegant way to do a similar approach?

推荐答案

在这个例子中, DataAccessProvider 是一个简单的工厂方法(图案)的一个例子。通常情况下,你将有一个调用方法 GetFileLoader() CreateFileLoader(),而不是一个性质的版本,但结果是一样。

In this example the DataAccessProvider is an example of a simple Factory Method (pattern). Normally you would have a method called GetFileLoader() or CreateFileLoader() instead of a Property version, but the result is the same.

返回的目的 IFileProvider 而不是 FileProvider 是依赖倒置,这样人们可以写其他类型的 FileProvider ,并将其注入到应用程序,而不进行返工或重新编译所有的对象依赖于一个<$ C需要$ C> IFileProvider 。这不是屏蔽。

The purpose of returning the IFileProvider instead of FileProvider is for Dependency Inversion, this way one could write other types of FileProvider and inject them into the application without needed to rework or recompile all of the object that depend on an IFileProvider. It's not about masking.

如果人关心有多少 FileLoader 的实例被创建,那么可以使用该Singleton模式目的。然而,这通常不是一个问题,如果 FileLoader 是一个轻量级的对象,因为CLR垃圾收集器将自动为您采取照顾。

If one is concerned about how many instances of FileLoader are created, then one could use the Singleton pattern for that object. However, this is normally not an issue if the FileLoader is a lightweight object, since the CLR garbage collector will take care of that automatically for you.

这篇关于什么样的模式是这样的('提供')?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆