Business delegation model architecture is more flexible business layer

xiaoxiao2021-03-06  59

Disclaimer: This article is designed from the IBM China Technical Website EJB Best Practice Column, I only optimized on its basis! Please advise!

1. Design a business interface, the code is as follows:

Public interface business {

} // I don't do anything

2, in this case we assume that the Book is designed to program the idea of ​​the interface, we should extract the interface from the business method, the sample code is as follows:

Pubic Interface Bookinterface Extends Business {

Public long createbook () throws bokexception;

Public List Findall () THROWS BOOKEXCEPTION;

Public List Findselltop10 () THROWS BOOKEXCEPTION;

}

3, press "Ship", we now come to implement this business interface, the sample code is as follows:

Public Class BookImpl Implements BookInterface {

PRIVATE DAOFACTORY DAOFACTORY;

PRIVATE BOOKDAO BOOKDAO

Public bookimpl () {

// Initialize the DAO factory

}

Public Long Createbook (Book Book) THROWS BOKEXCEPTION {

// Initialize BOOKDAO

Return Dao.create (BOOK);

// Release BookDao resources

// abnormal processing

}

Public List Findall () THROWS BOOKEXCEPTION {

...

}

Public List Findselltop10 () THROWS BOOKEXCEPTION {

...

}

}

4. From the above code, use DAO mode and factory model, our business layer has been well coupled and the persistent layer maintains a good coupling, then the coupling between the business layer and the representation? We use the business delegation model to design:

Public interface delegate {

} // Business delegation interface

Public class bookdelegate implements delegate {

Private static final string my_name = "book";

Private BusinessFactory BusinessFactory; // Sample

Private bookinterface interface; // This is an interface

Public bookdelegate () {

INIT ();

}

Public long cretebook (book book) {

Return Interface.createbook (BOOK);

}

Public List Findall () {

Return interface.FindAll ();

}

Public List Findselltop10 () {

Return interface.findselltop10 ();

}

Private vidinit () {

BusinessFactory = BusinessFactory.getInstance ();

Interface = (bookinterface) BusinessFactory.createbusiness (my_name);

}

Private void destroy () {

}

}

Now, let's uncover the veil of BusinessFactory, BusinessFactory requires a configuration file, business.properties, this file example:

## Business.propertiesbook = net.huisky.test.business.bookimpl

Bean = net.huisky.test.business.beanimpl

##and many more

## over

Public class businessfactory {// single case mode

PRIVATE BusinessFactory Myself;

PRIVATE PROPERTIES PROPS;

private businessfactory () {

INIT ();

}

Private vidinit () {

Read Business.Properties, initialize the PrOPS;

}

Public Business Createbusiness (String Name) {

Return (Business) Class.Forname (Props.GetProperty (MAME));

// Treatment exception

}

}

Result: In servlet, we only need

BookDelegate delegate = new bookdelegate ();

You can call the business method, and DELEGATE is used in interface, not implementation

If you want to rewrite business methods, you don't have to change the call of the servlet.

Just rewrite a beanimpl!

Another: The business delegation can be written more flexible, that is, using dynamic delegation, it is achieved by using Java reflex mechanism, interested in:

http://www-900.ibm.com/developerWorks/cn/java/J-EJB1119/index.shtml

转载请注明原文地址:https://www.9cbs.com/read-117333.html

New Post(0)