创建静态工厂

时间:2015-02-07 23:57:13

标签: java processing

我正在创建一个控制串行连接打印机的应用程序,我想提供一个静态工厂方法PrinterLocator.FindPrinters(),它将返回连接到我系统的可用打印机数组。

例如,串行库提供Serial.list()这是一个静态方法,返回与我系统中可用端口相对应的字符串数组。我正在尝试创建类似的东西,但我收到错误"没有封闭的SLPDriver类型的实例可访问。必须使用SLPDriver"

类型的封闭实例限定分配

实现此设计模式的正确方法是什么?

SLPDriver:

SerialPrinter myPrinter;


void setup()
{
  SerialPrinter[] availablePrinters = PrinterLocator.FindPrinters();
  if(availablePrinters.length > 0)
  {
    myPrinter = availablePrinters[0];
  }
}

void draw()
{
}

SerialPrinter:

import processing.serial.*;

static class PrinterLocator
{
  static final int baudRates[] = {2400, 4800, 9600, 19200, 38400, 57600, 115200};
  static final int baudCount = baudRates.length;

  static SerialPrinter[] FindPrinters()
  {
    SerialPrinter[] foundPrinters, tempPrinters;
    foundPrinters = new SerialPrinter[0];
    String[] foundPorts = Serial.list();
    int numPorts = foundPorts.length;
    int numPrintersFound = 0;
    if(numPorts <= 0) return foundPrinters;

    SerialPrinter testPrinter;

    tempPrinters = new SerialPrinter[numPorts];
    for(int i = 0; i < numPorts; i++)
    {
      for(int b = 0; b < baudCount; b++)
      {
        testPrinter = new SerialPrinter("test", foundPorts[i], baudRates[b]);
        if(testPrinter.IsValid())
        {
          tempPrinters[numPrintersFound] = testPrinter;
          numPrintersFound++;
          break;
        } 
      }
    }

    if(numPrintersFound > 0)
    {
      foundPrinters = new SerialPrinter[numPrintersFound];
      for(int i = 0; i < numPrintersFound; i++)
      {
        foundPrinters[i] = tempPrinters[i];
      }
    }

    return foundPrinters;
  }

}

class SerialPrinter
{
  //Members
  private Serial myPort;
  private String printerName;
  private boolean valid;
  private String portName;
  private int baudRate;

  public SerialPrinter()
  {
    this("","",0);  
  }

  public SerialPrinter(String name, String port, int baud)
  {
    printerName = name;
    Configure(port, baud);
  }

  public boolean IsValid()
  {
    return valid;
  }

  public boolean Configure(String port, int baud)
  {
    print("Configuring Printer ");
    print(port);
    print("@");
    print(baud);
    print(": ");
    try
    {
      myPort.stop();
      myPort = null;
    }
    catch (Exception e) {}

    portName = port;
    baudRate = baud;
    try
    {
      myPort = new Serial(this, port, baud);
      myPort.clear();
      myPort.write(0xA5);
      int timeout = millis() + 1000;
      while((millis() < timeout) && (myPort.available() == 0)) { }
      if(myPort.available() > 0)
      {
        int inByte = myPort.read();
        if(inByte == 0xC9)
        {
          valid = true;
        }
        else
        {
          valid = false;
        }
      }
      else
      {
        valid = false;
      }   
    }
    catch (Exception e)
    {
      valid = false;
    }
    if(valid)
    {
      println("[OK]");
    }
    else
    {
      println("[ERR]");
    }
    return valid;
  }
}

1 个答案:

答案 0 :(得分:0)

  

无法访问SLPDriver类型的封闭实例。必须使用SLPDriver类型的封闭实例来限定分配

错误表明SerialPrinterSLPDriver的内部类。

您需要将类声明更改为static:

static class SerialPrinter
{
    ...

或者,如果它应该是内在的,您需要使用SLPDriver实例来创建SerialPrinter

someSLPDriver.new SerialPrinter(...)

http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html


Serial构造函数似乎需要PApplet的实例。实例化表达式也应该给你一个错误,因为this指的是SerialPrinter,而不是PApplet

你可以尝试的东西如下:

...

static class PrinterLocator {
    ...

    static SerialPrinter[] FindPrinters(SLPDriver context) {
        ...

        ... = context.new SerialPrinter(...);
    }
}

class SerialPrinter {
    ...

    public boolean Configure(...) {
        ...

        ... = new Serial(PApplet.this, ...);
    }
}

当您在小程序的上下文中调用FindPrinters时,您需要将其称为

... = PrinterLocator.FindPrinters(this);