BAEL-3091: The Prototype Pattern in Java (changed code based on valid comments from a reader)

This commit is contained in:
Vivek Balasubramaniam
2019-10-29 22:27:15 +05:30
parent db85c8f275
commit d3d5b060e7
20517 changed files with 1642290 additions and 0 deletions
@@ -0,0 +1,30 @@
package com.baeldung.petstore.app;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baeldung.petstore.client.api.PetApi;
import com.baeldung.petstore.client.model.Pet;
@Service
public class DefaultPetService implements PetService {
@Autowired
private PetApi petApi;
public List<Pet> findAvailablePets() {
return petApi.findPetsByStatus(Arrays.asList("available"));
}
public List<Pet> findPendingPets() {
return petApi.findPetsByStatus(Arrays.asList("pending"));
}
public List<Pet> findSoldPets() {
return petApi.findPetsByStatus(Arrays.asList("sold"));
}
}
@@ -0,0 +1,26 @@
package com.baeldung.petstore.app;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class PetController {
@Autowired
private PetService petService;
@RequestMapping("/pets")
@ResponseBody
public String listAvailablePets() {
StringBuilder sb = new StringBuilder("<h1>Available pets:</h1>");
sb.append("<ul>");
petService.findAvailablePets()
.forEach( p -> sb.append("<li>" + p.getName() + "</li>"));
sb.append("</ul>");
return sb.toString();
}
}
@@ -0,0 +1,27 @@
package com.baeldung.petstore.app;
import java.util.List;
import com.baeldung.petstore.client.model.Pet;
public interface PetService {
/**
* Find available pets
* @return List of available pets
*/
List<Pet> findAvailablePets();
/**
* Find Pending pets
* @return List of pending pets
*/
List<Pet> findPendingPets();
/**
* Find sold pets
* @return List of sold pets
*/
List<Pet> findSoldPets();
}
@@ -0,0 +1,15 @@
package com.baeldung.petstore.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@Import(PetStoreIntegrationConfig.class)
public class PetStoreApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(PetStoreApplication.class, args);
}
}
@@ -0,0 +1,23 @@
package com.baeldung.petstore.app;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baeldung.petstore.client.api.PetApi;
import com.baeldung.petstore.client.invoker.ApiClient;
@Configuration
public class PetStoreIntegrationConfig {
@Bean
public PetApi petpi() {
return new PetApi(apiClient());
}
@Bean
public ApiClient apiClient() {
ApiClient apiClient = new ApiClient();
return apiClient;
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -0,0 +1,17 @@
package org.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.petstore.app.PetStoreApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PetStoreApplication.class)
public class SpringContextIntegrationTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}
@@ -0,0 +1,17 @@
package org.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.petstore.app.PetStoreApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PetStoreApplication.class)
public class SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}