Fourth-open and closed Principle

Source: Internet
Author: User

Labels: design mode open and closed principle Java code object-oriented

Let's give an inappropriate example. Mr. A is a charming and elegant man. She is very fond of many beautiful women. He has always been very open and easygoing, so he is very close to the beautiful people around him. He cannot tell whether it is love or friendship. How many years has it been a dream? Who doesn't want to be surrounded by beautiful women like the cloud? One day, John made a girlfriend. His girlfriend can be described as an outstanding hero. Isn't it a dog's blood? Don't worry. We're not trying to tell stories. But Mr. A is used to being open and does not know how to close it. It's easy-going with the girls around you. Consistent, girlfriend angry. The consequences are serious.
Why? Because love is the only one that cannot be violated, cannot be shared, and is occupied by private. It should be closed externally. Small A doesn't know how to grasp the principle of open and closed. So what is the principle of open and closed? See the following.
1. What is the open and closed principle?
The so-called open and closed principle means that software entities should be extensible and cannot be modified. That is to say, it is open to extensions and closed to modifications.
When you write a program, the classes in your program are independent of each other, and some devastating changes are closed, but they can be expanded. As in the example of a calculator In the first sentence, if you write a program to calculate addition at the beginning, you cannot modify the class for addition calculation when you want to add the subtraction function, but you can extend it, inherit it, rewrite its methods, and so on.
Therefore, when writing a program, you must meet the future and write a class as extensible, but it cannot be easily modified.
It is said that the principle of open and closed is the core of object-oriented design. What should I do?
Suddenly you think of a joke, you can go to Beijing University to learn for a year, then go to lanxiang to learn for a year, and then go to New Oriental to learn for a year. When you come out, you can use a computer to control the excavator for cooking. Is it very high-end atmosphere?
Ii. What are the application scenarios?
1. All aspects of programming;
2. Systems with relatively large business logic.
Iii. Code-on (Banking System)
In fact, the examples we wrote earlier have used the principle of open and closed. Well, maybe you have used many of them before, but you just don't know the name.
In a bank, if multiple businesses such as access are carried out in each window, the business processor must be very familiar with various businesses and be highly careful not to handle wrong businesses for others, his desktop will also require n more things.
However, if you separate these services, a window is a kind of business, the efficiency will be improved, the staff will be relaxed, and people will not have to wait so long.
Bank. Java
Public interface bank {
Public void work (INT money );
}

Deposit. Java
Public class deposit implements bank {
Public void work (INT money ){
// Todo auto-generated method stub
System. Out. println ("You saved" + money + "Yuan ");
}

}

Widthdraw. Java
Public class withdraw implements bank {
Public void work (INT money ){
// Todo auto-generated method stub
System. Out. println ("You have obtained" + money + "Yuan ");
}

}

Bankfactory. Java

// Factory type, not very familiar, right, simple factory Mode

Public class bankfactory {
Public static bank createbank (string choice ){
Bank = NULL;
If (choice. Equals ("Withdraw ")){
Bank = new withdraw ();
} Else if (choice. Equals ("deposit ")){
Bank = new deposit ();
}
Return bank;
}
}

Main. Java
Public class main {
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Bank worker1 = bankfactory. createbank ("Withdraw ");
Worker1.works (10000 );
Bank worker2 = bankfactory. createbank ("deposit ");
Worker2.work( 20000 );
}

}

Iv. Summary
The principles are set by humans and must be learned and used. The learning design pattern aims to design high-quality code, making it easy to maintain, expand, reuse, and clear. This is a common pursuit. If we have our own business needs and have our own ideas, we feel that this is a good practice. Congratulations, you are wrong. If you ask yourself a few questions, can my class be divided into multiple classes? Do my classes need to be divided into multiple classes? If the boss asks me to add other functions, will these classes be affected? When you think clearly, congratulations, you have used the design pattern perfectly.
The boundary of the open and closed mode is too vague. If the boundary is too closed, it will only be a girlfriend. People live for the whole, making friends. The so-called, the sea memory confidant, if the day is adjacent.

Fourth-open and closed Principle

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.