5 ! Contributed by jilfa12@yahoo.com
12 CHARACTER(len
=20) :: factory_type
!! Descriptive name for database
13 CLASS(Connection
), POINTER :: connection_type
!! Which type of database ?
14 CONTAINS !! Note 'class' not 'type' !
15 PROCEDURE
:: init
!! Constructor
16 PROCEDURE
:: create_connection
!! Connect to database
17 PROCEDURE
:: finalize
!! Destructor
20 TYPE, ABSTRACT
:: Connection
22 PROCEDURE(generic_desc
), DEFERRED
, PASS(self
) :: description
26 SUBROUTINE generic_desc(self
)
28 CLASS(Connection
), INTENT(in
) :: self
29 END SUBROUTINE generic_desc
32 !! An Oracle connection
33 TYPE, EXTENDS(Connection
) :: OracleConnection
35 PROCEDURE
, PASS(self
) :: description
=> oracle_desc
36 END TYPE OracleConnection
39 TYPE, EXTENDS(Connection
) :: MySQLConnection
41 PROCEDURE
, PASS(self
) :: description
=> mysql_desc
42 END TYPE MySQLConnection
46 SUBROUTINE init(self
, string
)
47 CLASS(CFactory
), INTENT(inout
) :: self
48 CHARACTER(len
=*), INTENT(in
) :: string
49 self
%factory_type
= TRIM(string
)
50 self
%connection_type
=> NULL() !! pointer is nullified
53 SUBROUTINE finalize(self
)
54 CLASS(CFactory
), INTENT(inout
) :: self
55 DEALLOCATE(self
%connection_type
) !! Free the memory
56 NULLIFY(self
%connection_type
)
57 END SUBROUTINE finalize
59 FUNCTION create_connection(self
) RESULT(ptr
)
60 CLASS(CFactory
) :: self
61 CLASS(Connection
), POINTER :: ptr
63 IF(self
%factory_type
== "Oracle") THEN
64 IF(ASSOCIATED(self
%connection_type
)) DEALLOCATE(self
%connection_type
)
65 ALLOCATE(OracleConnection
:: self
%connection_type
)
66 ptr
=> self
%connection_type
67 ELSEIF(self
%factory_type
== "MySQL") THEN
68 IF(ASSOCIATED(self
%connection_type
)) DEALLOCATE(self
%connection_type
)
69 ALLOCATE(MySQLConnection
:: self
%connection_type
)
70 ptr
=> self
%connection_type
73 END FUNCTION create_connection
75 SUBROUTINE oracle_desc(self
)
76 CLASS(OracleConnection
), INTENT(in
) :: self
77 WRITE(*,'(A)') "You are now connected with Oracle"
78 END SUBROUTINE oracle_desc
80 SUBROUTINE mysql_desc(self
)
81 CLASS(MySQLConnection
), INTENT(in
) :: self
82 WRITE(*,'(A)') "You are now connected with MySQL"
83 END SUBROUTINE mysql_desc
92 TYPE(CFactory
) :: factory
93 CLASS(Connection
), POINTER :: db_connect
=> NULL()
95 CALL factory
%init("Oracle")
96 db_connect
=> factory
%create_connection() !! Create Oracle DB
97 CALL db_connect
%description()
99 !! The same factory can be used to create different connections
100 CALL factory
%init("MySQL") !! Create MySQL DB
102 !! 'connect' is a 'class' pointer. So can be used for either Oracle or MySQL
103 db_connect
=> factory
%create_connection()
104 CALL db_connect
%description()
106 CALL factory
%finalize() ! Destroy the object