Skip to content

Commit

Permalink
type name with _type suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
zoziha committed Jul 25, 2024
1 parent 439026d commit 7343905
Show file tree
Hide file tree
Showing 52 changed files with 1,117 additions and 1,117 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2021~2022, ZUO Zhihua
Copyright (c) 2021~2024, ZUO Zhihua
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

|项目|描述|
|:-:|:-:|
|版本:|0.2.0|
|版本:|0.3.0|
|作者:|ZUO Zhihua|
|网页:|https://zoziha.github.io/Fortran-Design-Patterns/|
|版权:|Copyright (c) 2021~2023 zoziha|
|版权:|Copyright (c) 2021~2024 zoziha|

## 开始

Expand Down
8 changes: 4 additions & 4 deletions doc/src/Introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ Fortran三种编程范式:https://zhuanlan.zhihu.com/p/412243161
|:-:|:-:|:-:|:-:|
|完成|抽象工厂、生成器、工厂方法、原型、单例。|适配器、桥接、组合、装饰、外观、代理、享元。|责任链、命令、迭代器、观察者、状态、模板方法、备忘录、中介者、访问者、策略。|

#### 创建型模式
### 创建型模式

- [X] 抽象工厂
- [X] 生成器
- [X] 工厂方法
- [X] 原型
- [X] 单例

#### 结构型模式
### 结构型模式

- [X] 适配器
- [X] 桥接
Expand All @@ -35,8 +35,8 @@ Fortran三种编程范式:https://zhuanlan.zhihu.com/p/412243161
- [X] 外观
- [X] 享元
- [X] 代理
#### 行为模式

### 行为模式

- [X] 责任链
- [X] 命令
Expand Down
4 changes: 2 additions & 2 deletions fpm.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name = "Fortran-Design-Patterns"
version = "0.2.0"
version = "0.3.0"
license = "BSD-3"
maintainer = "ZUO Zhihua"
copyright = "Copyright 2021~2023 ZUO Zhihua"
copyright = "Copyright 2021~2024 ZUO Zhihua"
description = "Fortran Design Patterns"
categories = ["Demo", "OOP", "Modern Fortran"]

Expand Down
14 changes: 7 additions & 7 deletions src/behavioral/chain-of-responsibility/CoR_main.f90
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ program CoR_main

use hospital_CoR

type(cashier) :: c
type(medical) :: m
type(doctor) :: d
type(reception) :: r
type(cashier_type) :: c
type(medical_type) :: m
type(doctor_type) :: d
type(reception_type) :: r

type(patient) :: p1, p2
type(patient_type) :: p1, p2

!> Set next for departments
call m%set_next(c)
call d%set_next(m)
call r%set_next(d)

p1 = patient("abc", .true., .true., .true., .true.)
p1 = patient_type("abc", .true., .true., .true., .true.)
!> Patient visiting
print *, "> Patient `"//p1%name//"` : "
call r%execute(p1)

p2 = patient("def", .true., .false., .false., .false.)
p2 = patient_type("def", .true., .false., .false., .false.)
!> Patient visiting
print *, "> Patient `"//p2%name//"` : "
call r%execute(p2)
Expand Down
126 changes: 63 additions & 63 deletions src/behavioral/chain-of-responsibility/CoR_module.f90
Original file line number Diff line number Diff line change
Expand Up @@ -4,68 +4,68 @@ module hospital_CoR
implicit none
private

public :: patient, department, reception, doctor, medical, cashier
public :: patient_type, department_type, reception_type, doctor_type, medical_type, cashier_type

type patient
type patient_type
character(:), allocatable :: name
logical :: registration_done
logical :: doctor_check_up_done
logical :: medicine_done
logical :: payment_done
end type patient
end type patient_type

type, abstract :: department
type, abstract :: department_type
contains
procedure(execute_procedure), deferred :: execute
procedure(set_next_procedure), deferred :: set_next
end type department
end type department_type

abstract interface
subroutine execute_procedure(self, p)
import department, patient
class(department), intent(inout) :: self
type(patient), intent(inout) :: p
import department_type, patient_type
class(department_type), intent(inout) :: self
type(patient_type), intent(inout) :: p
end subroutine execute_procedure
subroutine set_next_procedure(self, next)
import department
class(department), intent(inout) :: self
class(department), intent(inout) :: next
import department_type
class(department_type), intent(inout) :: self
class(department_type), intent(inout) :: next
end subroutine set_next_procedure
end interface

type, extends(department) :: reception
class(department), pointer :: next
type, extends(department_type) :: reception_type
class(department_type), pointer :: next
contains
procedure :: execute => reception_execute
procedure :: set_next => reception_set_next
end type reception
procedure :: execute => reception_type_execute
procedure :: set_next => reception_type_set_next
end type reception_type

type, extends(department) :: doctor
class(department), pointer :: next
type, extends(department_type) :: doctor_type
class(department_type), pointer :: next
contains
procedure :: execute => doctor_execute
procedure :: set_next => doctor_set_next
end type doctor
procedure :: execute => doctor_type_execute
procedure :: set_next => doctor_type_set_next
end type doctor_type

