适配器模式
正文:https://refactoringguru.cn/design-patterns/adapter
Go代码:https://refactoringguru.cn/design-patterns/adapter/go/example
module adapter_module
implicit none
private
public :: client_type, computer_type, mac_type, windows_type, windows_adapter_type
type client_type
contains
procedure :: insert_lightning_connector_into_computer => client_type_insert_lightning_connector_into_computer
end type client_type
type, abstract :: computer_type
contains
procedure(computer_type_insert_into_lightning_port), deferred :: insert_into_lightning_port
end type computer_type
type, extends(computer_type) :: mac_type
contains
procedure :: insert_into_lightning_port => mac_type_insert_into_lightning_port
end type mac_type
type, extends(computer_type) :: windows_type
contains
procedure :: insert_into_lightning_port => windows_type_insert_into_lightning_port
end type windows_type
type, extends(computer_type) :: windows_adapter_type
type(windows_type), pointer :: windows_machine
contains
procedure :: insert_into_lightning_port => windows_adapter_type_insert_into_lightning_port
end type windows_adapter_type
abstract interface
subroutine computer_type_insert_into_lightning_port(self)
import computer_type
class(computer_type), intent(inout) :: self
end subroutine computer_type_insert_into_lightning_port
end interface
contains
subroutine client_type_insert_lightning_connector_into_computer(self, com)
class(client_type), intent(inout) :: self
class(computer_type), intent(inout) :: com
print *, "Client inserts Lightning connector into computer."
call com%insert_into_lightning_port()
end subroutine client_type_insert_lightning_connector_into_computer
subroutine mac_type_insert_into_lightning_port(self)
class(mac_type), intent(inout) :: self
print *, "Lightning connector is plugged into mac machine."
end subroutine mac_type_insert_into_lightning_port
subroutine windows_type_insert_into_lightning_port(self)
class(windows_type), intent(inout) :: self
print *, "USB connector is plugged into windows machine."
end subroutine windows_type_insert_into_lightning_port
subroutine windows_adapter_type_insert_into_lightning_port(self)
class(windows_adapter_type), intent(inout) :: self
print *, "Adapter converts Lightning signal to USB."
call self%windows_machine%insert_into_lightning_port()
end subroutine windows_adapter_type_insert_into_lightning_port
end module adapter_module
program adapter_main
use adapter_module, only: client_type, computer_type, mac_type, windows_type, windows_adapter_type
implicit none
type(client_type) :: client
type(mac_type) :: mac
type(windows_type), target :: windows
type(windows_adapter_type) :: windows_adapter
call client%insert_lightning_connector_into_computer(mac)
windows_adapter%windows_machine => windows
call client%insert_lightning_connector_into_computer(windows_adapter)
end program adapter_main
!> Results shall be:
! Client inserts Lightning connector into computer.
! Lightning connector is plugged into mac machine.
! Client inserts Lightning connector into computer.
! Adapter converts Lightning signal to USB.
! USB connector is plugged into windows machine.