Three-tier architecture for Java software Design

Source: Internet
Author: User

Interface:

 Package DAO;  Public Interface Dao {    String getData ();}
 Package Biz; Import DAO. Dao;  Public Interface Biz {    void  setdao (dao dao);    String dealData ();}
 Package view; Import Biz. Biz;  Public Interface View {    void  setbiz (Biz biz);     void showData ();}

Implementation class:

 PackageDAO;ImportJava.io.BufferedReader;Importjava.io.FileNotFoundException;ImportJava.io.FileReader;Importjava.io.IOException; Public classFiledaoimplImplementsDao {@Override Publicstring GetData () {String Data=NULL; BufferedReader BR=NULL; Try{br=NewBufferedReader (NewFileReader ("Test.txt")); Data=Br.readline (); }  Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally {            if(br!=NULL) {                Try{br.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }        returndata; }}
 PackageBiz;ImportDAO. Dao; Public classUppercasebizimplImplementsBiz {PrivateDAO DAO; @Override Public voidsetdao (dao dao) { This. dao=DAO; } @Override Publicstring DealData () {String Data=Dao.getdata (); if(Data! =NULL) {data.touppercase (); }        returndata; }}
 package   view;  import   biz. Biz;  public  class  Textviewimpl implements   View { private   Biz Biz; @Override  public  void   Setbiz (Biz biz) { this . Biz= Biz; } @Override  public  void   ShowData () {String data  = Biz.dealdata ();    SYSTEM.OUT.PRINTLN (data); }}

Object Factory:

 PackageFactory;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.Properties;ImportBiz. Biz;Importview. View;ImportDAO. Dao; Public classSimplefactory {PrivateProperties Properties;  Publicsimplefactory () {Properties=NewProperties (); InputStream is=NULL; Try{ is=NewFileInputStream ("Conf.properties");        Properties.load (IS); }  Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally {            if(is!=NULL) {                Try{is.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }    }         PublicDao Createdao () {String className= Properties.getproperty ("DAO"); return(Dao) CreateObject (ClassName); }     PublicBiz createbiz () {String className= Properties.getproperty ("Biz"); return(Biz) CreateObject (ClassName); }     PublicView CreateView () {String className= Properties.getproperty ("View"); return(View) CreateObject (ClassName); }    PrivateObject CreateObject (String name) {Object result=NULL; Try{Class Clazz=class.forname (name); Result=clazz.newinstance (); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(instantiationexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(illegalaccessexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returnresult; }}

Configuration file: Conf.properties

View=view. Textviewimpl
Biz=biz.uppercasebizimpl
Dao=dao.filedaoimpl

Text file: Test.txt

HELLO World

Test class:

 Packagetest;Importview. View;ImportBiz. Biz;ImportDAO. Dao;ImportFactory. Simplefactory; Public classTestmain {/**     * @paramargs*/     Public Static voidMain (string[] args) {simplefactory simplefactory=Newsimplefactory (); DAO DAO=Simplefactory.createdao (); Biz Biz=simplefactory.createbiz ();        Biz.setdao (DAO); View View=Simplefactory.createview ();        View.setbiz (Biz);    View.showdata (); }}

Output

HELLO World

Three-tier architecture for Java software Design

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.