Skip to content

Commit

Permalink
Added decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
farhanjk committed Jan 4, 2014
1 parent 91224dd commit d39bfe6
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Tested on: GNU Fortran (GCC) 4.8.
- [X] Bridge
- [X] Composite
- [X] Criteria
- [ ] Decorator
- [X] Decorator
- [ ] Façade
- [ ] Flyweight
- [ ] Proxy
Expand Down
Binary file added structural/decorator/decorator_main
Binary file not shown.
35 changes: 35 additions & 0 deletions structural/decorator/decorator_main.f90
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
61 changes: 61 additions & 0 deletions structural/decorator/decorator_module.f90
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

0 comments on commit d39bfe6

Please sign in to comment.