我有这个wcf方法
Profile GetProfileInfo(string profileType, string profileName)
Run Code Online (Sandbox Code Playgroud)
和业务规则:
如果profileType是从数据库中读取的"A".
如果profileType是从"xml"文件中读取的"B".
问题是:如何使用依赖注入容器实现它?
我试图简洁地描述何时使用工厂,我自己和我的团队.我遇到了以下相关问题,这有点帮助:
基于这些链接以及一系列其他来源(在下面列出),我提出了以下内容:
何时使用抽象工厂模式:
说明:
"我是否为每种对象类型创建了一个工厂?这看起来过分了."
何时不使用工厂:
参考文献:
我的问题是:我的摘要是否准确,是否有意义?有什么我忽略的吗?
提前致谢.
language-agnostic oop design-patterns factory factory-pattern
C#工厂模式是否需要向上转换?
我希望上课在图书馆G中在班级图书馆A中创造一个亚当,而不使G依赖于A.上帝在班级图书馆E中为夏娃制作亚当斯,夏娃可以知道并依赖亚当.(编辑 - 这个样本越来越好:)
我能想到的解决方案是在A中有一个AdamFactory.这样AdamFactory知道Adam并且可以很容易地创建它(可能只是调用Adam的构造函数).上帝收到一个AdamFactory,可以命令CreateAdam.
现在,因为上帝不被允许认识亚当,AdamFacotry的CreateAdam必须返回一个物体,这需要Eve将AdamFactory返回的物体向上投射到亚当身上.
我认为这会奏效.然而,我对上传感到不安,因为这是禁忌.这真的是必须的吗?
PS - 没有Blasphemy的意图,如果某人的感受受到伤害我会道歉.使用上帝和亚当而不是创造者和创造似乎更好,因为后两个词彼此太相似了.
编辑:重新接口建议.让我们假设Adam有两种方法:ProvideLove,ProvideFood和ProvideProtection(我们保持这个样本是kis-safe :).夏娃将亚当用于这两个目的,但当然上帝没有.那么为什么要向上帝提供AdamFactor返回实现IAdam而不仅仅是对象的东西的知识呢?我不明白!
编辑:工作代码(同一个库中的每个人,我的目标是分离到不同的库)看起来像这样:
Adam God.LoadAdam(AdamID theAdamID)
var adam = new Adam(theAdamId, this)
Adam.Adam(AdamID theAdamID, God theGod)
_god = theGod
_mind = theGod.LoadMind(theAdamId, this)
Mind God.LoadMind (AdamID theAdamID, Adam theAdam)
var mind = new Mind (theAdam)
var mindId = new minId(theAdamId)
mind.DeserializeFromFile(minId)
Mind.Mind (Adam theAdam)
_adam = theAdam
Run Code Online (Sandbox Code Playgroud) 我有2个班:
public class Articles
{
private string name;
public Articles(string name)
{
this.name = name;
}
public void Output()
{
Console.WriteLine("The class is: " + this.GetType());
Console.WriteLine("The name is: " + name);
}
}
Run Code Online (Sandbox Code Playgroud)
和
public class Questionnaire
{
private string name;
public Questionnaire(string name)
{
this.name = name;
}
public void Output()
{
Console.WriteLine("The class is: " + this.GetType());
Console.WriteLine("The name is: " + name);
}
}
Run Code Online (Sandbox Code Playgroud)
我想写一个方法,它取一个整数(1表示Articles应该返回,2表示意思Questionnaire)和一个名字.
此方法必须返回这两个类之一的实例:
public [What type??] Choose(int x, string …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring Boot for Java独立应用程序.我有一个使用服务的bean.我想在运行时注入该服务的不同实现,基于Spring的属性文件中的属性(就此而言为4).
这听起来像工厂模式,但Spring也允许使用注释来解决问题,就像这样.
@Autowired @Qualifier("selectorProperty") private MyService myService;
Run Code Online (Sandbox Code Playgroud)
然后在beans.xml文件中我有一个别名,这样我就可以在@Qualifier中使用该属性.
<alias name="${selector.property}" alias="selectorProperty" />
Run Code Online (Sandbox Code Playgroud)
在我的不同实现中,我会有不同的限定符.
@Component("Selector1")
public class MyServiceImpl1
@Component("Selector2")
public class MyServiceImpl2
Run Code Online (Sandbox Code Playgroud)
application.properties
selector.property = Selector1
selector.property = Selector2
Run Code Online (Sandbox Code Playgroud)
而对于工厂模式,在Spring中,您可以使用ServiceLocatorFactoryBean来创建一个可以提供相同功能的工厂.
<bean
class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean"
id="myServiceFactory">
<property
name="serviceLocatorInterface"
value="my.company.MyServiceFactory">
</property>
</bean>
public interface MyServiceFactory
{
MyService getMyService(String selector);
}
Run Code Online (Sandbox Code Playgroud)
然后在您的bean中,您可以使用类似的东西在运行时获得正确的实现,具体取决于属性的值.
@Value("${selector.property}") private String selectorProperty;
@Autowired private MyServiceFactory myServiceFactory;
private MyService myService;
@PostConstruct
public void postConstruct()
{
this.myService = myServiceFactory.getMyService(selectorProperty);
}
Run Code Online (Sandbox Code Playgroud)
但是这个解决方案的问题是我找不到一种方法来避免使用XML来定义工厂,我想只使用注释.
所以问题是,是否有一种方法可以仅使用注释来使用ServiceLocatorFactoryBean(或类似的东西),或者如果我不想在XML中定义bean,我是否被迫使用@Autowired @Qualifier方法?或者是否还有其他方法可以在运行时根据Spring 4避免使用XML的属性注入不同的服务?如果您的答案只是使用@Autowired @Qualifier别名,请说明为什么这比使用众所周知的工厂模式更好.
使用额外的XML迫使我@ImportResource("classpath:beans.xml")在我的Launcher类中使用,我也不愿意使用它.
谢谢.
我习惯于创建我自己的工厂(如图所示):
public class ElementFactory
{
public IElement Create(IHtml dom)
{
switch (dom.ElementType)
{
case "table":
return new TableElement(dom);
case "div":
return new DivElement(dom);
case "span":
return new SpanElement(dom);
}
return new PassthroughElement(dom);
}
}
Run Code Online (Sandbox Code Playgroud)
我终于开始在我当前的项目中使用IoC容器(AutoFac)了,我想知道是否有一些使用AutoFac优雅地实现同样功能的神奇方法?
我正在尝试创建一个传递参数x并返回一个新类的函数C.C应该是固定基类的子类A,只有一个加法:添加某个类属性并设置为相等x.
换一种说法:
class C(A):
C.p = x # x is the parameter passed to the factory function
Run Code Online (Sandbox Code Playgroud)
这很容易吗?我应该注意哪些问题?
像我之前的许多人一样,我正在努力让我的派生类型自动注册到我的工厂.我读了很多问题,并试着把注意力集中在我没有找到的地方.
除了自动注册外,我的一切运行都很顺利.
我的目标:
是)我有的:
template <class T>
class abstract_factory
{
public:
template < typename Tsub > static void register_class();
static T* create( const std::string& name );
private:
// allocator<T> is a helper class to create a pointer of correct type
static std::map<std::string, boost::shared_ptr<allocator<T> > > s_map;
}; …Run Code Online (Sandbox Code Playgroud) 我试图通过Autofac了解委托工厂模式.我知道如何使用带有Keyed()注册的IIndex <>来实现工厂,这里很好地解释了:配置在抽象类上定义的Autofac委托工厂
我想知道是否可以使用Func <>创建工厂,我将如何为以下示例进行注册:
public enum Service
{
Foo,
Bar
}
public interface FooService : IService
{
ServiceMethod();
}
public interface BarService : IService
{
ServiceMethod();
}
public class FooBarClient
{
private readonly IService service;
public FooBarClient(Func<Service, IService> service)
{
this.service = service(Service.Foo);
}
public void Process()
{
service.ServiceMethod(); // call the foo service.
}
}
Run Code Online (Sandbox Code Playgroud) 在工厂模式中使用Reflection是一个好习惯吗?
public class MyObjectFactory{
private Party party;
public Party getObject(String fullyqualifiedPath)
{
Class c = Class.forName(fullyqualifiedPath);
party = (PersonalParty)c.newInstance();
return party;
}
}
Run Code Online (Sandbox Code Playgroud)
PersonalParty实施派对
factory-pattern ×10
c# ×3
factory ×3
autofac ×2
java ×2
oop ×2
.net ×1
c++ ×1
casting ×1
class-design ×1
delegates ×1
duck-typing ×1
python ×1
python-3.x ×1
reflection ×1
spring ×1
spring-boot ×1
templates ×1
wcf ×1
xml ×1