[Original · tutorial · serialization] "Android's big talk Design Model"-Design Principles chapter 3: open and closed principle Sun Wukong's role of Ma Wen

Source: Internet
Author: User
<Big talk design model> description and copyright notice of this tutorial

L this document references and uses free images and content on the Internet, and is released in a free and open manner, hoping to contribute to the mobile Internet and the smart phone age! This document can be reproduced at will, but cannot be used for profit.

L if you have any questions or suggestions about this document, go to the official blog

Http://www.cnblogs.com/guoshiandroid/ (with the following contact information), we will carefully refer to your suggestions and modify this document as needed to benefit more developers!

L The latest and complete content of "big talk design mode" will be regularly updated on the official blog of guoshi studio. Please visit the blog of guoshi studio.

Http://www.cnblogs.com/guoshiandroid/get more updates.

Guoshi studio is a technical team dedicated to enterprise-level application development on the Android platform. It is committed to the best Android Application in China.ProgramDevelopment institutions provide the best Android enterprise application development training services.

Official contact information for Enterprise Training and Development Cooperation:

Tel: 18610086859

Email: hiheartfirst@gmail.com

QQ: 1740415547

QQ: 148325348

Guoshi studio is better for you!


 

  View other parts:Overall description and Chapter index of this tutorial

PDF download link

Open and closed Principle Sun Wukong serves Ma Wen 

Use Cases:

Sun Wukong received the Dinghai magic needle from donghailong Palace and returned to Huaguo mountain to live a free life with his subordinates. It was not a long time, because he deleted the lists of himself and all the monkeys in Huaguo Mountain in hell, and also took the Dinghai magic needle, and soon he was taken to heaven by King Yan and Dragon King. The Jade Emperor was about to go to arrest the Demon monkey for sin. Long Wang urged him to stop. Long Wang said Sun Wukong had a wide range of gods, and Yan Wang also agreed. The Jade Emperor hesitated and asked them how to do well. Taibai jinxing said that it would be better to seal him from the official post in the tiangu, which is regarded as an official seal. In fact, he actually suppressed it in the dark. Yu Di deeply agrees. But what do Sun Wukong do? Yu Di couldn't figure out any number of positions at the moment, so Xuan xianqing entered the dynasty to discuss the matter together. Yu Di asked: "Zhong Aiqing, where can I find a vacant position in Tiancheng? Give the Demon monkey an official ." All people say that the positions in tianting are full and there are no vacant positions. At the moment, everyone did not know how to do well. The incident was too exciting for Venus and reported to the Jade Emperor: "I have reported to the Jade Emperor. In view of the overwhelming number of employees in the heaven court, it is better to seal him a job in Ma Wen." Yu Di asked: "Ma Wen? What is a job ?" Taibai jinxing said that Ma Wen was used to manage tianma. At present, tianma was not managed. The establishment of a new tianma Wen position in tianting is conducive to the management of tianma, and does not affect the existing positions in tianting, finally, it can appease the Demon monkey.

The reason why the above positions are difficult to arrange is that it is caused by the official system of the entire court. In heaven, the higher the mana, the lower the corresponding mana. In addition, because of the high capability, more and more Xiantao and Jindan of taishangjun become more and more powerful, which leads to higher and higher positions. On the other hand, as the mana grows, the service life is getting longer and longer. What is the result of this? This leads to a tenure system! A job is almost always under the control of the corresponding XianRen. In tianting, It is a radish, and once the official role is determined, it is difficult to change it! Let's say that Tota Li Tianwang will always be Li Tianwang, and no one can replace him! How can we arrange Sun Wukong now? To ensure that tianting has no influence on the interests of both the order and the fairy, but also to appease Sun Wukong, you have to create a new position!

Once you hear the thoughts of Taibai jinxing, Yudi is happy. Send someone to invite Sun Wukong immediately. Sun Wukong has long heard that tianting is fun, and there are still officials in tianting. In addition, he was overjoyed and went to the office happily.

Definition: 

Open-closed principle: If a software entity is open to the extension, the modification is disabled. Open to expansion means that when there are new demands or changesCodeExtension to adapt to the new situation. Closed modification means that once the class is designed, the work can be done independently without any modification to the class.

The open and closed principle is the core of all object-oriented principles.

"Requirements are always changing ".

"No software in the world remains unchanged ".

How can we achieve the principle of openness and closure? The answer is: encapsulate changes, rely on interfaces and abstract classes, instead of depending on specific implementation classes. Programming for interfaces and abstract classes is not applicable to specific implementations. Because the interfaces and abstract classes are stable, they are a kind of commitment to the client, which is relatively unchanged. If you need to expand or develop new functions in the future, you only need to implement or inherit interfaces or abstract classes to overwrite or expand new functions. This will not affect new functions at the same time. This makes it easy to open the extension and disable the modification.

Actually, the absolutely closed system does not exist. No matter how close we are, a system will always change, "there is no software in the world", and "the demand is always changing ". What we need to do is not to eliminate changes, but to isolate and encapsulate changes. We cannot control the changes, but we can predict where the change will occur. Abstract The areas to be changed, so that we can expand as much as possible when we face changes, without changing future code.

