Business Logic
Goal
- Consider approaches for encapsulating business logic.
- Implement a manager interface.
- Implement a service interface.
Concepts
- business logic
- domain
- domain logic
- domain model
- International Standard Book Number (ISBN)
- International Standard Serial Number (ISSN)
- manager
- mediator design pattern
- service
- service layer
- sidecar file
Lesson
You've already learned that a well-written program will separate its various concerns into different sections of the code. A program's most important concern is its central functionality, its reason for being. Somewhere between accepting input and producing output, a computer program usually does something. Massaging user input so that it can be better processed is ancillary to this task. Logging is a separate issue. Even producing the output is somewhat of an afterthought; because the program may have the ability to output data in a variety of formats or suppress output altogether.
The part of a computer program that handles what the program actually does between input and output is referred to as its business logic. You saw the general layers of a multi-tiered program when you focused on the presentation layer. Business logic sits in a layer under the presentation layer and concerns itself with the core program functionality, leaving data storage to the data layer and communication with the user to the presentation layer. The business logic layer is all about doing work.
Managers
You've already seen and implemented the repository design pattern. The repository sits on edge of the data layer and provides an abstraction for serializing and deserializing individual resources. We need a similar interface to sit in the business logic layer to encapsulate the main logic of our application and talk to other components such as a repository.
One such interface might implement the mediator design pattern. The purpose of this design pattern is to help manage the interaction among various objects and types of objects. For this reason many times an interface for the mediator design pattern will be referred to as a manager. For example consider a farm with many animals. There might be a BarnyardManager
to keep track of the farm structures, along with which animals live in which parts of the farm.
Services
Another interface you will see in the business layer is one that will provide a service to the rest of the application. The implementation of the service interface(s) may in turn talk to managers in order to perform their services.
We can make a service for taking care of tasks on a farm.
The implementation of this service will likely use the BarnyardManager
.
Review
Summary
- TODO
Gotchas
- TODO
In the Real World
- TODO
Think About It
- TODO
Self Evaluation
- TODO
Task
The marketing department contacts your team to request additional functionality in the Booker application. Until now Booker application has been tracking books
in the sense of historical written works, not as individual titles each sold by a particular publisher. The work 紅樓夢/红楼梦 (Dream of the Red Chamber), for example, is available from many publishers in various editions, even translated into different languages. Your company's executives now wish to make individual titles for sale. Booker's domain model will need to be updated, and new business logic added.
Domain Model
Each individual book release is assigned an International Standard Book Number (ISBN) to identify the title commercially. Similarly periodicals are issued an International Standard Serial Number (ISSN) for identification. Booker so far has identified historical works merely by title; you must now update the domain model to capture an ISBN or ISSN identifier.
- Add support for individual title identification.
- Add an ISBN property to your book interface/class. For the snapshot books, use an appropriate ISBN from any publisher.
- Find at least one book that has multiple ISBNs (such as from multiple publishers) but the same title, and add it to the snapshot repository.
- Add an ISSN property to your periodical interface/class. Determine the appropriate ISSN for each existing periodicals.
- Add a
getID()
method to all publications to return the ISBN or ISSN as appropriate.
Data Layer
Update the data layer to support storage and lookup of publication ID.
- Update your
PublicationRepository
interface to allow lookup of publications by ID.- The caller can pass an ISBN or ISSN to look up a publication.
- The method signature should resemble your current method signature for lookup by name.
- Now that several publications can have the same name, update the name lookup interface to support returning multiple publications.
- Change your file repository implementation to store publications by ID rather than by name.
- Files will be stored using the filename format
id.pub.dat
instead ofpublication-name.pub.dat
, using the publication's identifier rather than name.
- Files will be stored using the filename format
In order for Booker to sell books, it will need to be able to keep track of stock quantities. Add the capability of storing the stock of books to PublicationRepository
.
- Add methods for simply getting and setting the stock (the number of copies) of a named book.
- In the file repository implementation, store the stock as a plain-text integer value in a text file in the with the filename format
id.stock.txt
.- This type of file that sits alongside another file and provides supplementary information is called a sidecar file.
- Make it easy on yourself and don't create this file until you need to, considering the absence of the file to indicate a stock of zero.
- Don't forget to delete the sidecar file if you ever delete the main publication file.
Business Logic Layer
Now you can update the application business logic so that Booker can begin to function as a bookstore.
- Create a
PublicationManager
interface to manage stock.- Add a method for increasing and decreasing the stock for a book.
- The
PublicationManager
implementation will delegate to aPublicationRepository
for actually getting and setting the stock for a book.
- Create a
BookerService
to encapsulate the main application logic.- Transfer any central application logic you may have placed in the
Booker
class into yourBookerService
implementation. - Create a method for purchasing a copy of a book.
- The service implementation will delegate to a
PublicationManager
for checking and reducing the stock. - Don't forget to throw an exception if a caller attempts to purchase a title for which there is no stock left.
- Transfer any central application logic you may have placed in the
Presentation Layer
Add a purchase
command to allow a user to purchase a book. With this command a command-line parameter --isbn
will indicate the ISBN of the book to purchase. Purchasing will lower the quantity in stock of the identified book by one. Display to the user the success of the purchase operation, along with the remaining stock.
- For now there will be no price involved.
- The
load-snapshot
command will increment the stock of each of the snapshot books. - Don't forget that the
--name
filter may now display multiple books with the same name. - Don't forget to add unit tests as appropriate.
booker list [--locale <locale>] [--name <name>] [--type (book|periodical)]
booker load-snapshot [--locale <locale>]
booker purchase --isbn <ISBN> [--locale <locale>]
booker -h | --help
Option | Alias | Description |
---|---|---|
list | Lists all available publications. | |
load-snapshot | Loads the snapshot list of publications into the current repository. | |
purchase | Removes a single copy of the book identified by ISBN from stock. | |
--help | -h | Prints out a help summary of available switches. |
--isbn | Identifies a book, for example for the purchase command. | |
--locale | -l | Indicates the locale to use in the program, overriding the system default. The value is in language tag format. |
--name | -n | Indicates a filter by name for the list command. |
--type | -t | Indicates the type of publication to list, either book or periodical. If not present, all publications will be listed. |
See Also
- Business Logic (Wikipedia)
- Mediator Design Pattern (SourceMaking)
- Service (systems architecture) (Wikipedia)
- International Standard Book Number (Wikipedia)
- International Standard Serial Number (Wikipedia)
- Sidecar file (Wikipedia)
References
Acknowledgments
- Three-tier application diagram modified from diagram by Bartledan (talk), based on a file by User:Foofy [Public domain], via Wikimedia Commons.