在功能方法中解决继承依赖关系 [英] Solve inheritance dependency in a functional approach

查看:148
本文介绍了在功能方法中解决继承依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题


继承依赖关系


  • 类型B的属性


  • 类型B继承于类型A




我们将考虑一个UI控件层次结构,其中每个控件属于顶级Form,而Form本身就是一个Control。


作者通过参数化类来解决这个问题:

  [< AbstractClass>] 
type Control<'Form>(name)=
member this.Name = name

抽象表单:'表单

类型表单(名称)=
inherit Control< Form>(name)

覆盖this.Form = this

类型Button(name,form)=
inherit Control< Form> ;(名称)

覆盖this.Form = form

让form = new Form(form)
let button = new Button(button,form)

但是我怎么能用功能方法重新设计:我们根本不会使用继承。相反,我们会将组合与参数化结合使用?

解决方案

这个问题很难回答,因为问题的表述本质上是面向对象的,当你说form是一个控件时,这个是这个问题的一部分暗示了OO建模世界的方式,你通常不会在函数式编程中使用它。
$ b

严格地说,您的示例代码实际上并没有做任何有用的事情 - 它会创建一堆对象,但不会使用它们来呈现任何用户界面或实现任何其他功能。

当然,我怀疑这就是问题所在,如果我想模拟一个非常简单的包含窗体和按钮的用户界面,我可能会从这样的事情开始:

  type SimpleControl = 
|字符串按钮
|字符串标签

类型ContainerControl =
| Form

类型Control =
| Containe ContainerControl * Control列表
|简单的SimpleControl

这使您可以定义容器控件(如窗体),其中可以包含其他控件和按钮和标签,这些是简单的控件,不能包含其他元素。这只是一个例子 - 取决于你实际想要构建什么类型的用户界面以及你想要怎样处理它们,你可以定义不同的类型。简短的说法是,函数式编程通常需要不同的思考方式,而在面向对象的世界中有意义的一些问题在功能世界中是没有意义的。


The problem:

Inheritance dependency

  • Type A stores a value of type B in a property

  • Type B inherits from type A

We'll consider a UI control hierarchy, where every control belongs to a top-level "Form", and the Form itself is a Control.

The author solves this problem by parameterizing classes:

[<AbstractClass>]
type Control<'Form>(name) = 
    member this.Name = name

    abstract Form : 'Form

type Form(name) = 
    inherit Control<Form>(name)

    override this.Form = this

type Button(name,form) = 
    inherit Control<Form>(name)

    override this.Form = form

let form = new Form("form")       
let button = new Button("button",form)

But how can I redesign in a functional approach: "We would not be using inheritance at all. Instead, we would use composition in conjunction with parameterization"?

解决方案

This question is hard to answer, because the formulation of the question is inherently object-oriented. When you say "form is a control", the "is a" part of the question hints at an OO way of modelling the world that you would not typically use in functional programming.

Strictly speaking, your sample code does not actually do anything useful - it creates a bunch of objects, but does not use them to render any user interface or implement any other functionality.
Of course, I suspect this is what the question is getting at. If I wanted to model a very simple user interface that includes forms and buttons, I might start with something like this:

type SimpleControl = 
  | Button of string
  | Label of string

type ContainerControl = 
  | Form 

type Control = 
  | Container of ContainerControl * Control list
  | Simple of SimpleControl

This lets you define container controls (such as form) that can contain other controls and buttons and labels, which are simple controls and cannot contain other elements. This is just an example though - depending on what kind of user interfaces you actually want to build and what you want to do with them, you would define the types differently.

The short version is, functional programming often requires a different way of thinking and some of the questions that make sense in object-oriented world do not make sense in the functional world.

这篇关于在功能方法中解决继承依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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