Skip to content

Commit

Permalink
Add strategy.
Browse files Browse the repository at this point in the history
  • Loading branch information
zoziha committed Oct 30, 2021
1 parent 70ca0b1 commit cf52a91
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

|项目|描述|
|:-:|:-:|
|版本:|0.0.21|
|版本:|0.0.22|
|作者:|左志华(zoziha)|
|网页:|https://zoziha.github.io/Fortran-Design-Patterns/|
|版权:|Copyright (c) 2021 zoziha|
Expand All @@ -19,6 +19,7 @@
### 软件依赖

- Git
- [fortran-lang/fpm](https://github.com/fortran-lang/fpm)
- [Rust](https://www.rust-lang.org/zh-CN/)
- [mdbook](https://github.com/rust-lang/mdBook)

Expand Down
5 changes: 2 additions & 3 deletions doc/src/Introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ Fortran三种编程范式:https://zhuanlan.zhihu.com/p/412243161

|状态|创建型模式|结构型模式|行为模式|
|:-:|:-:|:-:|:-:|
|完成|抽象工厂、生成器、工厂方法、原型、单例。|适配器、桥接、组合、装饰、外观、代理、享元。|责任链、命令、迭代器、观察者、状态、模板方法、备忘录、中介者、访问者。|
|未完成|||策略。|
|完成|抽象工厂、生成器、工厂方法、原型、单例。|适配器、桥接、组合、装饰、外观、代理、享元。|责任链、命令、迭代器、观察者、状态、模板方法、备忘录、中介者、访问者、策略。|

#### 创建型模式

Expand Down Expand Up @@ -46,6 +45,6 @@ Fortran三种编程范式:https://zhuanlan.zhihu.com/p/412243161
- [X] 备忘录
- [X] 观察者
- [X] 状态
- [ ] 策略
- [X] 策略
- [X] 模板方法
- [X] 访问者
1 change: 1 addition & 0 deletions doc/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- [中介者模式](behavioral/mediator.md)
- [备忘录模式](behavioral/memento.md)
- [状态模式](behavioral/state.md)
- [策略模式](behavioral/strategy.md)
- [模板方法模式](behavioral/template-method.md)
- [访问者模式](behavioral/visitor.md)

Expand Down
13 changes: 13 additions & 0 deletions doc/src/behavioral/strategy.md
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}}
```
11 changes: 11 additions & 0 deletions fpm.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
name = "Fortran-Design-Patterns"
version = "0.0.22"
license = "BSD-3"
maintainer = "左志华"
copyright = "Copyright 2021 左志华"
description = "Fortran Design Patterns"
categories = ["Demo", "OOP", "Modern Fortran"]

[library]
source-dir = "src"
Expand Down Expand Up @@ -41,6 +47,11 @@ name = "state"
source-dir = "src/behavioral/state"
main = "state_main.f90"

[[test]]
name = "strategy"
source-dir = "src/behavioral/strategy"
main = "strategy_main.f90"

[[test]]
name = "template_method"
source-dir = "src/behavioral/template-method"
Expand Down
20 changes: 20 additions & 0 deletions src/behavioral/strategy/strategy_main.f90
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
64 changes: 64 additions & 0 deletions src/behavioral/strategy/strategy_module.f90
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

0 comments on commit cf52a91

Please sign in to comment.