正如标题所问的那样,你脑子里的触发器应该什么时候触发,表示“啊哈!我应该在这里使用工厂模式!”?我发现这些时刻会出现在许多其他设计模式中,但我从来没有停下来思考这个模式。
我必须从没有通用逻辑的文件名中解析一些数字.我想使用python的方式"尝试,你将被宽恕",或尝试 - 除了结构.现在我要添加两个以上的案例.这样做的正确方法是什么?我现在想要嵌套尝试或尝试 - 除了传递,尝试 - 除 - 传递,...哪一个会更好还是别的什么?工厂方法也许(如何?)?
这将在未来很容易扩展,因为会有更多的情况.
下面是我想要的(不起作用,因为每次尝试只能存在一个exeption):
try:
# first try
imNo = int(imBN.split('S0001')[-1].replace('.tif',''))
except:
# second try
imNo = int(imBN.split('S0001')[-1].replace('.tiff',''))
except:
# final try
imNo = int(imBN.split('_0_')[-1].replace('.tif',''))
Run Code Online (Sandbox Code Playgroud)
编辑:
哇,谢谢你的回答,但没有模式匹配请.我的坏,在开始时加上"一些共同的逻辑"(现在改为"没有共同的逻辑",对此抱歉).在上面的情况下,模式非常相似......让我添加一些完全不同的东西来说明问题.
except:
if imBN.find('first') > 0: imNo = 1
if imBN.find('second') > 0: imNo = 2
if imBN.find('third') > 0: imNo = 3
...
Run Code Online (Sandbox Code Playgroud) 工厂是否应负责寻找模型以及创建模型?
例如:
如果我有产品型号,它的工厂应该有以下方法:
$product = $productFactory->findByID(32);
$product = $productFactory->all();
$product = $productFactory->findByName($productName);
$product = $productFactory->create($data);
Run Code Online (Sandbox Code Playgroud) 考虑以下简单的工厂示例:
public class MyFactory : IMyFactory
{
public MyObject CreateObject()
{
return new MyObject();
}
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,MyObject实现IDisposable接口.通常我希望消费程序使用如下:
// Use using to properly dispose of MyObject
using (MyObject obj = myFactory.CreateObject())
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
通常的做法是期望消费工厂的开发人员处理这样的处理吗?或者工厂应该保留它创建的对象列表,并确保它们定期清理,或者可能在工厂处置时清理?
来自 GoF 关于工厂方法模式的章节:
定义一个用于创建对象的接口,但让子类决定实例化哪个类。工厂方法让类将实例化推迟到子类。
其思想是让子类决定实例化哪个类,而GoF对这一思想的实现,就是在超类中提供一个抽象方法,子类需要重写该方法,从而提供自己的实例化逻辑。
我不明白的是,为什么使用抽象方法来实现它,而不是使用作为工厂的类成员。我会解释一下。
这是 GoF 的方式:
public abstract class Creator {
public void doStuff() {
Product product = createProduct();
/* etc. */
}
public abstract Product createProduct();
}
Run Code Online (Sandbox Code Playgroud)
用法:
MyCreator myCreator = new Creator() {
@Override
public Product createProduct() {
return new MyProduct();
}
}
myCreator.doStuff();
Run Code Online (Sandbox Code Playgroud)
但为什么要费心子类化等呢?我建议这种方法:
public class Creator {
private Factory factory;
/* constructor */
public Creator(Factory factory) {
this.factory = factory;
}
public void doStuff() {
Product product = factory.createProduct(); …Run Code Online (Sandbox Code Playgroud) 此模式使用抽象工厂,然后是工厂的实现。
我确信这两个类有一个标准的命名约定,但我不知道它是什么。
例如:
public abstract class ChocolateFactory { };
public class MyChocolateFactory { } : ChocolateFactory
这里的标准约定是什么?
我在想 ChocolateFactoryBase 或 ConcreteChocolateFactory,但也许还有别的东西(很像 Enums 往往以 Enum 为后缀,例如PetTypeEnum这样你就可以做PetTypeEnum PetType;
希望这不是主观的。
我正在使用ASP.NET Core,我知道框架已经提供了这样的Logging机制,但是使用它来说明我的问题.
我正在使用一种Factory模式来构建Logger类,因为我不知道日志记录的类型(因为它存储在DB中).
ILogger合同
Log(string msg)
Run Code Online (Sandbox Code Playgroud)
然后,在根据从DB传递的参数创建Logger之后,LoggerFactory将返回ILogger:
public class LoggerFactory
{
public static Contracts.ILogger BuildLogger(LogType type)
{
return GetLogger(type);
}
//other code is omitted, GetLogger will return an implementation of the related logger
Run Code Online (Sandbox Code Playgroud)
现在,当我需要使用Logger时,我必须这样做:
public class MyService
{
private ILogger _logger
public MyService()
{
_logger = LoggerFactory.BuildLogger("myType");
}
Run Code Online (Sandbox Code Playgroud)
但是,我打算在没有任何实例化的情况下保留我的类,我需要在MyService中使用Constructor DI,我需要在Startup上注入所有依赖项:
services.AddTransient<Contracts.ILogger, LoggerFactory.BuildLogger("param") > ();
Run Code Online (Sandbox Code Playgroud)
但这不起作用,我们需要通过一个具体的实现.如何使用DI来完成这项工作,是否有更好的方法来实现它?
c# asp.net-mvc dependency-injection factory-pattern asp.net-core
public class PlatformEventFactory {
public PlatformEvent createEvent(String eventType) {
if (eventType.equals("deployment_activity")) {
return new UdeployEvent();
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个工厂类,它PlatformEvent根据 eventType创建类型对象。
在创建对象private RedisTemplate<String, Object> template后,UdeployEvent 类依赖于我要注入的UdeployEvent对象。
@Component
public class UdeployEvent implements PlatformEvent {
private RedisTemplate<String, Object> template;
private UDeployMessage uDeployMessage;
private static final Logger logger = LoggerFactory.getLogger(UdeployEvent.class);
public UdeployEvent() {
uDeployMessage = new UDeployMessage();
}
/*public void sendNotification() {
}*/
public RedisTemplate<String, Object> getTemplate() {
return template;
}
@Autowired
public void setTemplate(RedisTemplate<String, Object> …Run Code Online (Sandbox Code Playgroud) java spring dependency-injection factory-pattern spring-boot
当我在代码中使用工厂模式时,我总是问自己一个问题(C#,但它适用于我认为的任何语言)。
我有一个“服务”,负责与我的数据库交互,对对象进行操作并与我的对象模型交互。
该服务有时使用工厂来委托对象的实例化。但是这个工厂显然需要自己与数据库交互才能正确实例化我的对象。例如,将数据库上下文传递给 Create 方法是好还是坏做法?
像这样 :
var myNewObject = MyFactory.Create(myDatabaseContext);
Run Code Online (Sandbox Code Playgroud)
另一种方法是让服务始终是唯一与数据库对话的服务。
var myNewObject = MyFactory.Create();
var extraProperty = myDatabaseContext.Get(something);
myNewObject.extraProp = extraProperty;
Run Code Online (Sandbox Code Playgroud)
有什么建议吗?
c# design-patterns entity-framework factory-pattern dbcontext
我正在尝试在工厂设计模式中使用智能指针。我进行了谷歌搜索以获得一些想法,但找不到任何类型的实现,但有很多很棒的想法。所以,我写了自己的代码,但有两点我不确定。
首先,我将添加我的代码,然后提出我的问题。
// AgentGameStyleFactory.hpp
#ifndef AgentGameStyleFactory_hpp
#define AgentGameStyleFactory_hpp
#include "AgentGameStyle.hpp"
#include <memory>
class AgentGameStyleFactory
{
public:
static std::unique_ptr<AgentGameStyle> instantiate(const AgentPlayingStyleData::ePLAYING_CHARACTER pStyle);
private:
static AgentGameStyle* instantiateHelper(const AgentPlayingStyleData::ePLAYING_CHARACTER pStyle);
};
#endif /* AgentGameStyleFactory_hpp */
Run Code Online (Sandbox Code Playgroud)
// AgentGameStyleFactory.cpp
#include "AgentGameStyleFactory.hpp"
using namespace AgentPlayingStyleData;
std::unique_ptr<AgentGameStyle> AgentGameStyleFactory::instantiate(const ePLAYING_CHARACTER pStyle)
{
std::unique_ptr<AgentGameStyle> newStyle( instantiateHelper(pStyle) );
return std::move(newStyle);
}
AgentGameStyle* AgentGameStyleFactory::instantiateHelper(const ePLAYING_CHARACTER pStyle)
{
AgentGameStyle* newStyle = NULL;
switch(pStyle)
{
case ePLAYING_CHARACTER::ePC_CONTAIN:
newStyle = new ContainGameStyleAgent();
break;
case ePLAYING_CHARACTER::ePC_COUNTER:
newStyle = new CounterGameStyleAgent();
break;
case ePLAYING_CHARACTER::ePC_STANDARD:
newStyle = …Run Code Online (Sandbox Code Playgroud) factory-pattern ×10
c# ×4
java ×2
abstract ×1
asp.net ×1
asp.net-core ×1
asp.net-mvc ×1
c++ ×1
dbcontext ×1
dispose ×1
factory ×1
idisposable ×1
laravel ×1
nested ×1
oop ×1
php ×1
python ×1
sequential ×1
spring ×1
spring-boot ×1
try-catch ×1
vb.net ×1