Case study: The job is not mistaken for postgraduate examination. The failure of the postgraduate examination, the work is not ready, this is not
Open-closed principle: Software entities (classes, modules, functions, etc.) should be extensible, but not modifiable.
Two characteristics: The corresponding extension is open and is closed for changes.
What is the design to be able to face the change in demand can be relatively stable, so that the system can be in the first version of the new version of the continuous release? : Open-closed principle
Case: Company staff late: What is not change is the performance, can change the time of work
No matter how closed the module is, there will be some changes that cannot be closed to it. Since it is impossible to be completely closed, the designer must make a choice as to which block of changes the module he designs should be closed. You must first guess what kinds of changes are most likely to occur, and then construct abstractions to isolate those changes.
In the face of demand, changes to the program are made by adding new code, rather than changing the existing code.
Open-closed principle is the core of object-oriented design. Following this principle brings you the benefits of maintainability, scalability, reusability, and flexibility. Developers should abstract only those parts of the program that are frequently changing, but it is also not a good idea to deliberately abstract each part of the application.
development of the closure principle (open-closed Principle OCP)
Software entities (Classes,modules,functions etc) should open for extension, but close for modification.
What do you mean?
The so-called open closure principle is that software entities should be developed for expansion and closed for modification. The open closure principle is the core of all object-oriented principles. The goal of software design itself is to package change, reduce coupling, and open closed principle is the most direct embodiment of this goal.
The principle of open closure is mainly embodied in two aspects:
Opening up to extensions means that existing code can be extended to accommodate new situations when there are new requirements or changes.
Closing a modification means that once the class is designed, it can work independently of the class, rather than making any modifications to the classes.
Why should we use the principle of open closure?
Software requirements are always changing, there is no software in the world is unchanged, so for software designers, must not need to modify the original system, the implementation of flexible system expansion.
How do you open to extensions, to modify the closed?
The core idea of open closure is to abstract programming, not specific programming, because the abstraction is relatively stable. Let the class depend on the fixed abstraction, so the modification is closed, but through the object-oriented inheritance and polymorphism mechanism, can realize the inheritance of the abstract body, by covering its methods to change the inherent behavior, to implement a new extension method, so for the extension is open.
Classes that violate this principle must be improved by refactoring. The design patterns commonly used in implementation are the template method mode and the strategy mode. Package change is an important means to realize this principle, and encapsulate the frequently changing state as a class.
Take the bank clerk as an example
The design of the OCP is not implemented:
public class Bankprocess
{//Deposit
public void Deposite () {}
Withdrawal
public void Withdraw () {}
Transfer
public void Transfer () {}
}
public class Bankstaff
{
Private bankprocess Bankpro = new bankprocess ();
public void Bankhandle (client client)
{
Switch (client. Type)
{//Deposit
Case "Deposite":
Bankpro. Deposite ();
Break
Withdrawal
Case "Withdraw":
Bankpro. Withdraw ();
Break
Transfer
Case "Transfer":
Bankpro. Transfer ();
Break
}
}
}
This design is obviously a problem, the current design only deposits, withdrawals and transfers three functions, in the future if the business increased, such as the increase of the subscription fund function, financial functions, etc., it is necessary to modify the Bankprocess business class. Our analysis of the above design can not be found in the inability to encapsulate the business in a class, violating the principle of single responsibility, and there are new requirements, the need to modify the existing code is in violation of the open closure principle.
From an open and closed perspective, the most likely expansion in a banking system is the addition or alteration of business functions. The business process should be implemented as part of the extension. When there are new features, there is no need to re-comb the existing business and then make big changes to the system.
How can coupling and flexibility be achieved?
That is abstraction, the business function is abstracted as an interface, when the salesman relies on fixed abstraction, the modification is closed, and through inheritance and polymorphism inheritance, from the abstraction of the extension of the new implementation is to expand the open.
The following are the OCP-compliant designs:
First, declare a business processing interface
public interface ibankprocess{void Process ();
public class Depositprocess:ibankprocess
{
public void Process ()
{//Deposit-taking business
Console.WriteLine ("Process Deposit");
}
}
public class Withdrawprocess:ibankprocess
{
public void Process ()
{//Processing of withdrawal transactions
Console.WriteLine ("Process withdraw");
}
}
public class Transferprocess:ibankprocess
{
public void Process ()
{//Transfer business
Console.WriteLine ("Process Transfer");
}
}
public class Bankstaff
{
Private ibankprocess Bankpro = null;
public void Bankhandle (client client)
{
Switch (client. Type)
{//Deposit
Case "Deposit":
Userproc = new Deposituser ();
Break
Transfer
Case "Transfer":
Userproc = new Transferuser ();
Break
Withdrawal
Case "Withdraw":
Userproc = new Withdrawuser ();
Break
}
Userproc.process ();
}
}
So when the business changes, only need to modify the corresponding business implementation class, other unrelated business will not have to modify. When the business grows, it is only necessary to increase the implementation of the business.
Design recommendations:
The principle of open closure is the most important design principle, the principle of Liskov substitution and the principle of synthetic/aggregate multiplexing provide the guarantee for the open and closed principle.
It can be reconstructed by template method mode and strategy mode, and the design idea of closed and extended opening is realized.
Package change is an important means to realize the principle of open closure, and it is generally encapsulated as an abstraction for the frequently changing state, such as ibankprocess interface in banking business.
Reject the misuse of abstraction, and only the parts that are constantly changing are abstracted.
Open-closed principle