如何在不影响派生类中的实现的情况下表示基类中方法执行的状态?

发布于 2024-08-21 11:54:22 字数 1500 浏览 7 评论 0原文

我正在尝试创建一个抽象类,定义一个抽象方法,该方法是从在抽象方法中实现某些特定行为的其他类派生的。我希望抽象类包含某种状态信息,该信息表示派生类中的实现没有错误地退出,但我想在 AbstractClass 中而不是在派生类中实现所有状态处理。我想让派生类完全不知道 AbstractClass 中的功能。下面是一个例子。我在代码中做了注释来描述我想要实现的目标。

public abstract class AbstractClass
    {
        public void Start()
        {
            ThreadStart ts = new ThreadStart(PerformWork);
            Thread t = new Thread(ts);
            t.Start();
            Thread.Sleep(2000);

            // Dependent on if ReportWork exited without expcetions
            // I want to call ReportSuccess or ReportFailure from this method.
            // However, I dont want to implement any reporting functionallity (or
            // as little as possible)
            // into the deriving classes PerformWork() method. Simply put
            // I want the deriving classes to be totally unaware of the reporting. 

        }

        public void ReportSuccess()
        {
            Console.WriteLine("Success!");
        }

        public void ReportFailure()
        {
            Console.WriteLine("Failure!");
        }
        public abstract void PerformWork();
    }

从 AbstractClass 派生的类:

class ConcreteImplementationClass:AbstractClass
    {
        public override void PerformWork()
        {
            // Implements some functionality
            // without knowing anything about
            // whats going on in AbstractClass.           
        }
    }

有人对我如何实现此功能或如何创建类似的功能有任何建议吗?

I'm trying to create an abstract class, defining an abstract method, that is to be derived from other classes that implements some specific behavior in the abstract method. I want the abstract class to contain some kind of state information that represents however the implementation in the derived classes exited without errors, but I want to implement all state handling in AbstractClass and not in the deriving classes. I want to make the deriving classes totally unaware of the functionality in AbstractClass. Below is an example. I made comments in the code to describe what I'm trying to achieve.

public abstract class AbstractClass
    {
        public void Start()
        {
            ThreadStart ts = new ThreadStart(PerformWork);
            Thread t = new Thread(ts);
            t.Start();
            Thread.Sleep(2000);

            // Dependent on if ReportWork exited without expcetions
            // I want to call ReportSuccess or ReportFailure from this method.
            // However, I dont want to implement any reporting functionallity (or
            // as little as possible)
            // into the deriving classes PerformWork() method. Simply put
            // I want the deriving classes to be totally unaware of the reporting. 

        }

        public void ReportSuccess()
        {
            Console.WriteLine("Success!");
        }

        public void ReportFailure()
        {
            Console.WriteLine("Failure!");
        }
        public abstract void PerformWork();
    }

A class deriving from AbstractClass:

class ConcreteImplementationClass:AbstractClass
    {
        public override void PerformWork()
        {
            // Implements some functionality
            // without knowing anything about
            // whats going on in AbstractClass.           
        }
    }

Do anyone have any advice for how I could achieve this functionality, or how I could create something similar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等待圉鍢 2024-08-28 11:54:22

如果我理解正确的话,如果 PerformWork() 成功则调用 ReportSuccess() ,如果失败则调用 ReportFailure()

为什么不

public abstract void PerformWork();

改成

public void Start()
{
    bool result = false;

    // This will enable Perform work to operate in its own thread
    Action threadAction = new Action(() =>
        {
            result = PerformWork();
        });

    ThreadStart ts = new ThreadStart(threadAction);
    Thread t = new Thread(ts);
    t.Start();
    Thread.Sleep(2000);

    // Dependent on if ReportWork exited without expcetions
    // I want to call ReportSuccess or ReportFailure from this method.
    // However, I dont want to implement any reporting functionallity (or
    // as little as possible)
    // into the deriving classes PerformWork() method. Simply put
    // I want the deriving classes to be totally unaware of the reporting. 

    if(result)
    {
        ReportSuccess();
    }
    else
    {
        ReportFailure();
    }
}

If I understand this correctly you want to call ReportSuccess() if the PerformWork() is successfully and ReportFailure() if it fails?

Why not change

public abstract void PerformWork();

to

public void Start()
{
    bool result = false;

    // This will enable Perform work to operate in its own thread
    Action threadAction = new Action(() =>
        {
            result = PerformWork();
        });

    ThreadStart ts = new ThreadStart(threadAction);
    Thread t = new Thread(ts);
    t.Start();
    Thread.Sleep(2000);

    // Dependent on if ReportWork exited without expcetions
    // I want to call ReportSuccess or ReportFailure from this method.
    // However, I dont want to implement any reporting functionallity (or
    // as little as possible)
    // into the deriving classes PerformWork() method. Simply put
    // I want the deriving classes to be totally unaware of the reporting. 

    if(result)
    {
        ReportSuccess();
    }
    else
    {
        ReportFailure();
    }
}
一城柳絮吹成雪 2024-08-28 11:54:22

考虑到您想在其中运行代码,然后在所有派生方法中调用 base.PerformWork() ,您是否不能将基方法设为虚拟?

Can you not make your base method virtual, given that you want to run code in it, then call base.PerformWork() in all the derived methods?

岛歌少女 2024-08-28 11:54:22

您正在描述面向方面的编程,AOP。

您可以使用任意数量的拦截策略轻松实现此目的。最简单的可能是城堡代理。

请参阅这个...

You are describing Aspect Oriented Programming, AOP.

You can easily achieve this using any number of intercept strategies. The easiest would probably be castle proxy.

See this...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文