Last active
November 28, 2018 16:05
-
-
Save Adzz/99253ac7ce382b62b028ae46efe67fd0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule AmazeOn do | |
defstruct [price: 10] | |
end | |
defmodule Area do | |
defstruct [] | |
end | |
defmodule Square do | |
defstruct [:side] | |
end | |
defprotocol ShapePricePerShop do | |
def calculate(calculation, shape, shop) | |
end | |
defprotocol AreaProtocol do | |
def calculate(shape, shop) | |
end | |
defprotocol SquareProtocol do | |
def calculate(shop, shape) | |
end | |
defimpl SquareProtocol, for: AmazeOn do | |
def calculate(%AmazeOn{price: price}, %Square{side: side}) do | |
side * side * price | |
end | |
end | |
defimpl AreaProtocol, for: Square do | |
def calculate(shape = %Square{}, shop) do | |
SquareProtocol.calculate(shop, shape) | |
end | |
end | |
defimpl ShapePricePerShop, for: Area do | |
def calculate(%Area{}, shape, shop) do | |
AreaProtocol.calculate(shape, shop) | |
end | |
end | |
ShapePricePerShop.calculate(%Area{}, %Square{side: 10}, %AmazeOn{}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment