Implementing FighterJet 

Now that we understand what to expect from the Vehicle interface, we can develop something that actually implements the interface. We will create a new FighterJets module and define the FighterJet data type as follows:

"FighterJet is a very fast vehicle with powerful weapons."
mutable struct FighterJet

"power status: true = on, false = off"
power::Bool

"current direction in radians"
direction::Float64

"current position coordinate (x,y)"
position::Tuple{Float64, Float64}

end

To conform to the Vehicle interface defined previously, we must first import the generic functions from the Vehicle module and then implement the logic for operating the FighterJet vehicle. Here is the code for the power_on and power_off functions:

# Import generic functions
import Vehicle: power_on!, power_off!, turn!, move!, position

# Implementation of Vehicle interface
function power_on!(fj::FighterJet)
fj.power = true
println("Powered on: ", fj)
nothing
end

function power_off!(fj::FighterJet)
fj.power = false
println("Powered off: ", fj)
nothing
end

Of course, a real fighter jet may be a bit more involved than just setting a Boolean field to either true or false. For testing purposes, we also print something to the console so that we know what is happening. Let's also define the function to steer the direction:

function turn!(fj::FighterJet, direction)
fj.direction = direction
println("Changed direction to ", direction, ": ", fj)
nothing
end

Again, the logic for the turn! function here is as simple as changing the direction field and printing some text on the console. The move! function is a little more interesting:

function move!(fj::FighterJet, distance) 
x, y = fj.position
dx = round(distance * cos(fj.direction), digits = 2)
dy = round(distance * sin(fj.direction), digits = 2)
fj.position = (x + dx, y + dy)
println("Moved (", dx, ",", dy, "): ", fj)
nothing
end

Here, we have used the trigonometric functions sin and cos to calculate the new position that the fighter jet will be traveling to. Finally, we must implement the position function, which returns the current position of the fighter jet:

function position(fj::FighterJet)
fj.position
end

Now that the FighterJet type fully implements the interface, we can utilize the game logic as expected. Let's give it a spin by creating a new FighterJet object and invoke the go! function as follows:

In a nutshell, implementing an interface is a fairly simple task. The key is to understand what functions are required to implement an interface and make sure that the custom data type can support those functions. As a professional developer, we should clearly document the interface functions so that there is no confusion about what needs to be implemented.

At this point, we can consider the interface that we just designed as hard contracts. They are hard in the sense that all of the functions specified in our interface must be implemented by any object participating in our space-travel program. In the next section, we will go over soft contracts, which correspond to interface functions that may be optional.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset