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

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

问题描述

最近我一直在考虑保护我的一些代码。我很好奇,如何确保一个对象永远不能直接创建,但只能通过工厂类的某种方法。让我们说我有一些业务对象类,我想确保这个类的任何实例将具有有效的内部状态。为了实现这一点,我需要在创建一个对象之前执行一些检查,可能在它的构造函数中。这一切都可以,直到我决定要让这个检查成为业务逻辑的一部分。那么,我如何才能通过业务逻辑类中的某种方法来安排一个业务对象来创建,而不是直接?使用C ++的一个好的老的朋友关键字的第一个自然的欲望将与C#短暂。所以我们需要其他选项...



我们来试试一下例子:

  public MyBusinessObjectClass 
{
public string MyProperty {get;私人集合

public MyBusinessObjectClass(s​​tring myProperty)
{
MyProperty = myProperty;
}
}

public MyBusinessLogicClass
{
public MyBusinessObjectClass CreateBusinessObject(string myProperty)
{
//执行一些检查on myProperty

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

返回null;
}
}

直到你还记得你还可以创建MyBusinessObjectClass实例直接,不检查输入。我想排除那个技术上的可能性。



那么社区对此有何看法?

  public BusinessClass 
{
public string MyProperty {get;私人集合}

private BusinessClass()
{
}

private BusinessClass(s​​tring myProperty)
{
MyProperty = myProperty;


public static BusinessClass CreateObject(string myProperty)
{
//对myProperty执行一些检查

if(/ * all ok * /)
返回新的BusinessClass(myProperty);

返回null;
}
}

调用它将非常简单:

  BusinessClass objBusiness = BusinessClass.CreateObject(someProperty); 


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...

Let's try some example:

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

    public MyBusinessObjectClass (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;
    }
}

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?

解决方案

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;
    }
}

Calling it would be pretty straightforward:

BusinessClass objBusiness = BusinessClass.CreateObject(someProperty);

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

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