您在這裡

Code Template

26 七月, 2019 - 09:51
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at http://cnx.org/contents/402b20ad-c01f-45f1-9743-05eadb1f710e@37.6

Whenever we want the IList to perform a task, we add a method to IList and write appropriate concrete implementations in MTList and NEList. The following table illustrates the code template for writing the concrete code in MTList and NEList.

Table 3.3 Interpreter Pattern coding template for lists
interface IList
public abstract returnType methodName(parameter list); // returnType may be 'void'
MTList
// no data
NEList
Object _first;
IList _rest;
public returnType methodName(param list) {
/*
This is in general the base case of a recursive call. Write the (non-recursive)
code to solve the problem.
*/
}
public returnType methodName(param list) {
/*
This is in general a recursive method. The code here can refer to _first and _rest, and all the methods in NEList When referencing _rest, one usually makes the recursive call: _rest.methodName(appropriate parameters).
*/
}
 

In Class Exercuses (Role-Acting)

  • Find a number in a list and return "Found it!" if the number is in the list otherwise return "Not found!"
  • Append a list B to a given list A and return a new list consisting of A and B concatenated together.