如何在私有构造函数中注入服务

时间:2017-09-08 08:10:52

标签: angularjs typescript dependency-injection singleton private-constructor

我有一个现有的代码,通过使用私有构造函数和返回对象实例返回对象来实现单例模式 -

export class SingletonFactory {
  private static factoryInstance = new SingletonFactory();

  private constructor() {
  }

  public static getInstance() {
    return SingletonFactory.factoryInstance;
  }
}

我需要为这个工厂注入一个依赖项。我将代码更改为以下内容 -

@Inject('MyService')
export class SingletonFactory {
  private static factoryInstance = new SingletonFactory();

  private constructor(private myService : MyService) {
  }

  public static getInstance() {
    return SingletonFactory.factoryInstance;
  }
}

请建议我如何在构造函数中创建对象时注入依赖项?

1 个答案:

答案 0 :(得分:0)

注入您的组件。

# library for plot
library(plot3D)

# setting seed and generating some data
set.seed(10)

#### storing data in matrix ####
datamatrix <- matrix(c(rnorm(500,-1,.4),rnorm(500,2,0.2),runif(500,-3,0),runif(500,0,3)),nrow=1000,ncol=2,byrow=F)

# locations of cuts
xcuts <- seq(min(datamatrix[,1]),max(datamatrix[,1]),length.out = 6)
ycuts <- seq(min(datamatrix[,2]),max(datamatrix[,2]),length.out = 6)

# calculating values for cutting
xvals <- cut(datamatrix[,1], xcuts)
yvals <- cut(datamatrix[,2], ycuts)

# initializing matrix to store count in each bin
z <- matrix(0,length(levels(yvals)),length(levels(xvals)))

for(i in 1:length(levels(xvals))){
  for(j in 1:length(levels(yvals))){
    z[j,i] <- length(intersect(which(xvals == levels(xvals)[i]),which(yvals == levels(yvals)[j])))
  }
}

#### finding labels from factors cut ####
factsx <- levels(xvals) # factsx <- levels_pc2_cut # or something like that
xlabsFacts <- rep(NA,length(factsx))

for(i in 1:(length(factsx))){

  comma_sep <- unlist(gregexpr(pattern =',',factsx[i])) # location of the comma in the factor

  #taking section of text and converting to numbers
  xlabsFacts[i] <- as.numeric(substr(factsx[i],2,comma_sep-1))
  xlabsFacts[i+1] <- as.numeric(substr(factsx[i],comma_sep+1,nchar(factsx[i])-1))

}

factsy <- levels(yvals) # factsy <- levels_pc1_cut # or something like that
ylabsFacts <- rep(NA,length(factsy))

for(i in 1:(length(factsy))){

  comma_sep <- unlist(gregexpr(pattern =',',factsy[i])) # location of the comma in the factor

  #taking section of text and converting to numbers
  ylabsFacts[i] <- as.numeric(substr(factsy[i],2,comma_sep-1)) 
  ylabsFacts[i+1] <- as.numeric(substr(factsy[i],comma_sep+1,nchar(factsy[i])-1))

}


#### formatting plot ####
# contour plot without axes
contour2D(z
          ,yaxt='n' # no y axis ticks
          ,xaxt='n' # no x axis ticks
          ,ylab='y values' # y axis label
          ,xlab='x values' # x axis label
)

# adding x axis with tick marks
axis(side=1 # bottom
     ,at=seq(0,1,length.out = length(xlabsFacts)) # change 6 to number of tick marks you want
     ,labels=round(xlabsFacts,2) # change to labels for tick marks from your data
)

# adding x axis with tick marks
axis(side=2 # bottom
     ,at=seq(0,1,length.out = length(ylabsFacts)) # change 6 to number of tick marks you want
     ,labels=round(ylabsFacts,2) # change to labels for tick marks from your data

)
相关问题