type, extends(department) :: medical
class(department), pointer :: next
type, extends(department_type) :: medical_type
class(department_type), pointer :: next
contains
procedure :: execute => medicine_execute
procedure :: set_next => medicine_set_next
end type medical
procedure :: execute => medicine_type_execute
procedure :: set_next => medicine_type_set_next
end type medical_type

type, extends(department) :: cashier
class(department), pointer :: next
type, extends(department_type) :: cashier_type
class(department_type), pointer :: next
contains
procedure :: execute => cashier_execute
procedure :: set_next => cashier_set_next
end type cashier
procedure :: execute => cashier_type_execute
procedure :: set_next => cashier_type_set_next
end type cashier_type

contains

subroutine reception_execute(self, p)
class(reception), intent(inout) :: self
type(patient), intent(inout) :: p
subroutine reception_type_execute(self, p)
class(reception_type), intent(inout) :: self
type(patient_type), intent(inout) :: p

if (p%registration_done) then
print *, "Patient registration already done.✔️"
Expand All @@ -77,19 +77,19 @@ subroutine reception_execute(self, p)
p%registration_done = .true.
call self%next%execute(p)

end subroutine reception_execute
end subroutine reception_type_execute

subroutine reception_set_next(self, next)
class(reception), intent(inout) :: self
class(department), intent(inout) :: next
subroutine reception_type_set_next(self, next)
class(reception_type), intent(inout) :: self
class(department_type), intent(inout) :: next

allocate (self%next, source=next)

end subroutine reception_set_next
end subroutine reception_type_set_next

subroutine doctor_execute(self, p)
class(doctor), intent(inout) :: self
type(patient), intent(inout) :: p
subroutine doctor_type_execute(self, p)
class(doctor_type), intent(inout) :: self
type(patient_type), intent(inout) :: p

if (p%doctor_check_up_done) then
print *, "Doctor checkup already done.✔️"
Expand All @@ -101,19 +101,19 @@ subroutine doctor_execute(self, p)
p%doctor_check_up_done = .true.
call self%next%execute(p)

end subroutine doctor_execute
end subroutine doctor_type_execute

subroutine doctor_set_next(self, next)
class(doctor), intent(inout) :: self
class(department), intent(inout) :: next
subroutine doctor_type_set_next(self, next)
class(doctor_type), intent(inout) :: self
class(department_type), intent(inout) :: next

allocate (self%next, source=next)

end subroutine doctor_set_next
end subroutine doctor_type_set_next

subroutine medicine_execute(self, p)
class(medical), intent(inout) :: self
type(patient), intent(inout) :: p
subroutine medicine_type_execute(self, p)
class(medical_type), intent(inout) :: self
type(patient_type), intent(inout) :: p

if (p%medicine_done) then
print *, "Medicine already given to patient.✔️"
Expand All @@ -125,19 +125,19 @@ subroutine medicine_execute(self, p)
p%medicine_done = .true.
call self%next%execute(p)

end subroutine medicine_execute
end subroutine medicine_type_execute

subroutine medicine_set_next(self, next)
class(medical), intent(inout) :: self
class(department), intent(inout) :: next
subroutine medicine_type_set_next(self, next)
class(medical_type), intent(inout) :: self
class(department_type), intent(inout) :: next

allocate (self%next, source=next)

end subroutine medicine_set_next
end subroutine medicine_type_set_next

subroutine cashier_execute(self, p)
class(cashier), intent(inout) :: self
type(patient), intent(inout) :: p
subroutine cashier_type_execute(self, p)
class(cashier_type), intent(inout) :: self
type(patient_type), intent(inout) :: p

if (p%payment_done) then
print *, "Payment Done.✔️"
Expand All @@ -147,14 +147,14 @@ subroutine cashier_execute(self, p)
print *, "Cashier getting money from patient."
p%payment_done = .true.

end subroutine cashier_execute
end subroutine cashier_type_execute

subroutine cashier_set_next(self, next)
class(cashier), intent(inout) :: self
class(department), intent(inout) :: next
subroutine cashier_type_set_next(self, next)
class(cashier_type), intent(inout) :: self
class(department_type), intent(inout) :: next

allocate (self%next, source=next)

end subroutine cashier_set_next
end subroutine cashier_type_set_next

end module hospital_CoR
12 changes: 6 additions & 6 deletions src/behavioral/command/command_main.f90
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
!> Reference: https://refactoring.guru/design-patterns/command/go/example
program test_command

use command_pattern, only: tv, on_command, off_command, button
type(tv) :: t
type(on_command) :: on_c
type(off_command) :: off_c
use command_pattern, only: tv_type, on_command_type, off_command_type, button_type
type(tv_type) :: t
type(on_command_type) :: on_c
type(off_command_type) :: off_c

type(button) :: on_b
type(button) :: off_b
type(button_type) :: on_b
type(button_type) :: off_b

!> Linking
allocate (on_c%d, source=t)
Expand Down
Loading

0 comments on commit 7343905

Please sign in to comment.