Question: Instructions Create a simple server that uses simple design patterns Singleton: Wrap your main server instance as a singleton class Factory: Your server should parse

Instructions

Create a simple server that uses simple design patterns

  • Singleton: Wrap your main server instance as a singleton class

  • Factory: Your server should parse the routes and create a new Processor class depending on the route

  • Builder: The process method of your Processor should use the Builder pattern to create a response.

  • Lazy Loading: Lazy load users data on the first fetch.

Final response should be converted to Json

First parse url, then use factory to instantiate correct Processor class.

Processor Class

  • Has a process() method that returns a Response object

  • Add a subclass per endpoint

  • Takes a hashmap of key value args

  • User a factory to create these, based on the endpoint

Response Class

  • This class gets stringified

  • Needs a ResponseBuilder helper

  • Needs Date

  • Needs Params Map (String, String)

  • Needs response (Object, use Integer for ints)

  • Needs Response Code (String)

Server Class

  • Wrap simple server as a singleton, and give it a method

    • getInstance()

    • then run() to start

Endpoints

  • /math/add?a=&b=

    • return a + b

  • /math/add?a=

    • return a + 1

  • /posts?userid=

    • Single post

  • /users?userid=

    • Single userId

  • All others return the string ERROR as the response code.

Return Format

You will need a response class that looks like this before passing to JSON.

"{

"date": "2019-02-10T20:40:59.257Z", // time that this was generated

"params": {

"a": 2

},

responseCode: OK

"response": 3,

}"

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!