简体   繁体   中英

C# Let multiple class share the same instance of another class

Is there any way to let multiple class share the same instance of another class? So in my c# program i've got three class Gamelogic(A), FPSController(B), and SpawnPlayer(C).

In my case class B and C would use and alter some variables in A, in order to use the variables i'm currently instantiating A in two different classes and then use dot notation to access the variable from the instance of A, but the problem is that after A instantiated in different classes, the change in instance.variable does not share between B and C class at all.

Would static type be a good way of solving this? Or writing a main would be better.

Any suggestions would be appreciated.

There are a few ways. Here is one:

One way would be dependency injection. You can pass the instance of A along to the constructors of B and C (or to a setter/property of B and C):

A a = new A();
B b = new B(a);
C c = new C(a);

But this doesn't allow you to change the A reference in both objects easily, which seems to be your problem. One way to easily change the reference, is to wrap the reference in another object somehow.

A nice way to do this could be to create a Context object and pass that context object along to B and C instead of passing A. The context object plays the role as our wrapper. A context object becomes more useful if multiple variables needs to be shared between them (the shared/global state) - see the "Context pattern". Example:

public class Context {
    public A a;
    public ...other state you want to share...;

    public Context(A a) { this.a = a; ... }
}

...

A a = new A();

Context context = new Context(a,...);

B b = new B(context);
C c = new C(context);

Depending on your situation, a static variable might be fine, however. (Or a singleton)

(In some cases passing the A-instance along to the methods of B and C, rather than to their constructor, might be better - then they also always get the current version of a (and might be more thread-safe))

It sounds like you only need to have one instance of each class and pass the data between them. There are several ways to achieve this.

Static classes are one way to go then you'd simply access/set Gamelogic.Property in each of FPSController and SpawnPlayer .

Another way would be to pass the instance of Gamelogic to FPSController and SpawnPlayer when they are created.

However, both of these couple your classes together in ways that might make future changes hard.

A third way would be to make all three classes independent and use events to change values of variables and notify the other classes of changes to these variables. This, however, would probably introduce some lag into your game as there's no guarantee that events are processes in the order you expect.

I can think of two ways, either implement the Singleton pattern for the given classes, which would ensure that only one instance exist at any given time and you can freely use it where ever, will thus achieve your purpose. Check the link by Jon skeet.

If Singleton is not an option then, create a factory, where you pass a key and for given key ensure that only same object is returned, as you can internally store the object in a Dictionary , which would always return same object and you can still create multiple object unlike Singleton

show, this my code.

public interface IMyInterface
{
    void SetData(String Data);
}


    public partial class Form1 : Form, IMyInterface
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2(this as IMyInterface);
        frm.Show();
    }

    public void SetData(String Data)
    {
        textBox1.Text = Data;
    }
}


public partial class Form2 : Form
{
    private IMyInterface frm = null;

    public Form2(IMyInterface frm)
    {
        InitializeComponent();

        this.frm = frm;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        frm.SetData(textBox1.Text);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM