1

I have searched the web for this issue. And there is a post already with the same title, but not really describing the actual question. So I haven't found the right solution.

I'm developing very large projects. So it is absolutely necessary to split it up into several layers or components or whatever you want to call it.

Each layer exposes some functionality. For each layer it is not important how it will be implemented (since this changes oftenly on large projects), but what is important, is how we call the functionality and what it returns us => the contract!

If we have two layers one concerning the TransactionService and another the BankService, the BankService is a layer higher than the TransactionService and will use the transactionservice. The transactionService will merely do a transaction:

TransactionVO doTransaction(clientFromVO, clientToVO);

And another layer which does additional stuff.. TransactionVO is not an model class! They are value-objects so the implementation of the service is not exposed to outside the layer. In java we can achieve this by:

public interface TransactionService {
   TransactionVO doTransaction(clientFromVO, clientToVO);
}

The implementation would be like:

public class TransactionServiceImpl {
   public TransactionVO doTransaction(clientFromVO, clientToVO) { 
     // implementation 
   }
}

Because we define our interfaces up front, we have a contract which states how each service will look like (without having an actual implementation, the TransactionServiceImpl doesn't need to exist yet).

Therefor 2 teams can work simultaneously on each layer and they can mock the implementation. I wonder how to achieve this in Django! Creating a REST api for each layer is simpily not done (to much overhead, performance issues..) So for those who have experience on very large projects?

Fico
  • 619
  • 8
  • 12

2 Answers2

1

Python does not have "interfaces" in the same sense as Java. Instead, there is "duck typing" - where the "interface" is implied by the exposed behavior. The answers in this question ( How to handle "duck typing" in Python? ) should address some of the issues around this.

With a dynamic language like Python, if you want to define the behavior of an class before it is implemented, you write a test case in advance. If you are new to test driven development in Python, you can start by reading this.

There are a number of mocking libraries available for Python. This is an example.

Community
  • 1
  • 1
Ngure Nyaga
  • 2,989
  • 1
  • 20
  • 30
  • 1
    It's to difficult to describe all of the advantages of interface-programming here. But the duck typing isn't a solution. For instance, the DragonServiceImpl could provide a method fly(). The YellowDuckServiceImpl and WhiteDuckServiceImpl also provide such method fly(). But a dragon is not a duck! Anyways it's to complex to describe all the advantages of API or interface-based programming here. It seems to me there is no perfect solution in python. But there i a module which might adress a part of the problem I'm looking into right now: http://docs.python.org/2/library/abc.html – Fico Oct 30 '12 at 09:31
0

It seems python does not provide this out of the box. But there is something called abc module:

I quote from http://www.doughellmann.com/PyMOTW/abc/ "By defining an abstract base class, you can define a common API for a set of subclasses. This capability is especially useful in situations where a third-party is going to provide implementations..."

It's not a perfect solution but it provides some of the things I need. The following comment has let me to this: Java abstract/interface design in Python therefor it's worth mentioning ;)

Community
  • 1
  • 1
Fico
  • 619
  • 8
  • 12
  • 1
    Perhaps you should stop trying to write Java in Python? – Burhan Khalid Oct 30 '12 at 09:40
  • I understand your argument. But it's not because you don't understand the need for this approach that it is a bad approach. If it was, not so many books would have been written mentioning interface-based or API based programming. That being said, you understanding the need for such an approach is irrelevant, so questioning the approach doesn't bring any-one with a similar problem anywhere closer to a solution. – Fico Oct 30 '12 at 12:56