简体   繁体   English

在多个对象上调用相同的方法

[英]Call same method on multiple objects

I have a number of methods that are called on different 3rd party systems.我有许多方法可以在不同的 3rd 方系统上调用。 I now have another 3rd party system that will have the same set of methods actioned against it.我现在有另一个 3rd 方系统,它将对它采取相同的方法集。 If both 3rd party systems are connected I will then call the methods on each object in turn.如果两个 3rd 方系统都已连接,我将依次调用每个对象上的方法。

Currently I have a class that I pass round that I can call the method once and it checks and then calls it on each system that is enabled, this has an instance of each objects classes, similar to this:目前我有一个我可以传递的类,我可以调用该方法一次,它检查然后在每个启用的系统上调用它,它具有每个对象类的实例,类似于:

 public class AACSCaller 
    {

        3rdPartySystem1 _system1;
        3rdPartySystem2 _system2;

        public AACSCaller(Settings appSettings)
        {
            _appSettings = appSettings;
            if (appSettings.system1Enabled)
            {
                _system1 = new 3rdPartySystem1();
            }
            if (appSettings.system2Enabled)
            {
                _system2 = new 3rdPartySystem2();
            }
        }
        public void Method1()
        {
            if (appSettings.system1Enabled)
            {
                _system1.Method1();

            }
            if (appSettings.system2Enabled)
            {
                _system2.Method1();

            }
        }

        public void Method2()
        {
            if (appSettings.system1Enabled)
            {
                _system1.Method2();

            }
            if (appSettings.system2Enabled)
            {
                _system2.Method2();

            }
        }
    }

Is this sensible, as it does seem there maybe a better way and I may well be connecting additional system at some point.这是否明智,因为它似乎确实有更好的方法,而且我很可能会在某个时候连接其他系统。

A possible solution here is to define an interface or base class for 3rdPartySystem1 and 3rdPartySystem2 classes, store instances in a collection and call required methods for every item in collection.此处可能的解决方案是为3rdPartySystem13rdPartySystem2类定义接口或基类,将实例存储在集合中并为集合中的每个项目调用所需的方法。 If only one system is enabled, you'll have only one item in collection, if both is enabled, you'll call them one by one in loop如果只启用了一个系统,你将只有一个集合,如果两个都启用,你将在循环中一一调用它们

public interface IThirdPartySystem
{
    void Method1();
    void Method2();
}

public class ThirdPartySystem1 : IThirdPartySystem
{
    //implementation
}

public class ThirdPartySystem2 : IThirdPartySystem
{
    //implementation
}

public class AACSCaller 
{
    IList<IThirdPartySystem> _systems = new List<IThirdPartySystem>();

    public AACSCaller(Settings appSettings)
    {
        _appSettings = appSettings;
        if (appSettings.system1Enabled)
        {
            _systems.Add(new ThirdPartySystem1());
        }
        if (appSettings.system2Enabled)
        {
            _systems.Add(new ThirdPartySystem2());
        }
    }
    public void Method1()
    {
        foreach (var system in _systems)
            system.Method1();           
    }

    public void Method2()
    {
         foreach (var system in _systems)
            system.Method2();            
    }
}

I suggest you to use interface that have Method1 and Method2 methods and then create to classes System1 and System2 that are implements the interface.我建议你使用的界面,有Method1Method2的方法,然后创建的类System1System2是实现该接口。 Where AACSCaller is create you initialize the correct implementation of the interface and in your methods your just Call to the correct instance method without conditions.在创建 AACSCaller 的地方,您初始化接口的正确实现,并在您的方法中无条件地调用正确的实例方法。

public class AACSCaller 
{
    IThirdPartySystem ThirdPartySystem;

    public AACSCaller(Settings appSettings)
    {
        _appSettings = appSettings;
        ThirdPartySystem = appSettings.system1Enabled ? new ThirdPartySystem1() : new ThirdPartySystem2();

    }

    public void Method1() => ThirdPartySystem.Method1();

    public void Method2() => ThirdPartySystem.Method2();
}

public interface IThirdPartySystem
{
    void Method1();
    void Method2();
}

public class ThirdPartySystem1 : IThirdPartySystem
{
    public void Method1()
    {
        //code here..
    }

    public void Method2()
    {
        //code here..
    }
}

public class ThirdPartySystem2 : IThirdPartySystem
    {
        public void Method1()
        {
            //code here..
        }

        public void Method2()
        {
            //code here..
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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