solid principles of 02 java

problem

What is the solid principles of java

answer

  • Single Responsibility Principle: single responsibility principle
    a method do one thing only a duty, a kind of thing only a group of related undertake similar duties. If more than one way to do things, when people want to use this method, first thing, other things have to happen this way you have to split this method. If this method is defined at the time to do one thing, then it will not be split, it is also easy to maintain a lot for the class also have this constraint. This is the single responsibility principle. Consider the following code:
//下面这个方法就不符合单一职责原则,因为调用者只想使用该方法创建一个数组,
//但是莫名其妙该数组被打印了出来;应该将打印的语句抽象成另外一个方法。
public static int[] createArrays(int length)
	{
		int[] arrays = new int[length];
		Random rd = new Random();
		for (int i = 0; i < arrays.length; i++)
		{
			arrays[i] = rd.nextInt(100);
		}
                //这一句话应该作为另一个方法被抽象出去
                System.out.println(Arrays.toString(arrays));
		return arrays;
	}
  • Open Closed Principle: the principle of opening and closing the
    software to modify the source code should be shut down, when the expansion of new feature development, that is to say the expansion of the best new features is the new code. So what to do to do this, interface-oriented programming. Before the so-called interface-oriented programming is to complete a function, first define a corresponding interface, be sure the caller call interface to use this feature. Consider the following code:
* Operator.java
public interface Operator {
    int exec(int a,int b);
}

* Add.java
public class Add implements Operator {
    @Override
    public int exec(int a, int b) {
        return a+b;
    }
}
* Test.java
public class Test {
    public static void main(String[] args) {
        Operator operator = new Add();
        System.out.println(operator.exec(1,2));
    }
}

If at this time to increase the multiplication, only you need to add a Multy.java class without the need to modify the original Add.java

  • Liskov Substitution Principle: Richter Alternatively principle
    succession has its advantages, it is possible to reduce the code duplication, increase multiplexing; however, inherited break encapsulation, inheritance and therefore combine more than often emphasized, it is both to increase reuse does not destroy the encapsulation. LSP principle of popular terms is where all the parent class appears, can sub-class instead, there are two specific requirements: subclass must fully implement the parent class method, when invoked by calling the parent class; subclass parent implementation the best method of amplifying an input parameter.
  • Law of Demeter: Demeter
    also known as the least knowledge of the principle that a class with other classes to know as little as possible, this way, the link between reduced to the class will have to have some degree of coupling it will be much lower. Contact less, reduces the complexity, stability of the system would be much better, namely Road to SR.
  • Interface Segregation Principle: Interface Segregation Principle
    This is a single line of succession duties, responsibilities, said that if a single class, then the ISP is specific to the interface. It can be divided into a plurality of the interface into multiple interfaces Interface will not be using a single interface. If you have to use a single interface, users will have to rely on a method he does not, then the user will have to bear the impact of those changes in his approach to his need. Thus, the interface can be split if it should be broken, in other words, the method is less interface, to concentrate. This is the interface segregation principle.
  • Dependence Inversion Principle: Dependency Inversion Principle
    refers to high-rise should not rely on the underlying directly, but should depend on the underlying abstraction. Abstract should not rely on the details, the details should depend abstract, abstract to be more stable and reliable than the details. The key is to embody a DIP-oriented programming interface.
    Six explain the principles of mutual overlap and, in order to improve the robustness of the code are, maintainability, and extensibility.

Guess you like

Origin www.cnblogs.com/alichengxuyuan/p/12577548.html
Recommended