17 Stubs The Problem A Developer has been
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: - 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. 2
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. 3
Example Consider a class called Data. Consumer that processes data obtained by a class called Data. Store, a class that connects to a database to obtain records. Data. Consumer is coded by Developer A, while Data. Store is coded by Developer B. package com. jds. processor; import com. jds. dao. Data. Store; public class Data. Processor{ public Collection process (Data. Store store, String data. Key){ List temp. Collection = new Array. List(); Object temp. Object = (Object) store. get. Data(data. Key); //… // Process the data //… temp. Collection. add(temp. Object); return (Collection)temp. Collection; } } 4
The Solution • The Data. Processor class has code that relies on the behavior of another class: Object temp. Object = (Object) store. get. Data(data. Key); • Researching the API reveals that the method get. Data(String) of class Data. Store reveals the following information: /** Retrieves the data specified by the argument @param data. Key the key representing the data stored in this instance @returns the data specified by the argument */ public Object get. Data(String data. Key) 5
Creating the Stub //This is a package for stub classes only package com. jds. dao. stub; public class Data. Store{ //This is a package for stub classes only public Object get. Data(String data. Key){ if(data. Key. equals(“name”) return “Test Name”; if(data. Key. equals(“id”) return “ID 000001”; } //Other methods } 6
Implementing the Stub The stub is now ready for use. For the Data. Processor 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. Data. Store; import com. jds. dao. stub. Data. Store; //Use stub implementation class Data. Processor{ //… } 7
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. 8
- Slides: 8