samedi 25 juin 2016

Python api design using factory and builder pattern

I am new to python and I have to design a Python API(version - 2.7) similar to an existing Java API

Python version - 2.7

The Java API is as follows

There is a Process interface

public interface Process<T> {

  Future<T> create(Client<T> client)

}

There are two implementations of Process . For simplicity I have shown the implementation of one 1) FirstProcess
2) SecondProcess

public class FirstProcess implements Process {

   Future<T> create(Client<T> client) {

   }
}

There is a ProcessFactory

public interface ProcessFactory {

  Process createProcess()
}

There are two implementations of ProcessFactory
1) FirstProcessFactory
2) SecondProcessFactory

For simplicity I have just shown the implementation of one

public class FirstProcessFactory implements ProcessFactory {

  Process createProcess() {

     return new FirstProcess()
  }
}

There is a builder for the Process

ProcessBuilder

public class ProcessBuilder {

   // Has a lot of methods for config setup and validation

   public Process build() {

      //Based on the config uses the ProcessFactory to return 
      // either FirstProcess or SecondProcess
   }
}

I have a few questions

1) I have already read about how I could use the named parameters instead of the Builder pattern.

2) If I use the named parameters how I can implement the Factory Pattern, how can I decide about instantiating FirstProcess or SecondProcess based on the config. The existing java logic is present in the build() method of ProcessBuilder and uses the ProcessFactory

3) Would it be a good python design if I have the same Java API design with both builder and the factory patterns ?

Aucun commentaire:

Enregistrer un commentaire