The principle of interface isolation of six principles of design pattern

Source: Internet
Author: User
Tags ming

First, the concept:

The interface isolation principle is defined in two ways:

1, clients should not being forced to depend upon interfaces that they don ' t use.

( the client should not forcibly rely on interfaces it does not need )

2. The dependency of one class to another one should depend on the smallest possible interface.

( dependencies between classes should be based on the smallest interface )

The client should not rely on interfaces it does not need. The methods in the interface should be as few as possible, do not make the interface too bloated, do not have a lot of irrelevant logical methods. And the premise of satisfying the principle of single responsibility.

Second, examples:

Use brick walls as an example. Bricks have red bricks, cement bricks and so on. Here's a hypothesis: lazy employees and diligent employees have different skills. Diligent employees can move bricks and walls. A lazy employee can only make walls.

So the operation of the wall and the operation of the brick to be isolated. The class diagram is as follows:



Look at the class diagram only, look at the code as follows:

Worker interface: "Whether it is a drywall worker or a worker who moves bricks, know what bricks are used and add a Setbrick method (dependency inversion principle)"

Package Dim.interfaceiso.simples.brick;public interface Ibrickman {/** * Set name * @param name *        /public void SetName (STR ing Name);        /**         * Set Age         * @param ages         *        /public void Setage (int.);        /**         * Set to move what bricks         * @param brick *         /public        void Setbrick (Ibrick brick);}

drywall worker Interface "inherit worker interface":

Package Dim.interfaceiso.simples.brick;public Interface Ibrickbuilder extends ibrickman{    /**     * Drywall */           public void Bulidwall (); }

Brick worker Interface:

Package Dim.interfaceiso.simples.brick;public interface Ibrickloader {/** * move brick */public void Removedbrick ();}

Brick Interface:

Package Dim.interfaceiso.simples.brick;import Java.awt.color;public Interface Ibrick {/** * Set brick size * @param size */public void setSize (int size);/** * Set Brick color * @param color */public void setcolor (color color);   /**    * Set brick name    */public void SetName (string name);p ublic String  getName ();

red brick Type:

Package Dim.interfaceiso.simples.brick;import Java.awt.color;public class Redbrick implements ibrick{//red brick size private int size;//red brick color private color color;   Private String name= "";p ublic redbrick () {//TODO auto-generated constructor stub//system.out.println ("Red brick!");} @Overridepublic void setSize (int size) {//TODO auto-generated method stubthis.size=size; System.out.println ("Red brick size is  " +this.size);} @Overridepublic void SetColor (color color) {//TODO auto-generated method Stubthis.color=color;} @Overridepublic void SetName (String Name) {//TODO auto-generated method Stubthis. Name=name;} @Overridepublic String GetName () {//TODO auto-generated method Stubreturn Name;}}

Cement Brick Type:

Package Dim.interfaceiso.simples.brick;import Java.awt.color;public class Cementbrick implements ibrick{//size private int size;//color private color color;   Private String name= "";p ublic Cementbrick () {//TODO auto-generated constructor stub//system.out.println ("Cement brick!") ;} @Overridepublic void setSize (int size) {//TODO auto-generated method stubthis.size=size; SYSTEM.OUT.PRINTLN ("Cement brick size is  " +this.size); @Overridepublic void SetColor (color color) {//TODO auto-generated method Stubthis.color=color;} @Overridepublic void SetName (String Name) {//TODO auto-generated method Stubthis. Name=name;} @Overridepublic String GetName () {//TODO auto-generated method Stubreturn Name;}}

Diligent employee class: "Will wall also will move brick, realize brick and drywall interface"

Package Dim.interfaceiso.simples.brick;public class Diligentworker implements Ibrickbuilder,ibrickloader{private String nameofman= "";p rivate ibrick brick; @Overridepublic void SetName (String Name) {//TODO auto-generated method Stubnam Eofman=name;} @Overridepublic void setage (int age) {//TODO auto-generated method stub} @Overridepublic void Setbrick (Ibrick brick) {//T ODO auto-generated method Stubthis.brick=brick;} @Overridepublic void Removedbrick () {//TODO auto-generated method StubSystem.out.println (nameofman+ "Move" +brick.getname ());} @Overridepublic void Bulidwall () {//TODO auto-generated method StubSystem.out.println (nameofman+ "with" +brick.getname () + "Walls Up");}}


Lazy Employee class: "Only walls, to achieve drywall interface"

Package Dim.interfaceiso.simples.brick;public class Lazyworker implements Ibrickbuilder{private String name= ""; Private Ibrick brick; @Overridepublic void SetName (String Name) {//TODO auto-generated method Stubthis. Name=name;} @Overridepublic void setage (int age) {//TODO auto-generated method stub} @Overridepublic void Setbrick (Ibrick brick) {//T ODO auto-generated method Stubthis.brick=brick;} @Overridepublic void Bulidwall () {//TODO auto-generated method StubSystem.out.println (name+ "with" +brick.getname () + "drywall" );}}

Test class: Employees Xiao Ming is diligent staff, will also move brick walls. Xiao Hua is lazy, only the walls.

Package Dim.interfaceiso.simples.brick;public class TestClass {public static void main (string[] args) {//red brick    Ibrick Redbrick=new redbrick ();    Cement brick    ibrick cementbrick=new cementbrick ();    Set the brick name    redbrick.setname ("red brick");    Cementbrick.setname ("cement Brick");    Employee Diligentworker xiaoming=new Diligentworker (); Lazyworker xiaohua=new Lazyworker () xiaoming.setname ("Xiaoming"); Xiaoming.setbrick (redbrick); Xiaoming.removedbrick (); Xiaoming.bulidwall (); System.out.println ("============"); Xiaoming.setbrick (Cementbrick); Xiaoming.removedbrick (); System.out.println ("============"); Xiaohua.setname ("Xiao Hua"); Xiaohua.setbrick (Cementbrick); Xiaohua.bulidwall ();}}


Test results:


Xiao Ming moved red brick
Xiao Ming with the red brick walls
============
Xiao Ming moves Cement Bricks
============
Little Wah is using concrete bricks.



Third, the description:

1, interface isolation and the difference between a single duty: the single responsibility principle is to require the class and interface of the single responsibility, responsibility is the division of Business logic.

2, according to the principle of interface isolation, first meet the principle of single responsibility.

3, the interface to high cohesion, not too bloated.


RELATED links:

Single duty principle of six principles of design pattern

The six principles of design pattern on the Richter scale substitution principle

The principle of dependency inversion in six principles of design pattern


Some shortcomings, many corrections, thank you!

Resources:

"Zen of Design Patterns"

"Headfirst"

Six Principles of design pattern


The principle of interface isolation of six principles of design pattern

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.