Question
Instructions Create a simple server that uses simple design patterns written in JAVA Singleton : Wrap your main server instance as a singleton class Factory
Instructions
Create a simple server that uses simple design patterns written in JAVA
-
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,
}"
Data.json file
{ "users":[ { "username" : "rey", "userid" : 0, "myKey" : "myValue" }, { "username" : "lukeSkywalker", "userid" : 1 }, { "username" : "daenerysStormborn", "userid" : 2 }, { "username" : "khalDrogo", "userid" : 3 } ], "posts":[ { "postid":0, "userid": 1, "data": "I lost my lightsaber, anyone seen it?" }, { "postid":1, "userid": 0, "data": "Found lightsaber, looking for owner" }, { "postid":3, "userid": 1, "data": "Guess How many dragons I have" } ], "comments":[ { "postid":0, "userid": 1, "data": "I think I found it!" }, { "postid":0, "userid": 0, "data": "Wow thanks, maybe you can be a Jedi now!" }, { "postid":2, "userid": 0, "data": "None?" }, { "postid":2, "userid": 3, "data": "8?" } ] }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started