-
Notifications
You must be signed in to change notification settings - Fork 2
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
7 changed files
with
113 additions
and
4 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
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
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
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,13 @@ | ||
# 策略模式 | ||
|
||
正文:https://refactoringguru.cn/design-patterns/strategy | ||
|
||
Go代码:https://refactoringguru.cn/design-patterns/strategy/go/example | ||
|
||
```fortran | ||
{{#include ../../../src/behavioral/strategy/strategy_module.f90}} | ||
``` | ||
|
||
```fortran | ||
{{#include ../../../src/behavioral/strategy/strategy_main.f90}} | ||
``` |
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
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,20 @@ | ||
program strategy_main | ||
|
||
use strategy_module, only: add_t, sub_t, calculator_t | ||
implicit none | ||
type(add_t) :: add | ||
type(sub_t) :: sub | ||
type(calculator_t) :: calculator | ||
|
||
call calculator%set_strategy(add) | ||
print *, "Add:", calculator%strategy%calc(1, 1) | ||
|
||
call calculator%set_strategy(sub) | ||
print *, "Sub:", calculator%strategy%calc(1, 1) | ||
|
||
end program strategy_main | ||
|
||
!> Results shall be: | ||
|
||
! Add: 2 | ||
! Sub: 0 |
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,64 @@ | ||
module strategy_module | ||
|
||
implicit none | ||
private | ||
|
||
public :: add_t, sub_t, calculator_t | ||
|
||
type, abstract :: strategy_t | ||
contains | ||
procedure(strategy_t_calc), deferred :: calc | ||
end type strategy_t | ||
|
||
abstract interface | ||
integer function strategy_t_calc(self, a, b) result(c) | ||
import strategy_t | ||
class(strategy_t), intent(inout) :: self | ||
integer, intent(in) :: a, b | ||
end function strategy_t_calc | ||
end interface | ||
|
||
type, extends(strategy_t) :: add_t | ||
contains | ||
procedure :: calc => add_t_calc | ||
end type add_t | ||
|
||
type, extends(strategy_t) :: sub_t | ||
contains | ||
procedure :: calc => sub_t_calc | ||
end type sub_t | ||
|
||
type calculator_t | ||
class(strategy_t), pointer :: strategy | ||
contains | ||
procedure :: set_strategy => calculator_t_set_strategy | ||
procedure :: get_result => calculator_t_get_result | ||
end type calculator_t | ||
|
||
contains | ||
|
||
integer function add_t_calc(self, a, b) result(c) | ||
class(add_t), intent(inout) :: self | ||
integer, intent(in) :: a, b | ||
c = a + b | ||
end function add_t_calc | ||
|
||
integer function sub_t_calc(self, a, b) result(c) | ||
class(sub_t), intent(inout) :: self | ||
integer, intent(in) :: a, b | ||
c = a - b | ||
end function sub_t_calc | ||
|
||
subroutine calculator_t_set_strategy(self, strategy) | ||
class(calculator_t), intent(inout) :: self | ||
class(strategy_t), intent(in), target :: strategy | ||
self%strategy => strategy | ||
end subroutine calculator_t_set_strategy | ||
|
||
integer function calculator_t_get_result(self, a, b) result(c) | ||
class(calculator_t), intent(inout) :: self | ||
integer, intent(in) :: a, b | ||
c = self%strategy%calc(a, b) | ||
end function calculator_t_get_result | ||
|
||
end module strategy_module |