Giter Site home page Giter Site logo

05-generics-bounds's Introduction

This is an assignment to the Software Architecture class at the Technische Hochschule Nürnberg.

Assignment 5: Generics and Bounds

This assignment covers an advanced topic of generics: bounds on type parameters and wildcards. Bounds describe type constraints of generic classes or methods that allow to balance type safety with flexibility of use.

Setup

  1. Create a fork of this repository (button in the right upper corner)
  2. Clone the project (get the link by clicking the green Clone or download button)
  3. Import the project to your IDE (remember the guide in assignment 1)
  4. Validate your environment by running the tests from your IntelliJ and by running gradle test on the command line.

Groundwork

First, create the model for this assigment:

Model spec

  1. Create the enum PlantColor
  2. Implement the abstract class Plant
  3. Implement the child classes Flower and Shrub
    Remarks:
    • A shrub is always green; a flower may be any color but green (handle a wrong value in the constructor)
    • Flowers compare to each other by height
    • All good data classes should always implement equals and hashCode, ideally also toString.
  4. Create tests to ensure that your model classes are correct (e.g. green flowers won't work)

Basic Bounds

Implement a PlantBed which manages a list of plants (use SimpleList<T>).

PlantBed spec

A PlantBed may contain any subclass of Plant but nothing else! Use appropriate bounds when declaring the generic class.

Remarks: The method getPlantsByColor is very easy to implement if you think of the filter method of the SimpleList! Remember to create tests for nearly every line you code!

Bounds on Wildcards

Last but not least we'll look at the PECS (Producer Extends Consumer Super) principle.

Utils spec

  1. Modify the map method of the SimpleList interface according to the PECS principle
  2. Implement the utility method splitBedByColor in a utility class PlantBedUtility. Why should this class be declared abstract and have a private constructor?

Remark: the above UML diagram for the utility method does not include any bounds but you need some (PECS!) -- the compiler is unable to capture the types if you implement the method strictly as described in the UML!

05-generics-bounds's People

Contributors

prskr avatar sikoried avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.