5 Principles of OOP: open closure principle--OCP

Source: Internet
Author: User
Tags abstract closure extend getmessage

PHP 5 Principles for object-oriented OOP: Open closure principle--OCP

I. Introduction of OCP (ocp--open-closed Principle): PHP Open Source com
Software entities (classes,modules,functions,etc.) should is open for extension, but closed for modification.
Software entities should be open to extensions and closed to modifications, i.e. software entities should not be modified (in. NET which may be achieved by proxy mode).
Open for extension: When new requirements arise, you can extend the existing model to achieve the goal.
Close for modification: No modifications are allowed for existing binary code, such as Dll,jar.

Ii. Examples of OCP:
1. Example one PHP open source com
If we are going to write a payroll tax, the payroll tax in different countries have different rules of calculation, if we do not adhere to the OCP, directly write a class package of payroll tax calculation method, and each country on payroll tax specific implementation details are different. If we allow the changes, that is, all the payroll taxes (China payroll tax, US payroll tax, etc.) that are needed in the current system are implemented in one class, no one can guarantee that the future system will not be sold to Japan, and that if a new payroll tax is to be implemented in the software, this kind of payroll tax will have to be found. Add Japanese tax implementation details to each method and recompile to DLL. Although in the. NET in the running environment, we just have to overwrite the new DLL to the original DLL, does not affect the normal operation of existing programs, but every time a new situation to find out the class file, add new implementation details, this class of files continue to expand, later maintenance becomes more and more difficult, Nor does it satisfy the single principle of responsibility (SRP) that we have previously said, because changes in payroll taxes in different countries will cause a change in this category. If we stick to the OCP when we design this class, the public method of payroll tax is abstracted out to make an interface, the closed modification, the client (the class object using the interface) relies only on this interface to achieve the payroll tax that it needs, and if the system needs to add a new payroll tax later, As long as the extension of a country-specific payroll tax implements our previously defined interface, it can be used normally without having to re-modify the original class file.
PHP Open Source com
2. Example Two
The following example is neither open nor closed, because both the client and server are concrete classes, and if I want the client to use a different server class then modify all the server classes in the client class to the new server class.

Class Client
{
Server server;
void GetMessage ()
{
Server. Message ();
}
}

Class Server
{
void Message ();
}


The following is the implementation of the revised OCP principle, we see that the server class is inherited from Clientinterface, but Clientinterface is not called Serverinterface, The reason is that we want clientinterface to be fixed for the client and only change the server. This actually becomes a strategy mode (GOF strategy)


Interface Clientinterface
{
public void Message ();
Other functions
}

Class Server:clientinterface
{
public void Message ();
}

Class Client
{
Clientinterface CI;
public void GetMessage ()
{
Ci. Message ();
}
public void Client (Clientinterface paramci)
{
CI=PARAMCI;
}
}

Then in the main function (or the main control side)
public static void Main ()
{
Clientinterface ci = new Server ();
On top If there is a new server class just replace the server () on the line.
Client client = new Client (CI);
Client. GetMessage ();
}


3. Example Three
Use template method to implement OCP:
Public abstract class Policy
{
Private int[] I ={1, 1234, 1234, 1234, 132};
public bool Sort ()
{
Sortimp ();
}
Protected virtual bool Sortimp ()
{

}
}

Class Bubbleimp:policy
{
protected override bool Sortimp ()
{
Bubble sort
}
}
Class Bintreeimp:policy
{
protected override bool Sortimp ()
{
Sorting by dichotomy
}
}

Implemented in the main function
static void Main (string[] args)
{
If you want to use bubble sort, just change the bintreeimp below to Bubbleimp
Policy sort = new Bintreeimp ();
Sort. Sort ();
}


Iii. Advantages of OCP:
1, reduce the coupling between the various parts of the program, so that the exchange of program modules may be;
2, make each part of the software Easy Unit test, through the compilation and interface consistent simulation class (mock), can easily realize the unit test of the software parts;
3, conducive to the realization of the software module call, software upgrade can only deploy the changes in the part, without affecting the other parts;

Iv. using the OCP Note: PHP Open source com
1, the key to realize the OCP principle is abstraction;
2, two security to achieve the opening and closing principle of the design mode is: Strategy pattern (strategy mode), template Methord (template method mode);
3, according to the opening and closing principle, we try not to modify the class, only extend the class, but in some cases there will be some more bizarre situation, you can use several classes to complete the combination;
4, the possible changes in the package into an object, such as: state, message, algorithm, data structure and so on, packaging change is to achieve the "open and close principle" an important means, such as the constant changes in the state values, such as temperature, pressure, color, integration, ranking and so on, you can use these as independent properties, If there is a relationship between the parameters, it is necessary to abstract. For the behavior, if it is basically unchanged, it can be directly used as the method of the object, otherwise consider abstraction or encapsulation of these behaviors;
5, in many ways, OCP is the core of object-oriented design. Following this principle leads to the enormous benefits that object-oriented technology claims (flexibility, reusability, and maintainability). However, it is not a good idea to arbitrarily abstract every part of an application. You should simply abstract the part of the program that is showing frequent changes. Rejecting immature abstractions is as important as abstraction itself;

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.