Analysis of JAVA design mode (III)

Source: Internet
Author: User

Analysis of JAVA design mode (III)
4. Interface isolation Principle: the ISP (Interface Segregation Principle) client should not depend on the Interface it does not need, or the dependency relationship of the class should be established on the smallest Interface. For example, run the following code: 1 interface I {2 public void method1 (); 3 public void method2 (); 4 public void method3 (); 5 public void method4 (); 6 public void method5 (); 7} 8 9 class A {10 public void depend1 (I I) {11 I. method1 (); 12} 13 public void depend2 (I I) {14 I. method2 (); 15} 16 public void depend3 (I I) {17 I. method3 (); 18} 19} 20 21 class B implements I {22 public void method1 () {23 System. out. println ("method 1 for Class B to implement interface I "); 24} 25 public void method2 () {26 System. out. println ("method 2 for Class B implementing interface I"); 27} 28 public void method3 () {29 System. out. println ("method 3 for Class B implementing interface I"); 30} 31 // For Class B, method4 and method5 are not required, but because interface A has these two methods, 32 // in the implementation process, even if the method bodies of the two methods are empty, the two ineffective methods should be implemented. 33 public void method4 () {} 34 public void method5 () {} 35} 36 37 class C {38 public void depend1 (I I) {39 I. method1 (); 40} 41 public void depend2 (I I) {42 I. method4 (); 43} 44 public void depend3 (I I) {45 I. method5 (); 46} 47} 48 49 class D implements I {50 public void method1 () {51 System. out. println ("method 1 of class D implementing interface I"); 52} 53 // For Class D, method2 and method3 are not required, but because interface A has these two methods, 54 // in the implementation process, even if the method bodies of the two methods are empty, the two methods that have no effect must be carried out.. 55 public void method2 () {} 56 public void method3 () {} 57 58 public void method4 () {59 System. out. println ("Class D Method 4 for implementing interface I"); 60} 61 public void method5 () {62 System. out. println ("class D method for implementing interface I 5"); 63} 64} 65 66 public class Client {67 public static void main (String [] args) {68 A a = new A (); 69. depend1 (new B (); 70. depend2 (new B (); 71. depend3 (new B (); 72 73 C c = new C (); 74 c. depend1 (new D (); 75 c. depend2 (New D (); 76 c. depend3 (new D (); 77} I believe that the interface I is too bloated. In actual development, we should avoid this phenomenon as much as possible. The interface definition should be as detailed as possible. To solve the problem above, we can use interface isolation to solve the problem of interface bloated. Code after transformation: 1 interface I1 {2 public void method1 (); 3} 4 5 interface I2 {6 public void method2 (); 7 public void method3 (); 8} 9 10 interface I3 {11 public void method4 (); 12 public void method5 (); 13} 14 15 class A {16 public void depend1 (I1 I) {17 I. method1 (); 18} 19 public void depend2 (I2 I) {20 I. method2 (); 21} 22 public void depend3 (I2 I) {23 I. method3 (); 24} 25} 26 27 class B implements I1, I2 {28 publ Ic void method1 () {29 System. out. println ("method 1 for implementing interface I1 in Class B"); 30} 31 public void method2 () {32 System. out. println ("method 2 for Class B implementing interface I2"); 33} 34 public void method3 () {35 System. out. println ("class B method for implementing interface I2 3"); 36} 37} 38 39 class C {40 public void depend1 (I1 I) {41 I. method1 (); 42} 43 public void depend2 (I3 I) {44 I. method4 (); 45} 46 public void depend3 (I3 I) {47 I. method5 (); 48} 49} 50 51 class D implements I1, I3 {52 pu Blic void method1 () {53 System. out. println ("Class D Method 1 Implementing interface I1"); 54} 55 public void method4 () {56 System. out. println ("method 4 for implementing interface I3 in Class D"); 57} 58 public void method5 () {59 System. out. println ("Method 5 for implementing interface I3 in Class D"); 60} 61} refine the I interface to I1, I2, I3, in this way, when the three interfaces are implemented, there will be no redundant methods to implement without any operation. Note: When refining interfaces, we need to be as close as possible to the dependent businesses. In my understanding, do not violate the single responsibility principle. Although similar to the single responsibility principle, but different from a single responsibility, a single responsibility focuses on responsibilities, while the interface isolation principle focuses more on interface dependency isolation. Second, the single responsibility principle focuses more on the constraint class, followed by interfaces and Methods. Interface isolation focuses more on the Interface constraints and the construction of the program architecture. When using the interface isolation principle, pay attention to the following points: 1. The interface should be as small as possible, but there must be a limited number of interfaces. 2. Customize services for dependent classes and do not expose methods that are not required by the dependent classes. 3. improve cohesion and avoid excessive external interactions. 5. The Law Of Demeter (Law Of Demeter): A class should know the least about the classes that need to be coupled or called. How is the called class implemented internally, the caller does not need to care about it. The provider only needs to provide these methods for calling. Next let's take a look at the example of the teacher's name, this example is quite popular, the time relationship I do not repeat here for example: http://blog.csdn.net/lajiooo/article/details/6365239 6, Open and Close Principle: OCP (Open Close Principle) it is interpreted that a software entity should implement changes through extensions, rather than modifying existing code. The principle of opening and closing is a very virtual principle. The first five principles are specific interpretations of the principle. Using an example of a bookstore on page 45th of design patterns ZEN (version 2): public interface IBook {public String getName (); public int getPrice (); public String getAuthor ();} public class NovelBook implements IBook {private String name; private int price; private String author; public NovelBook (String name, int price, String author) {this. name = name; this. price = price; this. author = author;} @ Override public String getName () {retur N name ;}@ Override public int getPrice () {return price ;}@ Override public String getAuthor () {return author ;}} public class BookStore {private final static ArrayList <IBook> bookList = new ArrayList <IBook> (); static {bookList. add (new NovelBook ("tianlong Babu", 3200, "Jin Yong"); bookList. add (new NovelBook ("Notre Dame de Paris", 5600, "Hugo"); bookList. add (new NovelBook ("miserable world", 3500, "Hugo"); bookList. add (new NovelBook ("Mei", 4300, "Liling Smile ");} public static void main (String [] args) {NumberFormat formatter = NumberFormat. getCurrencyInstance (); formatter. setMaximumFractionDigits (2); System. out. println ("the records of books sold in the bookstore are as follows:"); for (IBook book: bookList) {System. out. println ("book name:" + book. getName () + "\ t Author:" + book. getAuthor () + "\ t book price:" + formatter. format (book. the getPrice ()/100.0) + "Yuan") ;}} project has been put into operation, but now there is a demand that all books of more than 40 yuan will be sold at off, how can I modify the program for selling other books at discount? I'm sure we can't modify the interface. The interface is an external contract. If you modify the interface, there are too many changes and the risks are high. The most direct idea is to directly modify the getPrice () method. This is a good idea, but there is a bad thing: After modifying the getPrice () method, what is displayed is the price after the discount. The original price cannot be displayed, so there is no comparison. Is there any better way? Here, let's experience the benefits of an open/closed principle, implement changes through extensions, add an OffNovelBook class, inherit from NovelBook, and override the getPrice () method, this class is used to deal with discounted novel books without modifying the original code. The code for the new class is as follows: 1 public class OffNovelBook extends NovelBook {2 3 public OffNovelBook (String name, int price, String author) {4 super (name, price, author ); 5} 6 7 @ Override 8 public int getPrice () {9 int selfPrice = super. getPrice (); // original price 10 11 int offPrice = 0; // price after discount, initialized to 012 if (selfPrice> 4000) {13 offPrice = selfPrice * 90/100; 14} 15 else {16 offPrice = selfPrice * 80/100; 17} 18 19 return offPrice; 20} 21} 22 public class BookStore {23 private final static ArrayList <IBook> bookList = new ArrayList <IBook> (); 24 static {25 bookList. add (new OffNovelBook ("tianlong Babu", 3200, "Jin Yong"); 26 bookList. add (new OffNovelBook ("Notre Dame de Paris", 5600, "Hugo"); 27 bookList. add (new OffNovelBook ("miserable world", 3500, "Hugo"); 28 bookList. add (new OffNovelBook ("Mei", 4300, "lanling Xiaosheng"); 29} 30 public static void main (String [] args) {31 NumberFormat formatter = NumberFormat. getCurrencyInstance (); 32 formatter. setMaximumFractionDigits (2); 33 System. out. println ("the book records sold by the bookstore are as follows:"); 34 for (IBook book: bookList) {35 System. out. println ("book name:" + book. getName () + 36 "\ t Author:" + book. getAuthor () + 37 "\ t book price:" + formatter. format (book. getPrice ()/100.0) + "RMB"); 38} 39} 40 41}

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.