Zen of design pattern--the principle of interface isolation of six design principles

Source: Internet
Author: User

The design pattern is to let us solve the problem more conveniently.
Share a story here. I have a friend, um yes is a friend, participate in a software competition, a classmate write the code on the server, Zooey update, regardless of the feelings of the people who write the client, simply can no longer cattle. If Java updates have been made this time, no consideration has been given to making changes without affecting the previous code, and how many programmers are going to bleed to death.

Definition of the interface isolation principle:
Set up a single interface, do not set up a bloated and enlarged interface. The interface is as thin as possible, and the methods in the interface are as few as possible.
This is not a single responsibility principle, the single responsibility requires the class and interface of the single responsibility, focus on the responsibility, this is the division of Business logic, and the pretext of isolation principle requires the interface of the minimum method. For example, the responsibility of an interface may be a method, which is a method is placed in an interface, and provided to a plurality of modules access, these 10 methods are placed in an interface, and provided to a number of modules access, each module according to the required permissions to access, outside the system through the document constraints "do not use the method do not access", According to the principle of single responsibility is permissible, according to the principle of interface isolation is not allowed, because it requires "to provide as many specialized interfaces as possible." What does a dedicated interface mean? Refers to each module should be provided to a single interface, provided to a few modules should have several interfaces, rather than build a huge bloated interface, to accommodate all client access.

Examples: How many beautiful women, different views
A process for a star Scout to find a beautiful woman

 Public Abstract  class abstractsearcher() {    protectedIpettygirl Prettygirl; Public Abstractseacher(Ipettygirl _pettygirl)    {PRETTYGRL = _pettygirl; } PublicAbstracevoid Show();} ---------- Public  interface ipettygirl{     Public void goodlooking();//Nice face     Public void nicefigure();//Have a body     Public void greattemperament();//have temperament}---------- Public  class pettygirl implements ipettygirl{    PrivateString name; Public Pettygirl(String name) { This. name = name; } Public void goodlooking() {System.out.println ( This. Name +"----"+"The face is beautiful."); } Public void greattemperament() {System.out.println ( This. Name +"----"+"Good"); Public void greattemperament() {System.out.println ( This. Name +"----"+"Good"); }}---------- Public  class Searcher extends abstractsearcher{     Public Searcher(Ipettygirl _pettygirl) {Super(_pettygirl); } Public void Show() {System.out.println ("---------beauty information is as follows:-------------");Super. pettygirl.goodlooking ();Super. Pettygirl.nicefigure ();Super. Pettygirl.greattemperament (); }}---------- Public  class Client{     Public Static void Main(string[] args) {Ipettygirl Yanyan =NewPettygirl ("HMS"); Abstractsearcher searcher =NewSearcher (Yanyan);    Searcher.show (); }}

OK, there's nothing wrong with it.
However, beauty everyone has a different definition, each time has different aesthetic standards. For example, a girl is not beautiful but temperament is very good, you can say that she is a beautiful woman. But our interface is defined by the three are the beauty, how to do? Maybe you feel like writing another beauty class, realizing that only the Greattemperament method can be implemented. But there are three methods in the Star Scout Show () method, how can you do this one?

 Public  interface igoodbodygirl() {     Public void goodlooking(); Public void nicefigure();} ---------- Public  interface igreattemperamentgirl{     Public void greattemperament();} Public  class pettygirl implements ipettygirl, igreattempramentgirl {    PrivateString name; Public Pettygirl(String name) { This. name = name; } Public void goodlooking() {System.out.println ( This. Name +"----"+"The face is beautiful."); } Public void greattemperament() {System.out.println ( This. Name +"----"+"Good"); Public void greattemperament() {System.out.println ( This. Name +"----"+"Good"); }}----------

OK, so after, have temperament and long good on it. If one day good face is also beautiful still need to modify, this time, there is a big kill device appeared-the design is limited, can not indefinitely consider the future change situation, otherwise it will fall into the mire of design can not extricate themselves.

Maintain the purity of the interface
1, the interface as small as possible, with a single responsibility conflict priority to consider the principle of single responsibility
2, the interface to high cohesion, as far as possible to publish public methods.
3. Customized Service
4, the interface design is limited

Zen of design pattern--the principle of interface isolation of six design principles

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.