Decoupling the classic three-tier architecture with Java Viewer mode

Source: Internet
Author: User

The three-tier architecture is a classic architectural model that differs according to the system's responsibilities. The system is divided into the representation layer, the logical layer and the data access layer, and with the data entity to transmit data, can be greatly encapsulated and reusability.

Classic three-layer architecture diagram:


We go deep into the frame of the composition. Take a look at the detailed class diagram, with a simple landing example:


Here through the Loginui. Two classes of Loginlogservice and Loginverificationservice are called. It can be seen from the class diagram that the U-layer and service layer (which is called the service layer of the BLL layer in this paper) directly uses the direct invocation method.

In object-oriented design. Always emphasized to interface programming, OK we add the interface:



All right. Add the interface later. You can decouple the U-layer and service layer by using the factory method or the management-dependent framework spring. We are always replacing Loginlogservice and Loginverificationservice. This is both compatible with programming for interfaces. By meeting the open and closed principle. Also fit on the Richter scale replacement.

Good. Then I have a question. I don't want to change the current two logic and want to add a third logic. Do you have to change the class of U-layer?

OK, now it's time for the observer pattern to appear, so let's see how this pattern can be used to solve the logic join to achieve true decoupling of the three-tier architecture (this is mainly the coupling between the U-layer and service layer). In fact, the observer pattern is the implementation of the Java Trust.

First, explain the next idea. We use Loginui as the source of the event and send Usernamepassword as a message. A registered service can process messages, which means that the U-layer and service layer are decoupled through messages. Look at the class diagram.


Here Loginui becomes Logineventsource, becomes an event source, and inherits EventSource Abstract class, two service classes inherit listener interface and become listener.

How is that in detail decoupled?

First look at EventSource abstract class. This abstract class implements the registration, deletion, and notification methods of the event source.

Package Com.tgb.chargesystem;import Java.util.arraylist;public Abstract class EventSource {//keep registered Listener Private arraylist<listener> listeners=new arraylist<listener> ();//register Listener to eventsource// EventSource has data that Listener interested inpublic void Registerlistener (Listener Listener) {Listeners.add (Listener );} Stop focuse on this eventpublic void RemoveListener (Listener Listener) {int i=listeners.indexof (Listener); if (i>0) { Listeners.remove (i);}} EventSource notify listener and send itself as message public void Notifylistenner () {for (int i=0;i<listeners.size (); i++) {Listener listener=listeners.get (i); listener.actionperformed (this);}}}


Next look at the subclass Logineventsource of EventSource, which provides two callback methods.

Package Com.tgb.chargesystem;import Java.util.arraylist;public class Logineventsource extends Eventsource{private String Username= "Xqf";p rivate string password= "123";//callback method, when success in Verification,be invoked public Str ing loginsuccess () {System.out.println (This.getclass (). GetName () + "Print:login Success"); return "Success";} Callback method, when the fail in Verification,be invoked public String Loginfail () {System.out.println (This.getclass (). GetName () + "print:login fail"); return "fail";} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;}}

Next look at the Listener interface

Package Com.tgb.chargesystem;public interface Listener {void actionperformed (EventSource e);}

Implements the Loginloglistener class of the Listener interface

Package Com.tgb.chargesystem;public class Loginloglistener implements Listener {public Loginloglistener (EventSource EventSource) {Eventsource.registerlistener (this);} @Overridepublic void actionperformed (EventSource e) {System.out.println ("====== save log to TXT file =========="); System.out.println ("Username:" + ((Logineventsource) e). GetUserName () + ", Password:" + ((Logineventsource) e). GetPassword ()); System.out.println ("======= Save Complete ==========");}}


Implements the Loginverificationlistener class for the Listener interface:

Package Com.tgb.chargesystem;public class Loginverificationlistener implements listener{private static final String Username= "XQF";p rivate static final String password= "123"; Public Loginverificationlistener (EventSource EventSource) {//register itself to Eventsourceeventsource.registerlistener (this);} @Overridepublic void actionperformed (EventSource e) {System.out.println (This.getclass (). GetName () + "Print:invoke Actionperformed method "); Logineventsource les= (Logineventsource) e;//according to username and Passowrd,call Callback methodif (Les.getusername ( ). Equals (USERNAME) &&les.getpassword () equals (PASSWORD)) {les. Loginsuccess ();} Else{les. Loginfail ();}}}

In this case, we also need a class to register the Listener class with the event source, so. The event source, the U-layer, is able to send messages to the service layer, where we will put this class on the U-level for the moment.

This is when we want to add logic, just need to implement the listener interface, and change the following class, the newly added class to register to Logineventsource on it.

public static void Main (string[] args) {Logineventsource logineventsource=new logineventsource (); Loginloglistener lll=new Loginloglistener (Logineventsource); Loginverificationlistener lvl=new Loginverificationlistener (Logineventsource); Logineventsource.notifylistenner () ;}









Decoupling the classic three-tier architecture with Java Viewer mode

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.