有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在Delphi中会是什么样子?

我的问题与我在上一期("Problems with converting Java code to delphi")中写的一个问题有关,我仍然有问题。我在上一个问题中看到的Java代码是我的工厂类的一部分,我正试图将其转换为Delphi。问题是我有一个名为IStandardDataProvider的主接口,它包含工厂中不同类的通用方法。但是,由于一些类还包含其他方法,因此它们不是所有类都通用的。我使用从接口IStandardDataProvider继承的另一个接口。问题是,我不能让通用的工作?查看我的整个java工厂类。这在德尔福会是什么样子

public class Factory {

    private static HashMap<String, IStandardDataProvider<?>> dataproviders = null;

    @SuppressWarnings("unchecked")
    public <T extends IStandardDataProvider<?>> T GetDataProvider(String dataProviderName) {
        if (dataproviders == null)
            buildDataProviderMap();
        if (dataproviders.containsKey(dataProviderName)) {
            return (T) dataproviders.get(dataProviderName);
        } else
            return null;
    }

    private void buildDataProviderMap() {
        // Build the database connection, that will be used in all the dataproviders
        DatabaseConnectionManager dbConnection = new DatabaseConnectionManager(ConfigurationManager.getConfiguration("sqlConnectionString"));

        // Instantiate the Hashmap 
        dataproviders = new HashMap<String, IStandardDataProvider<?>>();

        // Instantiate all the dataprovider implementations, and put them into the hash map
        dataproviders.put("EventDataProvider", new LocalEventDataProviderImpl(dbConnection));
        dataproviders.put("TaskActivityDataProvider", new LocalTaskActivityDataProviderImpl(dbConnection));
    }
}

更新:好的,这是我的delphi版本,我尝试将其转换为通用版本。目前我只能访问IStandardDataProvider

type
  TFactory = class(TObject)
  private
    DataProvider: TDictionary<string, IStandardDataProvider >;
    DbConnectionManager : TDatabaseConnectionManager;
    DBConnection : TSQLConnection;
    Configuration : TConfigurationManager;

    procedure BuildDataProviderMap;

  public
    constructor Create;
    destructor Destroy; override;

    function GetDataProvider(DataProviderName: string): IStandardDataProvider;
  end;

implementation

constructor TLocalDataProviderFactory.Create;
begin
  inherited Create;
  DbConnectionManager := TDatabaseConnectionManager.create;
end;

destructor TLocalDataProviderFactory.Destroy;
begin
  inherited;
  DbConnectionManager.Free;
  DataProvider.Free;
end;

function TLocalDataProviderFactory.GetDataProvider(DataProviderName: string): IStandardDataProvider;
begin
  if not Assigned(DataProvider) then
    BuildDataProviderMap;

  if DataProvider.ContainsKey(DataProviderName) then
  begin
    Result := DataProvider.Items[DataProviderName];
  end
  else
  begin
    Result:= nil;
  end;
end;

procedure TLocalDataProviderFactory.BuildDataProviderMap;
begin
  DataProvider := TDictionary<string, IStandardDataProvider>.Create;
  Configuration := TConfigurationManager.Create;
  DBConnection := DbConnectionManager.GetConnection(Configuration.GetConfiguration('sqlConnectionString'));
  DataProvider.Add('EventDataProvider',TLocalEventDataProviderImpl.create(DBConnection) );
  DataProvider.Add('TaskActivityDataProvider',TLocalTaskActivityDataProviderImpl.create(DBConnection) );
end;

end.

