传递类实例的最佳方法 [英] Best method to pass instance of class

查看:100
本文介绍了传递类实例的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题。

有一个名为Employee的类或dll。

现在所有其他类或dll都不应该创建employee类的实例。

但是他们可以在不创建实例的情况下使用员工类。



我想在Employee类中创建一个方法,用于传递员工类的实例。

有没有标准这样做的方法?或者最好的方法是什么?



不能使用静态类它会阻止任何实例。但我想创建实例。



如果你不明白。请在下面评论我很乐意提供更多详细信息。



谢谢。

I have simple problem.
There is one class or dll named Employee.
Now all the other class or dlls should not create instance of employee class.
But they can use employee class without creating instance.
OR
I want to create a method in Employee class which will be used to pass the instance of employee class.
Is there any standard way to do this? OR What is the best way to do it?

Cannot use static class it will prevent any instance. But I want to create instance.

If you didn't understand. Please comment below I will happy to provide additional details.

Thanks.

推荐答案

所以,



有一种名为Factory方法的设计模式。您可以将它与私有构造函数结合使用。这样您就可以根据自己的条件创建对象。阻止一些字段进行编辑等...



这里你有一些天真的实现,它可以让你概述这种模式:

So,

There is design pattern called Factory method. You can use this in combination with private contructor. This way you can create objects on your conditions. Block some fields for editing etc...

Here you have some naive implementation which can give you overview of this pattern:
public enum EmployeeType
{
    Regular,
    Manager,
}

public class Employee
{
    #region Public members
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Employee Manager { get; private set; }

    public EmployeeType EmployeeType
    {
        get;
        private set;
    }
    #endregion

    #region Constructors
    /// <summary>
    /// Private constructor prevents to create instance of this class from outside the class
    /// </summary>
    private Employee(EmployeeType employeeType, Employee manager = null)
    {
        EmployeeType = employeeType;
        Manager = manager;
    }
    #endregion

    #region Factory methods
    /// <summary>
    /// Factory method to create Regular employe
    /// </summary>
    /// <param name="manager"></param>
    /// <returns></returns>
    public static Employee CreateRegularEmployee(Employee manager)
    {
        return new Employee(EmployeeType.Regular, manager);
    }

    /// <summary>
    /// Factory method to create Manager
    /// </summary>
    /// <returns></returns>
    public static Employee CreateManager()
    {
        return new Employee(EmployeeType.Manager);
    }
    #endregion
}





创建Employee类实例的唯一方法是使用其静态方法:





Only way to create instance of Employee class is to use its static methods:

var manager = Employee.CreateManager();
manager.FirstName = "Jonh";
manager.LastName = "Smith";

var regular = Employee.CreateRegularEmployee(manager);
regular.FirstName = "Adam";
regular.LastName = "Hyde";





如果您尝试以正常方式创建Employee类的实例:



If you try to create instance of Employee class in normal way:

var regular2 = new Employee();



它不会编译而你将收到错误:


it won't compile and you will get an error:

Error	1	'FactoryMethod.Employee.Employee(FactoryMethod.EmployeeType, FactoryMethod.Employee)' is inaccessible due to its protection level	F:\Projekty\net\WinForms\FactoryMethod\FactoryMethod\Program.cs	21	28	FactoryMethod





有关于CP的文章:

在C#中实施工厂方法 [ ^ ]

工厂方法模式 [ ^ ]



[更新]

我只是测试了另一个解您将此类放在单独的DLL中,并将构造函数的保护级别从Private更改为Internal(您将拥有公共类和内部构造函数)。然后,您可以从DLL内部创建Employee类的实例,但不能从外部创建。您仍然可以使用Factory Method模式来实例化Employee类,或者您可以使用与Factory Method类似的Factory模式,您需要创建另一个类来创建Employee类,即EmployeeFactory,如下所示:





There are articles on CP:
Implementing Factory Method in C#[^]
Factory Method Pattern[^]

[Update]
I've just tested another solution. You an place this class in separate DLL and change constructor's protection level from Private to Internal (you will have public class and internal constructor). Then you can create instance of Employee class from inside of DLL but not from outside. You can still use Factory Method pattern to instantiate Employee class or you can use Factory pattern which is similar to Factory Method by you need to create another class to create Employee class i.e. EmployeeFactory like this:

public static class EmployeeFactory
{
    /// <summary>
    /// Factory method to create Regular employe
    /// </summary>
    /// <param name="manager"></param>
    /// <returns></returns>
    public static Employee CreateRegularEmployee(Employee manager)
    {
        return new Employee(EmployeeType.Regular, manager);
    }

    /// <summary>
    /// Factory method to create Manager
    /// </summary>
    /// <returns></returns>
    public static Employee CreateManager()
    {
        return new Employee(EmployeeType.Manager);
    }
}





并按照以下方式使用:



And use it like this:

var manager = EmployeeFactory.CreateManager();
manager.FirstName = "Jonh";
manager.LastName = "Smith";

var regular = EmployeeFactory.CreateRegularEmployee(manager);
regular.FirstName = "Adam";
regular.LastName = "Hyde";





如果您尝试以正常方式创建Employee类的实例:



If you try to create instance of Employee class in normal way:

var employee = new Employee();





它将无法编译,您将收到错误:



It won't compile and you will get an error:

Error	1	The type 'FactoryMethod.Employee' has no constructors defined	F:\Projekty\net\WinForms\FactoryMethod\FactoryMethod\Program.cs	21	28	FactoryMethod





我希望它可以帮到你。



I hope it help you.


因为你的类使用的是static关键字。 。 。查看更多



参考:



http://www.dotnetperls.com/static [ ^ ]



http://www.c-sharpcorner.com/UploadFile/74ce7b/static-class-in-C-Sharp/ [ ^ ]



静态关键字揭秘 [ ^ ]
because your class using the static keyword . . . see more

Reference :

http://www.dotnetperls.com/static[^]

http://www.c-sharpcorner.com/UploadFile/74ce7b/static-class-in-C-Sharp/[^]

Static Keyword Demystified[^]


看看这篇文章http://msdn.microsoft.com/en-us/library/ff650316.aspx [ ^ ]


这篇关于传递类实例的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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