1 / 8

17 Stubs

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:

gad
Download Presentation

17 Stubs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 17 Stubs

  2. 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.

  3. 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.

  4. 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; } }

  5. 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)

  6. 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 }

  7. 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{ //… }

  8. 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.

More Related