在C#工厂模式:如何保证一个对象实例只能由一个工厂类来创建? [英] Factory pattern in C#: How to ensure an object instance can only be created by a factory class?

查看:218
本文介绍了在C#工厂模式:如何保证一个对象实例只能由一个工厂类来创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在思考如何保护我的一些code的。我很好奇,人能如何确保一个对象不能直接创建,但只能通过工厂类的一些方法。让我们说我有一些业务对象类,我要确保这个类的任何实例都会有一个有效的内部状态。为了实现这一点,我将需要很可能在它的构造创建对象,之前执行一些检查。直到我决定我想使这个检查是业务逻辑的一部分,这都是好的。所以,我怎么能安排一个业务对象只能通过我的业务逻辑类中的一些方法可创建的,但从来没有直接?第一个自然渴望用好老朋友C ++的关键字将功亏一篑用C#。因此,我们需要其他选项...

Recently I've been thinking about securing some of my code. I'm curious how one could make sure an object can never be created directly, but only via some method of a factory class. Let us say I have some "business object" class and I want to make sure any instance of this class will have a valid internal state. In order to achieve this I will need to perform some check before creating an object, probably in its constructor. This is all okay until I decide I want to make this check be a part of the business logic. So, how can I arrange for a business object to be creatable only through some method in my business logic class but never directly? The first natural desire to use a good old "friend" keyword of C++ will fall short with C#. So we need other options...

让我们尝试一些例子:

public MyBusinessObjectClass
{
    public string MyProperty { get; private set; }

    public MyBoClass (string myProperty)
    {
        MyProperty = myProperty;
    }
}

public MyBusinessLogicClass
{
    public MyBusinessObjectClass CreateBusinessObject (string myProperty)
    {
        // Perform some check on myProperty

        if (true /* check is okay */)
            return new MyBusinessObjectClass (myProperty);

        return null;
    }
}

直到你还记得你还可以直接创建MyBusinessObjectClass实例,不检查输入的这一切​​都还好。我想完全排除这种可能性的技术。

It's all okay until you remember you can still create MyBusinessObjectClass instance directly, without checking the input. I would like to exclude that technical possibility altogether.

那么,什么是社会思考这个?

So, what does the community think about this?

推荐答案

看起来你只是想创建对象之前运行一些业务逻辑 - 所以你为什么不只是创建里面,做了BusinessClass一个静态方法的所有脏myProperty的检查工作,使构造私有?

Looks like you just want to run some business logic before creating the object - so why dont you just create a static method inside the "BusinessClass" that does all the dirty "myProperty" checking work, and make the constructor private?

public BusinessClass
{
    public string MyProperty { get; private set; }

    private BusinessClass()
    {
    }

    private BusinessClass(string myProperty)
    {
        MyProperty = myProperty;
    }

    public static BusinessClass CreateObject(string myProperty)
    {
        // Perform some check on myProperty

        if (/* all ok */)
            return new BusinessClass(myProperty);

        return null;
    }
}

调用这将是pretty简单:

Calling it would be pretty straightforward:

BusinessClass objBusiness = BusinessClass.CreateObject(someProperty);

这篇关于在C#工厂模式:如何保证一个对象实例只能由一个工厂类来创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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