Zen-interface isolation principle in Design Mode

Source: Internet
Author: User

Zen-interface isolation principle in Design Mode
Two types of interfaces: (1) instance Interface (2) Class Interface, use Interface-defined Interface isolation: (1) the client should not rely on interfaces that it does not need (2) the dependency between classes should be based on the smallest interface. Scenario Description: you are bored on the street one day, shouting a beautiful girl on the street. As a result, many girls will look back at you. We now give a definition to the beauty: (1) good looks (2) Good Body (3) good temperament, but now the problem arises. Is it only the beauty that fits all three, for example, a girl has a good temperament, but looks general, but she is still a beautiful girl. We call her angry:

 
Public class Client {public static void main (String [] args) {// TODO Auto-generated method stubIGoodBodyGril bodyGril = new IGoodBodyGril () {@ Overridepublic void niceFigure () {// TODO Auto-generated method stubSystem. out. println (good figure !);} @ Overridepublic void goodLooking () {// TODO Auto-generated method stubSystem. out. println (good looks !);}}; Export greatTemperamentGril = new IGreatTemperamentGril () {@ Overridepublic void greatTemperament () {// TODO Auto-generated method stubSystem. out. println (good temperament !);}}; Search search = new Search (greatTemperamentGril); search. show (); Search2 search2 = new Search2 (bodyGril); search2.show ();}}

The above is to split a bloated interface into two, so that AbstractSearch has two more targeted interfaces (this seems a bit like a single responsibility, but the scope is much larger than a single responsibility ). Restrictions on the Interface isolation principle: (1) the interface should be as small as possible (according to the interface isolation principle, the splitting interface must first meet the single responsibility principle) (2) high Cohesion is required for interfaces (the book explains what high cohesion is. An interesting example is given: One day you told your subordinates to pick up the XXX file on Obama's desk. After several months, your subordinates actually handed you this file. In this case, the performance of performing the task immediately without any conditions is high cohesion. Specifically, the isolation principle requires that the public method should be published as few as possible in the interface. In other words, the number of interfaces should be minimized. Well, the first one is returned.) (3) customized services (only provide the methods required by visitors) (4) interface design is limited (the smaller the interface design, the more flexible the system is, but correspondingly, the system becomes more complex, so this limit depends on the project.) interface isolation design principles: (1) an interface serves only one submodule or business logic (2) compress the public method in the interface through the business logic (3) the contaminated interface should be modified as much as possible. If there is a high risk of change, use the adapter mode for conversion (4) understand the environment and reject blind access

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.