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
97 additions
and
1 deletion.
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,35 @@ | ||
!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 decorator_main | ||
use decorator_module | ||
implicit none | ||
|
||
class(shape), allocatable :: circle_shape | ||
class(shape), allocatable :: rectangle_shape | ||
class(shape), allocatable :: redCircle_shape | ||
class(shape), allocatable :: redRectangle_shape | ||
type(circle) :: theCircle | ||
|
||
allocate(circle_shape, source=theCircle) | ||
print *, 'Circle with normal border:' | ||
call circle_shape%draw | ||
allocate(redCircle_shape, source=redShapeDecorator(circle_shape)) | ||
print *, 'Circle with red border:' | ||
call redCircle_shape%draw | ||
allocate(rectangle_shape, source=rectangle()) | ||
allocate(redRectangle_shape, source = redShapeDecorator(rectangle_shape)) | ||
print *, 'Rectangle with red border:' | ||
call redRectangle_shape%draw | ||
|
||
deallocate(circle_shape) | ||
deallocate(redCircle_shape) | ||
deallocate(redRectangle_shape) | ||
|
||
end program decorator_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,61 @@ | ||
!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 decorator | ||
module decorator_module | ||
implicit none | ||
|
||
type, abstract :: shape | ||
contains | ||
procedure(drawInterface), deferred :: draw | ||
end type shape | ||
abstract interface | ||
subroutine drawInterface(this) | ||
import shape | ||
class(shape), intent(in) :: this | ||
end subroutine | ||
end interface | ||
|
||
type, extends(shape) :: rectangle | ||
contains | ||
procedure :: draw => drawRectangle | ||
end type rectangle | ||
|
||
type, extends(shape) :: circle | ||
contains | ||
procedure :: draw => drawCircle | ||
end type circle | ||
|
||
type, extends(shape), abstract :: shapeDecorator | ||
end type shapeDecorator | ||
|
||
type, extends(shapeDecorator) :: redShapeDecorator | ||
class(shape), pointer :: decoratedShape | ||
contains | ||
procedure :: draw => drawWithRedShapeDecorator | ||
end type redShapeDecorator | ||
|
||
contains | ||
|
||
subroutine drawRectangle(this) | ||
class(rectangle), intent(in) :: this | ||
print *, "Draw rectangle" | ||
end subroutine | ||
|
||
subroutine drawCircle(this) | ||
class(circle), intent(in) :: this | ||
print *, "Draw circle" | ||
end subroutine | ||
|
||
subroutine drawWithRedShapeDecorator(this) | ||
class(redShapeDecorator), intent(in) :: this | ||
call this%decoratedShape%draw | ||
print *, "Red Border" | ||
end subroutine | ||
|
||
end module decorator_module |