This is an assignment to the Software Architecture class at the Technische Hochschule Nürnberg.
In this assignment we'll be looking at
- static and regular inner classes
- anonymous classes
- lambda expressions and
@FunctionalInterfaceby implementing a basic linked list.
A linked list consists of a controller (the list class) and elements that contain the actual payload (the container or element class).
The controller class maintains a reference to the first list element (head), and every element points to its successor (or to a null value, if it's the last).
The following UML shows all classes of this assignment (including the interfaces java.util.Iterator<T> and java.lang.Iterable<T>; we'll cover ).
Note: the concept of an iterator will be discussed in detail in a few weeks.
- Create a fork of this repository.
- Clone your fork to get a local working copy.
- Import the project to your IDE (choose Gradle project)
- What is a static class?
- Refresh your knowledge on UML (e.g. here)
- What's the difference between a regular inner and static inner class?
- Can you think of some use cases for both?
- Implement
Elementas static inner class ofSimpleListImpl.- Why is this class static, and ideally
private?- So that it can exist without its outer Wrapping class (copy to another list?). Private: So that you can't fool around and misuse the List-Structure
- Why is this class static, and ideally
- Implement the
Iteratorinterface as inner class ofSimpleListImpl.- Why is it helpful to make this class non-static?
- it has to access the instance-values during runtime
- Why is it helpful to make this class non-static?
- Add the
Iterableinterface to yourSimpleListImpl, and implement the required methods.- Why is implementing the
Iterableinterface essential for a (good) list implementation? (Hint: Check the test cases!)- to make it foreach-able
- Are there any language definition constraints to this?
- don't know
- Why is implementing the
-
Implement the
filtermethod for yourSimpleListImplclass (seeSimpleFilterinterface). -
Check the given test suite for an example on
- how to use an anonymous class with an interface.
- how an anonymous class can be replaced by a lambda expression.
-
Add some test methods and implement another filter logic (e.g. every third number, or any number smaller than a certain value).
-
Review anonymous classes and lambdas.
- Lambda expressions look very convenient; can you think of a scenario where they should not be used?
- Where Names are clearer for understanding the code, Where they are too long, Where they are reused often and Where they are Nested
- Recall how scoping works for anonymous (inner or local) classes; can you think of a scenario where to avoid them?
- They have access to every
- Lambda expressions look very convenient; can you think of a scenario where they should not be used?
-
Fragen
- Warum keine final static class? -RIP, nicht gehört