protected virtual methods in f#

余生颓废 提交于 2020-01-05 01:11:08

问题


  • F# does not support the definition of protected methods. Here it is explained why
  • F# replaces virtualmethods with abstractmethods defined in abstract classes (see here).

I was wondering if there is a way to prevent access to abstract methods from outside the derived classes at all.


回答1:


Like Patryk Ćwiek, I also don't think it's possible, but here's one alternative:

From Design Patterns we know that we should favour Composition over Inheritance. In my experience, everything you can do with Inheritance, you can also do with Composition. As an example, you can always replace Template Method with a Strategy.

A Template Method is a typical use of an abstract method, but if you replace it with a Strategy, you can (sort of) hide it from clients:

type Foo(strategy : IBar) =
    member this.CreateStuff() =
        // 1. Do something concrete here
        // 2. Use strategy for something here
        // 3. Do something else concrete here
        // 4. Return a result

No outside client of Foo can invoke strategy, so that accomplishes the same goal as making a member protected.

You may argue that the original creator of Foo may keep a reference to strategy, and will still be able to invoke it. That's true, but protected members aren't really completely hidden either, because you can often derive from the class in question, which enables you to invoke the protected member.

Another point is that if you separate the creator of Foo from the client of Foo, the strategy will be unavailable to the client.



来源:https://stackoverflow.com/questions/22221367/protected-virtual-methods-in-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!