Enterprise Java Development@TOPIC@
Revision: v2013-09-09Built on: 2014-03-06 23:54 EST
Copyright © 2014 jim stafford (jcstaff@apl.jhu.edu)
Abstract
This presentation introduces the concept of the business logic tier, using it to form a user facade around the capabilities of the service, and to leverage business logic interfaces and tests as a form of executable requirements.
Introduce the architectural layer responsible for external simplicity and internal coordination
At the completion of this topic, the student shall
have more understanding of:
Role of business logic
How business logic can provide top-down requirements
How business logic tests can provide executable requirement verification
be able to:
Define business logic interface
Define business logic class
Distinguish business logic from data access concerns
Write a white-box test to act as an acceptance test
Potential tight coupling between clients and complex business objects
Too many method invocations between clients and business objects
Business methods exposed for misuse by clients
Hide complex interactions behind a simple client interface
Reduce the number of business objects exposed to the client across the network
Hide implementation, interactions, and dependencies of business components
Use a business logic class to encapsulate the interactions required with the business objects
Simplifies complex systems
May appear to be a no value pass-thru in simple systems
Should involve more than one business object per facade
Should have more than one facade per system
Decouples the business objects from being aware of one another
Improves perceived network performance for remote interfaces
Centralizes security and transactions in some cases
/**
* Purchasing handles payment of purchased products.
*/
public interface Purchasing {
/**
* Creates an account for the user to use in purchasing products.
* @param email
* @param firstName
* @param lastName
* @return
*/
Account createAccount(String email, String firstName, String lastName);
/**
* Completes the purchase of the items in the user's shopping cart,
* empties the cart, and returns the total cost paid.<p/>
*
* Note that this capability is not yet fully defined.
* @param email
* @param password
* @return
*/
double checkout(String email, String password);
}
/**
* The catalog maintains a view of the inventory known to our application.
*/
public interface Catalog {
/**
* Returns a list of products in the catalog chunked into page sizes.
* @param offset
* @param limit
* @return
*/
List<Product> getProducts(int offset, int limit);
/**
* Adds the selected product to the users' shopping cart and returns
* the count of items.
* @param id
* @param validEmail
*/
int addToCart(int id, String validEmail);
}
@Entity
@NamedQueries({
@NamedQuery(name="blPurchasing.findAccountByEmail",
query="select a from Account a where a.email=:email")
})
public class Account {
public static final String FIND_BY_EMAIL="blPurchasing.findAccountByEmail";
@Id @GeneratedValue
private int id;
@Column(nullable=false, unique=true)
private String email;
@Column(nullable=false)
private String password;
@Column(nullable=false)
private String firstName;
@Column(nullable=false)
private String lastName;
...
@Entity
public class Product {
@Id @GeneratedValue
private int id;
@Column(nullable=false)
private String name;
@Column(nullable=false)
private double price;
@Column(nullable=false)
private int count;
...
@Entity
public class Cart {
@Id
private String email;
@OneToOne
@PrimaryKeyJoinColumn(referencedColumnName="email")
private Account account;
@ManyToMany
private List<Product> products = new ArrayList<Product>();
...
Test-first approach
/**
* A user shall be able to establish an account with just a first
* and last name and a unique email address.
*/
@Test
public void establishAccount() {
log.info("*** establishAccount ***");
//the user will supply their email address, first and last name
String email="jharb@ravens.com";
String firstName="john";
String lastName="harbaugh";
Account account = purchasing.createAccount(email, firstName, lastName);
//they will get back a generated password to use as a login for the account
assertNotNull("no account returned", account);
assertNotNull("no password assigned", account.getPassword());
}
/**
* A user shall be able browse products in the catalog.
*/
@Test
public void browseCatalog() {
log.info("*** browseCatalog ***");
//the user will ask for product summaries in pages
int pageSize=10;
int offset=0;
List<Product> products = catalog.getProducts(offset, pageSize);
//they will receive <= a page size of product information
assertNotNull("no products returned", products);
assertTrue("no products provided", products.size() > 0);
//they can page thru the entire set
for (int i=0; products.size() != 0; i++) {
offset += products.size();
products = catalog.getProducts(offset, pageSize);
assertTrue("this catalog never ends!!!", i<100);
}
}
/**
* A user shall be able to purchase a product in the catalog.
*/
@Test
public void purchaseProduct() {
log.info("*** purchaseProduct ***");
//the user selects a product
Product product=null;
Random random=new Random();
for (int i=0; product == null; i++) {
List<Product> products=catalog.getProducts(random.nextInt(100), 1);
product=products.iterator().next();
assertTrue("I can't find anything to buy!!!", i<1000);
}
//the user adds the product to their shopping cart by providing the
//product id and their credentials
int count=catalog.addToCart(product.getId(), validEmail);
//the user receives a count of the items in the cart
assertEquals("somebody tweeked my cart!!!!", 1, count);
//the user checks out with the cashier -- payment not yet implemented
double total=purchasing.checkout(validEmail, validPassword);
//the user gets a total amount back as their receipt
assertEquals("price doesn't add up", product.getPrice(), total, .01);
}
|-- pom.xml
`-- src
|-- main
| |-- java
| | `-- ejava
| | `-- examples
| | `-- blpurchase
| | |-- bo
| | | |-- Account.java
| | | |-- Cart.java
| | | `-- Product.java
| | |-- bl
| | | |-- Catalog.java
| | | `-- Purchasing.java
| | `-- blimpl
| | |-- CatalogImpl.java
| | `-- PurchasingImpl.java
| `-- resources
`-- test
|-- java
| `-- ejava
| `-- examples
| `-- blpurchase
| `-- bl
| |-- PurchaseTest.java
| `-- PurchasingFactory.java
`-- resources
|-- hibernate.properties
|-- log4j.xml
`-- META-INF
`-- persistence.xml