Visual Model For F# – Example 2

Rich Inheritance – Example 2

In this example, a program was modeled (Figure 1), which has a number of classes/types, with the inheritance relationships between them.

The below diagram shows the type called Shape, which has been marked as abstract. This type represents an abstract shape. In this class there is an abstract method Area, which will return the basic type float. This method will have to be overwritten (called the override) for each of the classes that inherit from the class Shape. Environment Microsoft Visual Studio 2010 even by compiling the program will tell you that the Area method should be defined in each class that inherits from the abstract Shape element.

Each class has overridden the method Area, and has been used to keyword override. In addition, Circle class has defined a property called pi, which will store an approximation of Pi needed to calculate the surface area.

Each of the three classes, which have derived from the Shape class in some other way calculate a surface area of the figure. The model is defined by three blocks of code names CodeBlockForCircle, CodeBlockForSquare and CodeBlockForTriangle, which were supplemented with the appropriate code.

Przykład związany z dziedziczeniemFigure 1: Example of inheritance associated with

The following listing is a visible source code generated on the basis of this model.

The code is properly formatted and suitable for compilation, if you complete the relevant sections do and let. It will be possible to compile the code even when the section let of the class Circle is only supplemented by hand, and sections do are removed everywhere.

You can see that each of the overwritten methods to calculate the surface area have a body, which is consistent with what is contained in the respective blocks of code.

Thanks to the plugin, it’s easy to model the hierarchy of classes. In designing the model, the user focuses on the project and therefore makes less mistakes in writing the source code.

[<AbstractClass>]
type Shape () =
  do
    //to do
  abstract member Area  : float

type Circle (r : float) =
  inherit Shape()
  let pi =
  do
    //to do
  override this.Area  =
    pi * r * r

type Square (a : float) =
  inherit Shape()
  do
    //to do
  override this.Area  =
    a * a

type Triangle (a : float,h : float) =
  inherit Shape()
  do
    //to do
  override this.Area  =
    0.5 * a * h

Listing 1: The source code generated from the model of Figure 1

adamex

About Adam Bielasty

.NET Software Developer

One thought on “Visual Model For F# – Example 2

  1. Nice post. Its realy good. Many info help me.

Leave a Reply

Your email address will not be published. Required fields are marked *


5 + = 13

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>