As shown in:

Story analysis:

Sun Wukong, presented by Taibai Venus, is a good example of the open and closed principle.

Why?

This should also begin with the officialdom mechanism of the court. In the court, the higher the mana, the lower the corresponding mana. In addition, because of the high capability, more and more Xiantao and Jindan of taishangjun become more and more powerful, which leads to higher and higher positions. On the other hand, as the mana grows, the service life is getting longer and longer. What is the result of this? This leads to a tenure system! A job is almost always under the control of the corresponding XianRen. In tianting, It is a radish, and once the official role is determined, it is difficult to change it! Let's say that Tota Li Tianwang will always be Li Tianwang, and no one can replace him! How can we arrange Sun Wukong now? To ensure that tianting has no influence on the interests of both the order and the fairy, but also to appease Sun Wukong, you have to create a new position!

To sum up, tianting has changed its order and expanded the position of Ma Wen to Sun Wukong. In addition, this expansion will not affect other orders of heaven. This is not to close the modification. Is it open to the extension ?!

At the same time, a new "Sun Wukong second" may appear soon, and long Wang may have to come to heaven again, "demand is always changed "! At this time, you only need to follow the same ideas for Sun Wukong to solve such changes.

Let's revisit the following:

"Requirements are always changing ."

"No software in the world remains unchanged ."

"For abstract programming, do not implement programming ."

As shown in:

JavaCode implementation: 

Interface for creating a job:

PackageCom. dieremng. designpattern. OCP;

/*

* Job Interface

*/

Public InterfacePosition {

/*

* Define job responsibilities

*/

Public VoidDuty ();

}

Taibai Venus, Tota Li Tianwang, and Ma Wen respectively implement the above interfaces. The Code is as follows:

package COM. dieremng. designpattern. OCP. impl;

Import COM. dieremng. designpattern. OCP. position;

/*

* Taibai jinxing's position implementation

*/

Public class taibaijinxin implements position {

Public void duty () {

system. out . println ("this is too white Venus ");

}

}

PackageCom. dieremng. designpattern. OCP. impl;

 

ImportCom. dieremng. designpattern. OCP. position;

/*

* Tota Li Tianwang's position Realization

*/

Public ClassTuotalitianwangImplementsPosition {

 

Public VoidDuty (){

System.Out. Println ("here is Tota Li Tianwang ");

}

 

}

 

 

PackageCom. dieremng. designpattern. OCP. impl;

 

ImportCom. dieremng. designpattern. OCP. position;

/*

* Ma Wen's position Realization

*/

Public ClassBimawenImplementsPosition {

 

Public VoidDuty (){

System.Out. Println ("here is Jack Ma Wen ");

}

 

}

 

 

Create a test class with the following code:

PackageCom. dieremng. designpattern. OCP. client;

 

ImportCom. dieremng. designpattern. OCP. position;

ImportCom. dieremng. designpattern. OCP. impl. bimawen;

ImportCom. dieremng. designpattern. OCP. impl. taibaijinxin;

ImportCom. dieremng. designpattern. OCP. impl. tuotalitianwang;

 

Public ClassPositiontest {

 

/**

*@ ParamARGs

*/

Public Static VoidMain (string [] ARGs ){

Position taibaijinxin =NewTaibaijinxin ();

Position tuotalitianwang =NewTuotalitianwang ();

Position bimawen =NewBimawen ();

 

Taibaijinxin. Duty ();

Tuotalitianwang. Duty ();

Bimawen. Duty ();

}

 

}

The program running result is as follows:

Here is too white Venus

This is Tota Li Tianwang.

Here is Ma Wen

 

Existing applications: 

The open and closed principle is the core of all object-oriented principles.

Software Analysis, Design, coding, maintenance, and other stages of the life cycle always strive to make changes close, open to expansion.

The various stages of the life cycle of the famous Baba Sports Network follow the principle of opening and closing. It makes basic CRUD operations into an interface and adopts the generic technology introduced by JDK 5, this ensures that you only need to implement this class for basic addition, deletion, modification, and query operations in the future. However, with the introduction of generic technology and the abstract Implementation of interfaces in the background, you can operate databases without having to write a line of code. If you need to extend your business in the future, you only need to extend your inheritance to expand your own unique operations, greatly improving the production efficiency.

Tip: 

The key to following the open and closed principle is to rely on abstraction. But does it always follow the open and closed principle if it depends on abstraction? What is more important here is the separation and encapsulation of duties. Only the elements that are changing and unchanged are separated and the changes are abstracted. This abstraction can use interfaces, you can also use abstract classes to separate abstract programming, rather than specific programming, so that we can truly follow the open and closed principle.

Note: This document references and uses free images and content on the Internet, released in a free and open manner, hoping to contribute to the mobile Internet and the smart phone age! This document can be reproduced at will, but cannot be used for profit.

Related Article

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.