Re-recognize the decorative mode--the decorative pattern realizes AOP

Source: Internet
Author: User
Tags dateformat

first, what is AOP?AOP: Aspect-oriented programming, he is a programming paradigm that provides a different perspective to consider program structure to improve object-oriented programming. Second, the function of AOP
in the system development process will have some common features, this is the object-oriented vertical thinking method can solve the problem, this time, AOP horizontal extension can solve these public service weaving system. AOP can encapsulate the logic or responsibility, such as transaction processing, log processing, and privilege control, that is not related to the business but is called by the business module, so as to reduce the duplication code of the system, reduce the coupling between modules, and benefit the future operability and maintainability. by comparing the above process with the adornment mode, the business function object can be regarded as the decorated object, and the public module is like an adorner, which can add function to the business function object transparently. so from one side, the decorative mode and AOP to achieve similar functions, but the implementation of AOP is different, will be more flexible, more configurable, and AOP a more important change in the thinking of the change of mind----master-slave transposition, so that the original active call to the function module into a passive wait, Even without the knowledge to be woven into a lot of new features. Third, the implementation of AOP with decoration:
Package itcast.test.decorator;/** * Adorner interface, need to implement the same interface as the object being decorated */public abstract class Dercorator implements Goodssaleebi {    /** * Holds the decorated Component Object */protected Goodssaleebi ebi;    /** * Through the construction method to the Decorated object * @prara, ebi decorated Object * * Public dercorator (Goodssaleebi ebi) {This.ebi=ebi; }}package Itcast.test.decorator;public class Checkdecorator extends dercorator{/** * Implement permissions control */Public Checkdecor    Ator (Goodssaleebi ebi) {super (EBI); } public boolean sale (String user,string customer,salemodel Salemodel) {//simple, just let Zhang San perform this function if ( !"            Zhang San ". Equals (user) {System.out.println (" Sorry "+user+", you do not have permission to save the sales order ");        It is no longer necessary to invoke the function of the decorated object to return false;        }else{return This.ebi.sale (User,customer,salemodel); }}}package itcast.test.decorator;import Java.text.dateformat;import java.text.simpledateformat;import Java.util.date;public class Logdercorator extends dercorator{public logdercorator (GoodssaleebI ebi) {super (EBI); } public boolean sale (String user,string customer,salemodel Salemodel) {//Execute business function Boolean f= This.ebi.sale        (User, customer, Salemodel);                Log DateFormat df= new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss SSS") after performing business functions;                System.out.println ("Logging:" +user+ "in" +df.format (New Date ()) + "saved a sales record, the customer is" +customer+ ", the purchase record is" +salemodel ";    return F;  }}package itcast.test.decorator;//Business Interface for commodity sales management public interface Goodssaleebi {/* * * Save sales information * @param customer * @param salemodel Sales Data * @return saved successfully */public boolean sale (String user,string customer,salemodel salemodel);} Package Itcast.test.decorator;public class Goodssaleebo implements Goodssaleebi {@Override public boolean sale (stri        ng user, String customer, Salemodel salemodel) {System.out.println (user+ "Saved" +customer+ "purchase" +salemodel+ "sales Data");    return true; }}package itcast.test.decorator;/** * Packaged Sales Single Data object */public class Salemodel {Private String goods;    private int salenum;    Public String Getgoods () {return goods;    } public void Setgoods (String goods) {this.goods = goods;    } public int Getsalenum () {return salenum;    } public void Setsalenum (int salenum) {this.salenum = Salenum;    The public String toString () {return "Commodity name =" +goods+ ", Purchase quantity =" +salenum;         }}package Itcast.test.decorator;public class Client {public static void main (string[] args) {//Get business interface, composite adorner        Goodssaleebi ebi = new Checkdecorator (new Logdercorator (New Goodssaleebo ()));        /* Prepare test data */Salemodel salemodel= new Salemodel ();        Salemodel.setgoods ("MoTo mobile");                Salemodel.setsalenum (2);        Call and Business function Ebi.sale ("Zhang San", "Zhang San Feng", Salemodel);            Ebi.sale ("John Doe", "Zhang San Feng", Salemodel); }}
Operation Result:
Iv. Analysisdecoration can realize the function of AOP, but also can achieve about any combination of ways, then we can see that the nature of the decorative mode has two points: Dynamic + combination, that is, the decoration mode can be dynamically combined with any function, these functions to our business functions to do the outer packaging, This also realizes the function of AOP. Decorative patterns are more flexible than inheritance, are easier to reuse, and simplify the definition of high-level. But in the code above we can see that there are many fine-grained objects, such as Logdercorator,Goodssaleebo, and so on,when the more complex the function, the more fine-grained objects needed.


Re-recognize the decorative mode--the decorative pattern realizes AOP

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.