Skip to content

Commit

Permalink
Added bridge design pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
farhanjk committed Dec 27, 2013
1 parent 48971f1 commit 863cc2c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ FortranPatterns

Implementation of popular design patterns in Fortran. This is intended to facilitate the community using Fortran for computationally expensive tasks. The main philosophy here is to make these patterns available for improving the reusability and efficiency of the code.

Compiler Suggested: Fortran 90.
Tested on: GNU Fortran (GCC) 4.8.

### List of patterns:

Expand All @@ -16,7 +16,7 @@ Compiler Suggested: Fortran 90.

#### Structural Patterns
- [X] Adapter
- [ ] Bridge
- [X] Bridge
- [ ] Composite
- [ ] Decorator
- [ ] Façade
Expand Down
Binary file added structural/bridge/bridge_main
Binary file not shown.
27 changes: 27 additions & 0 deletions structural/bridge/bridge_main.f90
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
80 changes: 80 additions & 0 deletions structural/bridge/bridge_module.f90
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

0 comments on commit 863cc2c

Please sign in to comment.