1, define,

We’ve said this before:

The Rap Says: The Single Duty Principle

Hip Hop: The Richter Substitution Principle

Hip Hop: The Inversion of Dependence Principle

Hip Hop: Principles of Interface Segregation

Hip-hop Speaks: The Demeter Rule

Gather the five principles, and you can summon the dragon.

Dragon is the open and closed principle, so to speak: it is the realization of the other five principles, the ultimate goal of object-oriented design.

Since the principle of openness and closure is so high-end, let’s take a look at the definition of the principle of openness and closure.

A software entity should be open for extension but closed for modification.

By being open to extension, we mean that we can add new features with impunity.

Turning off changes means that no changes are made to the original functionality.

This is the ultimate in code writing.

Sadly, this is unlikely to happen. The requirements themselves are being modified, the program is to serve the requirements, the code is not changing is impossible.

We should try our best to follow the principle of openness and closure. Fully achieve the open and closed principle, courtiers can not do wow!

2, How to do

You can think about: how can we be open to expansion and closed to modification?

Method 1: Abstracts an interface or abstract class, defining common methods for easy extension.

Method 2: Refers to an interface or abstract class, independent of the implementation class.

Method 3: Interfaces and abstract classes cannot be modified, but can be extended by inheriting the interface or abstract class.

All three of these approaches revolve around a core, what is it?

That’s right, abstraction.

We should program for the abstract, not for the concrete.

Is there a sense of deja vu?

Yes, the dependency inversion principle also mentioned this idea.

The principle of openness and seclusion is the general principle, and the principle of dependence and inversion is a very good implementation of the principle.

The Richter’s Substitution principle is also a very good implementation of the open and closed principle, so we can use scenes and code from Hip Hop: The Richter’s Substitution Principle.

3,

Chef of eight major cuisines

Tomato Restaurant, after careful and conscientious operation, from a small restaurant into a large restaurant.

Cook: boss, we now big business big customer flow is also big, although I am full of energy, but I also can’t stand so many people’s destruction.

Boss: Vandalized? Are you sure?

Cook: no, you heard wrong, it is to take care of, can not stand the care of so many people.

Boss: Small Turkey, can ah, strong desire for life. So what do you have in mind?

Chef: I think we can introduce chefs of eight major cuisines. First, we can hand over the dishes of any cuisine to the chefs of any cuisine. The taste and quality will be better, and only then can we match the restaurant of such high specifications.

Boss: Well, you have a point there. Go on.

Cook: two, more hands, but also can increase the speed of food, three to……

Boss: that makes sense. We’re hiring a chef right now, little Turkey. Congratulations, you’ve been promoted and you’re the future head chef. Because you really want to live.

Chef: Thanks, Boss. (Heart: I have a strong desire for life? What’s stronger? After school you don’t go, I let you taste my fierce, to make you a table of good dishes)

The desire for life is really strong.

4, implementation,

package com.fanqiekt.principle.liskov; Public void cook(String) {public void cook(String) {public void cook(String) {public void cook(String) DishName){system.out.println (" start cooking: "+dishName); cooking(dishName); System.out.println(dishName + ""); } /** * start cooking */ protected void cooking(String dishName); }Copy the code

Abstract cook classes, public cook methods, are responsible for some of the same logic that cooks do, such as starting the preparation of cooking, and leaving the pot.

Specific cooking details provide an abstract method of cooking (cooking), which cooks of specific cuisines need to rewrite.

package com.fanqiekt.principle.liskov; Public class ShanDongChef extends Chef{@override protected void cooking(String) DishName){switch (dishName){case "cookingTomato(); break; Default: throw new IllegalArgumentException(" Unknown food "); }} private void cookingTomato() {system.out.println (system.out.println); System.out.println(" refry tomato "); System.out.println("..." ); }}Copy the code

Shandong cuisine Chef ShanDongChef inherited the abstract Chef class Chef and realized the abstract cooking method.

With the addition of scrambled eggs and tomatoes, we can extend the ShanDongChef class by inherits the Chef abstract class, which is open to extension.

Avoid making changes to the Chef class to avoid creating new problems for the existing system, which is closed to changes.

Hip-hop says

Next, please enjoy the original song of open and closed principle.

Hip Hop says: Interface segregation Principle Rapper: Lazy people are open to expansion, open to modification, and closed to be specific, they should be bold and hard to attack. They are easy to open and closed when they fall in love with abstract programmingCopy the code

To try it, click here

Idle no matter to listen to music, knowledge has filled the brain to;

Learning new ways to review, wearing headphones is a big deal.