Design pattern – open and close principle is the principle of opposition and unity
What is the open close principle
Software entities should be open to extension and closed to modification, that is, entities should make changes through extensions, not code changes
What are the abstract class methods of software entities, projects, or modules divided according to a logical plan
Bookstores sell books
Then write the code as follows
Public interface Ibook{// name public String getName(); Public int getPrice(); // author public String getAuthor(); }Copy the code
Bookstores sell fiction books and write code
Public class implements IBook {// name private String name; // private int price; // private String author; Public NoveIBook(String _name, int _price, String _author){this.name = _name; this.price = _price; this.author = _author; } // Get author public StringgetAuthor() {returnthis.author; } // Price public StringgetPrice() {returnthis.price; } // Name public StringgetName() {returnthis.name; }}Copy the code
Where, the price is defined as int, not error, non-financial items, take two digits of precision, expand 100 times during calculation, shrink 100 times during presentation. books
public class BookStore { private final static ArrayList bookList = new ArrayList(); Java's three layers are the view layer, the service layer, and the persistence layer. Static {booklist.add (new NoveIBook(); // static{booklist.add (new NoveIBook(); // static{booklist.add (new NoveIBook());"", 3200, "")); } / / to buy books public static void main (String [] args) {/ / large format NumberFormat formatter. = NumberFormat getCurrencyInstance (); formatter.setMaximumFractionDigits(2);for(IBook book:bookList){ System.out.println(book.getName() + book.getPrice() + book.getAuthor()); }}}Copy the code
And then, there’s a discount. Modifying the interface The interface should not be modified because the interface is persistent. The implementation class modifiers the getPrice() method for discounted purposes. But, because the person who buys the book needs to see the realized price. So do not modify the extension implementation and add a subclass as follows
The following code
Public class extends NoveIBook {public class extends NoveIBook(String _name, int _price, String _author){ super(_name, _price, _author); } // Override public intgetPrice() {returnsuper.getPrice()*90 / 100; }}Copy the code
Then modify the contents of main.
change
Changes are divided into logical changes, submodule changes, and visible view changes.
Use the open close principle
Abstract the constraint
Abstract constraints describe a group of things. When the store increased the sale of a computer book. But there are many computer books, such as programming languages, databases and so on.
Public interface IComputerBook extends IBook{public String getScope(); // Scope of computer books}Copy the code
Similarly, writing computer books
public class ComputerBook implements IComputerBook{
private String name;
private String scope;
private String author;
public ComputerBook(String _name, int _price, String _author, String _scope){
this.name = _name;
this.price = _price;
this.author = _author;
this.scope = _scope;
}
public String getScope() {return this.scope;
}
public String getName() {return this.name;
}
public int getPrice() {returnthis.price; }}Copy the code
Just add it in main. Conclusion; Open to extensions, premise to abstraction constraints.
Metadata control module
That is, use configuration parameters to control module behavior.
The principle of summary
Single responsibility
The responsibility of a class should be simple
Replacement on the Richter scale
Richter’s substitution does not destroy inheritance that is, subclass objects can replace superclasses.
Dependency inversion
Interface oriented. That is, each interface does only one thing.
Interface segregation
Each interface does only one thing
Demeter’s rule
Communication communicates between classes. The less coupling between the two, the better.
The open closed principle
Open for extensions, closed for modifications