forked from farhanjk/FortranPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
!Copyright (c) 2013 Farhan J. Khan | ||
!THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
!IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
!FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
!COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
!IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
!CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
!Main Program | ||
program bridge_main | ||
use bridge_module | ||
implicit none | ||
|
||
!shape to draw | ||
class(shape), allocatable :: s1 | ||
!Bridge implementation instances | ||
type(redCircleAPI) :: redCircle | ||
type(blueCircleAPI) :: blueCircle | ||
|
||
!passing the drawable custom api | ||
allocate(s1, source=circleShape(redCircle)) | ||
call s1%draw | ||
deallocate(s1) | ||
allocate(s1, source=circleShape(blueCircle)) | ||
call s1%draw | ||
deallocate(s1) | ||
end program bridge_main |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
!Copyright (c) 2013 Farhan J. Khan | ||
!THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
!IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
!FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
!COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
!IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
!CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
!Module for defining bridge | ||
module bridge_module | ||
implicit none | ||
|
||
!Bridge | ||
type, abstract :: drawAPI | ||
contains | ||
procedure(drawCircleInterface), deferred :: drawCircle | ||
end type drawAPI | ||
abstract interface | ||
subroutine drawCircleInterface(this) | ||
import drawAPI | ||
class(drawAPI), intent(in) :: this | ||
end subroutine drawCircleInterface | ||
end interface | ||
|
||
!Bridge concrete classes | ||
type, extends(drawAPI) :: redCircleAPI | ||
contains | ||
procedure :: drawCircle => drawRedCircleImpl | ||
end type redCircleAPI | ||
|
||
type, extends(drawAPI) :: blueCircleAPI | ||
contains | ||
procedure :: drawCircle => drawBlueCircleImpl | ||
end type blueCircleAPI | ||
|
||
!Classes using the bridge | ||
type, abstract :: shape | ||
class(drawAPI), allocatable :: d | ||
contains | ||
procedure(drawInterface), deferred :: draw | ||
end type shape | ||
abstract interface | ||
subroutine drawInterface(this) | ||
import shape | ||
class(shape), intent(in) :: this | ||
end subroutine drawInterface | ||
end interface | ||
|
||
type, extends(shape) :: circleShape | ||
contains | ||
procedure :: draw => drawCircleShape | ||
end type circleShape | ||
|
||
!Constructor for initializing | ||
interface circleShape | ||
module procedure init_circleShape | ||
end interface | ||
|
||
contains | ||
|
||
type(circleShape) function init_circleShape(c) | ||
class(drawAPI), intent(in) :: c | ||
allocate(init_circleShape%d, source = c) | ||
end function | ||
|
||
subroutine drawRedCircleImpl(this) | ||
class(redCircleAPI), intent(in) :: this | ||
print *, "Draw red circle" | ||
end subroutine drawRedCircleImpl | ||
|
||
subroutine drawBlueCircleImpl(this) | ||
class(blueCircleAPI), intent(in) :: this | ||
print *, "Draw blue circle" | ||
end subroutine drawBlueCircleImpl | ||
|
||
subroutine drawCircleShape(this) | ||
class(circleShape), intent(in) :: this | ||
call this%d%drawCircle | ||
end subroutine drawCircleShape | ||
end module bridge_module |