80 likes | 241 Views
17 Stubs. The Problem. A Developer has been given the task of coding the implementation of a class according to the specifications assigned. The specifications of the class assigned requires reference to several other classes, however the following issues arise:
E N D
The Problem • A Developer has been given the task of coding the implementation of a class according to the specifications assigned. • The specifications of the class assigned requires reference to several other classes, however the following issues arise: • The actual implementation of these classes are to be obtained dynamically from a remote source. • The implementation of these classes relies on previously written, native legacy code. • Another developer is assigned the task of implementing these classes and is currently unfinished.
Solution: Stubs A stub is a skeleton, temporary substitute or proxy of the actual implementation of a class. A class stub usually defines the general public interfaces of a class.
Example Consider a class called DataConsumer that processes data obtained by a class called DataStore, a class that connects to a database to obtain records. DataConsumer is coded by Developer A, while DataStore is coded by Developer B. package com.jds.processor; import com.jds.dao.DataStore; public class DataProcessor{ public Collection process (DataStore store, String dataKey){ List tempCollection = new ArrayList(); Object tempObject = (Object) store.getData(dataKey); //… // Process the data //… tempCollection.add(tempObject); return (Collection)tempCollection; } }
The Solution • The DataProcessor class has code that relies on the behavior of another class: Object tempObject = (Object) store.getData(dataKey); • Researching the API reveals that the method getData(String) of class DataStore reveals the following information: /** Retrieves the data specified by the argument @param dataKey the key representing the data stored in this instance @returns the data specified by the argument */ public Object getData(String dataKey)
Creating the Stub //This is a package for stub classes only package com.jds.dao.stub; public class DataStore{ //This is a package for stub classes only public Object getData(String dataKey){ if(dataKey.equals(“name”) return “Test Name”; if(dataKey.equals(“id”) return “ID000001”; } //Other methods }
Implementing the Stub The stub is now ready for use. For the DataProcessor class to make use of the stub, all it needs is a simple change in the import declaration: package com.jds.processor; // import com.jds.dao.DataStore; import com.jds.dao.stub.DataStore; //Use stub implementation class DataProcessor{ //… }
Key Points By using stubs, one developer need not be dependent on the progress of another developer. What is important here is that both developers need to abide by the contract design of their respective classes. There is a great deal of reliance here on the documented definitions of each class. For a team working on object oriented development to work, the public design contract for each class must be strictly adhered to. That way, a developer can work on a class with complete confidence that when the components are integrated, they will all work as designed and intended.