Java Design Patterns-six principles of design patterns

Source: Internet
Author: User

The so-called No recruit wins a recruit, practice a kung fu divides into internal strength and drilling. Drilling is like the moves, is the so-called 23 kinds of design patterns. But the internal strength, is the heart, that is the 6 kinds of laws. The light will drilling that is the huaquanxiutui, the internal strength is the realm. So many design patterns, after learning 2 times, 3 times may also forget to remember only the Singleton and Factory mode. But as long as the principle of remembering, in the future design, consciously or unconsciously will use the essence of design patterns.


Six Principles of design


Single principle of responsibility

do not have more than one cause for class changes. In layman's terms, a class is responsible for only one responsibility. The origin of the problem: class T is responsible for two different duties: responsibility P1, Responsibility P2. When a class T needs to be modified due to a change in the duty P1 requirements, it is possible to cause a normal function to malfunction P2 functionality. a sentence summed up: not for the figure code, the cow champ mouth together to a class plug

The principle of the Richter replacement

1. Subclasses can implement the abstract methods of the parent class, but cannot override the non-abstract methods of the parent class.

2. Subclasses can add their own unique methods.

3. When a method of a subclass overloads a method of the parent class, the method's preconditions (that is, the parameter of the method) are more lenient than the input parameters of the parent class method.

4. When a method of a subclass implements an abstract method of the parent class, the method's post-condition (that is, the method's return value) is stricter than the parent class.

Sum up: Do not rewrite the parent class's already implemented methods, you can use the interface and other methods to bypass

Dependency Inversion principle

High-level modules should not rely on the lower layers, both should rely on their abstraction; abstractions should not depend on detail; detail should be dependent on abstraction.

Here is a Lezilai description:

Import Java.util.linkedlist;import java.util.queue;/** * There are two sequences A and b,a= (A1,A2,..., AK), b= (B1,B2,..., BK), A and B are sorted in ascending order, for 1& Lt;=i,j<=k, ask K minimum (AI+BJ), require the algorithm as efficient as possible * @author Administrator * */interface ieat{public void Eat ();//abstract Eat This action}class Eatap PLE implements ieat{@Overridepublic void Eat () {//Here is eating apple System.out.print ("eat a Apple");} Class Eatwater implements ieat{@Overridepublic void Eat () {//Here is draft System.out.print ("Dringk Water");}} public class Human{public void DoSomething (IEAT IEAT)//I love to eat, what to eat, see what is passed {ieat.eat ();} /*public void dosomething (String food)//I love to eat, what to eat, see what to pass {if (Food.equals ("Apple")) {//Eat Apple}if (food.equals ("Water")) { Water}}*/public static void main (string[] args) {Human human=new Human ();/*human.dosomething ("Apple"); Human.dosomething ("Water"); *///give you an Apple human.dosomething (new eatapple);//give you some water human.dosomething (new Eatwater ());}}

The comments are the methods we use most often. This method is not suitable for expansion, because if you want to eat bananas, eat watermelon, but also in the dosomething to write a bunch of judgments. It was written, and it was mixed.

So one sentence summed up: The use of abstract interface to describe the same action, reduce the implementation of this action between the person and the degree of coupling


Interface Isolation principle


The client should not rely on interfaces it does not need, and the dependency of one class on another should be based on the smallest interface. The problem is that class A relies on class B through interface I, Class C relies on class D through interface I, and if interface I is not the smallest interface for Class A and Class B, then Class B and Class D must implement methods that they do not need. A sentence summary:like fish and people two categories, fish is swimming and gills breathing two movements, people are walking and eating two movements, these actions can not be written in an interface, the four actions are included. To be split into two interfaces specifically for fish and people.



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.