Java assignment help
Constructing the Service Layer
© REGIS university
1
CS444 – Software Engineering
1
The Context
Consider an application that maintains a list of contacts
The application allows us to create, retrieve, update and delete Contact domain objects, where a Contact contains the following properties
firstName : String
lastName : String
email : String
phoneNum : String
The following slides illustrate how to create a service that facilitates the above CRUD operations: create, retrieve, update and delete
2
The Context
The UML diagram of the My Contacts domain layer
3
Account
setters, getters
validate
id
firstName
lastName
login: Login
contacts: List<Contact>
Login
setters, getters
validate
id
username
password
Contact
setters, getters
validate
id
firstName
lastName
phone
has a
has a
The Service Layer
The Service Layer contains the services the application requires
Service: a capability, such as persistence, that allows us to perform the CRUD operations
Initial/preliminary UML diagram that allows us to persist a user’s Account and their list of Contacts
4
Account Service
create
retrieveAll
update
delete
Account
Login
Contact
has a
has a
uses
The Separated Interface Pattern
Application services are typically created using the Separated Interface Pattern, that consists of
An interface that captures the “what”, i.e., what operations are allowed
A class implementation that captures the ”how”, implements the operations specified in the interface
5
Interface name
method signatures
Impl name
method implementations
implements
Separated Interface – UML Example
6
IAccountSvc
create(Account account) : Account
retrieveAll() : List<Account>
update(Account account) : Account
delete (Account account) : Account
AccountSvcImpl
implements
create(Account account) : Account
retrieveAll() : List<Account>
update(Account account) : Account
delete (Account account) : Account
Method signatures
Method implementations
A Java interface
A Java class
One Interface, Multiple Implementations
Note: a given interface can have multiple implementation classes that provide alternative implementations
7
IAccountSvc
create(Account account) : Account
retrieveAll() : List<Account>
update(Account account) : Account
delete (Account account) : Account
AccountSvcCacheImpl
implements
create(Account account) : Account
retrieveAll() : List<Account>
update(Account account) : Account
delete (Account account) : Account
AccountSvcFileImpl
create(Account account) : Account
retrieveAll() : List<Account>
update(Account account) : Account
delete (Account account) : Account
AccountSvcJDBCImpl
create(Account account) : Account
retrieveAll() : List<Account>
update(Account account) : Account
delete (Account account) : Account
Our Implementation: a Memory Cache
The service example illustrated on the following pages is a cache implementation
Cache implementation is an in-memory cache that will work as long as the app is up and running, but not if the application is restarted
Technique: In NetBeans, create:
A service package to host the Account service
Use the naming convention CS444<last name>.service
The Account Service in the Services package that includes:
A service interface
A service cache implementation
A unit test for the service cache implementation class
8
Create the Service Package
Right click on your application package (e.g., cs444)
Choose New -> Java Package
Name the new package: <project name>.service
Example: CS444Hemmes.service
9
Create the “service” package
Your project structure should look like this
There should be a Java package corresponding to each layer in the software architecture, with a consistent naming convention among the layers
10
new nested service package
Creating Services
Create an Account service for the CRUD operations of the application
In particular, the service layer requires
The service interface
The service class implementation that implements the operations in the service interface
11
Create the Account Svc Interface
12
1. right click the service package
2. New
3. Java Interface
Interface: IAccountSvc
Name the interface IAccountSvc
13
New Java Interface
New Java Interface
Interface: IAccountSvc
Add the imports and the CRUD method signatures
14
imports for visibility
Signatures of CRUD operations (for Account objects)
Create the Account Svc Implementation
Create a new Java class and name it AccountSvcCacheImpl
15
1. right click the service package
2. New
3. Java Class
AccountSvcCacheImpl
16
imports for visibility
implements the interface
the cache
stubbed out CRUD methods
AccountSvcCacheImpl
Update the create method
17
Unit Test
We create a unit test for the cache implementation
In the navigation pane, right click on the impl class
choose: Tools -> Create / Update Tests
18
1. uncheck
2. uncheck
3. OK
AccountSvcTest
The test is created with a stubbed out method: testSomeMethod()
Update the test method
19
AccountSvcTest
Rename the test method to be: testCRUD()
Enter the code for the test (initially just for the create method)
Run the test
20
Run the Test
Right click on the test, and choose either:
Run File
Test File
The test output window displays the results
21
Method: update(Account account)
22
Method: delete(Account account)
23
Updated Unit Test
24
Updated Unit Test
25
Run the Test
Right click on the test, and choose either …
Run File
Test File
The test output window displays the results
26