共 (1) 个答案

  1. # 1 楼答案

    正如在other question中告诉您的,Delphi不像Java那样支持通配符泛型。你能得到的最接近的东西是这样的:

    type
      IStandardDataProvider<T> = interface(IInterface)
        ...
      end;
    
    type
      TLocalDataProviderFactory = class
      private
        DataProvider: TDictionary<string, IInterface>;
        DbConnectionManager : TDatabaseConnectionManager;
        DBConnection : TSQLConnection;
        Configuration : TConfigurationManager;
    
        procedure BuildDataProviderMap;
    
      public
        constructor Create;
        destructor Destroy; override;
    
        function GetDataProvider<T>(DataProviderName: string): IStandardDataProvider<T>;
      end;
    
    implementation
    
    constructor TLocalDataProviderFactory.Create;
    begin
      inherited Create;
      DbConnectionManager := TDatabaseConnectionManager.create;
    end;
    
    destructor TLocalDataProviderFactory.Destroy;
    begin
      inherited;
      DbConnectionManager.Free;
      DataProvider.Free;
    end;
    
    function TLocalDataProviderFactory.GetDataProvider<T>(DataProviderName: string): IStandardDataProvider<T>;
    begin
      if not Assigned(DataProvider) then
        BuildDataProviderMap;
    
      if DataProvider.ContainsKey(DataProviderName) then
        Result := DataProvider.Items[DataProviderName] as IStandardDataProvider<T>
      else
        Result := nil;
    end;
    
    procedure TLocalDataProviderFactory.BuildDataProviderMap;
    begin
      DataProvider := TDictionary<string, IInterface>.Create;
      Configuration := TConfigurationManager.Create;
      DBConnection := DbConnectionManager.GetConnection(Configuration.GetConfiguration('sqlConnectionString'));
      DataProvider.Add('EventDataProvider', TLocalEventDataProviderImpl.Create(DBConnection) as IInterface);
      DataProvider.Add('TaskActivityDataProvider', TLocalTaskActivityDataProviderImpl.Create(DBConnection) as IInterface);
    end;
    
    end.
    

    这真的对您没有帮助,因为您需要知道实现IStandardDataProvider<T>的具体类,以便调用GetDataProvider(),例如:

    var
      Provider: IStandardDataProvider<TUpdateTest>;
    
    Provider := Factory.GetDataProvider<TUpdateTest>('EventDataProvider');
    

    否则,不要试图让工厂本身支持泛型,而是让调用代码来处理它,例如:

    type
      IStandardDataProvider<T> = interface(IInterface)
        ...
      end;
    
      IEventDataProvider = interface(IStandardDataProvider <TUpdateTest>)
        ...
      end;
    
    type
      TLocalDataProviderFactory = class
      private
        DataProvider: TDictionary<string, IInterface>;
        DbConnectionManager : TDatabaseConnectionManager;
        DBConnection : TSQLConnection;
        Configuration : TConfigurationManager;
    
        procedure BuildDataProviderMap;
    
      public
        constructor Create;
        destructor Destroy; override;
    
        function GetDataProvider(DataProviderName: string): IInterface;
      end;
    
    implementation
    
    constructor TLocalDataProviderFactory.Create;
    begin
      inherited Create;
      DbConnectionManager := TDatabaseConnectionManager.create;
    end;
    
    destructor TLocalDataProviderFactory.Destroy;
    begin
      inherited;
      DbConnectionManager.Free;
      DataProvider.Free;
    end;
    
    function TLocalDataProviderFactory.GetDataProvider(DataProviderName: string): IInterface;
    begin
      if not Assigned(DataProvider) then
        BuildDataProviderMap;
    
      if DataProvider.ContainsKey(DataProviderName) then
        Result := DataProvider.Items[DataProviderName]
      else
        Result := nil;
    end;
    
    procedure TLocalDataProviderFactory.BuildDataProviderMap;
    begin
      DataProvider := TDictionary<string, IInterface>.Create;
      Configuration := TConfigurationManager.Create;
      DBConnection := DbConnectionManager.GetConnection(Configuration.GetConfiguration('sqlConnectionString'));
      DataProvider.Add('EventDataProvider', TLocalEventDataProviderImpl.Create(DBConnection) as IInterface);
      DataProvider.Add('TaskActivityDataProvider', TLocalTaskActivityDataProviderImpl.Create(DBConnection) as IInterface);
    end;
    
    end.
    

    var
      Provider: IEventDataProvider;
    
    Provider := Factory.GetDataProvider('EventDataProvider') as IEventDataProvider;