Design Patterns - seven design principles (a)

Design Patterns - seven design principles (a)

Outline

A brief seven design principles:
opening and closing principle: that all core object-oriented design, open for extension, but closed for modification
Dependency Inversion Principle: For programming interfaces, depending on the abstract rather than relying on specific
single responsibility principle: an interface only responsible for one thing, only one class causes changes in
the interface segregation principle: the use of multiple specialized interface, rather than using a general interface to
Demeter (the least known principle): only communicate with friends (member variables, input method output parameters), do not talk to strangers, control the access modifier
Richter Substitution principle: subclass can extend the functionality of the parent class, but can not change the original function of the parent class
synthetic multiplexing principle: to make use of object composition (has- a) / polymerized (contanis-a), instead of inheriting object relations to software reuse


Open Closed Principle

definition

It refers to a software entity, such as classes, modules and functions should be open for extension but closed for modification.
The so-called open and close, it is also a principle to extend and modify the behavior of the two. Emphasis is to build abstract frame, with details to achieve expansion. The system can improve software reusability and maintainability. Opening and closing the principle of object-oriented design is the most basic design principles. It guides how we build a stable and flexible system, for example: We updated version, I try not to modify the source code, but you can add new features.

In real life, for the principle of opening and closing is also reflected. For example, many Internet companies are the implementation of flexible production time information, the provisions of 8 hours a day. His point is that this provision for working eight hours a day is close, but when you come, when to go is open. Early to leave early, late and leaving late.

Examples

Implement the principle of opening and closing the core idea is to abstract-oriented programming, then we look at a piece of code:

Bookstore to sell books, for example, to create books Interface:

/**
 * @author eamon.zhang
 * @date 2019-09-25 上午10:26
 */
public interface IBook {
    // 书籍名称
    public String getName();
    // 价格
    public int getPrice();
    // 作者
    public String getAuthor();
}

Books divided into many categories, such as novels, etc., to create fiction books:

/**
 * @author eamon.zhang
 * @date 2019-09-25 上午10:30
 */
public class NovelBook implements IBook {
    // 书名
    private String name;
    // 售价
    private int price;
    // 作者
    private String author;
    // 通过构造函数传递数据数据
    public NovelBook(String name, int price, String author) {
        this.name = name;
        this.price = price;
        this.author = author;
    }
    // 获取书名
    public String getName() {
        return this.name;
    }
    // 获取价格
    public int getPrice() {
        return this.price;
    }
    // 获取作者
    public String getAuthor() {
        return this.author;
    }
}

We give fiction books now do an activity, price concessions. If you modify getPrice NovelBook in () method, certain risks would exist that may affect the result of the call elsewhere. How do we advance before without modifying the original code to achieve price this function? Now, we write a preferential logic class treatment, NovelDiscountBook class (think about why I called NovelDiscountBook, rather than call DiscountBook):

/**
 * @author eamon.zhang
 * @date 2019-09-25 上午10:36
 */
public class NovelDiscountBook extends NovelBook {
    public NovelDiscountBook(String name, int price, String author) {
        super(name, price, author);
    }

    public double getOriginPrice(){
        return super.getPrice();
    }

    public double getPrice(){
        return super.getPrice() * 0.85;
    }
}

Class structure in FIG.

image.png


Dependency Inversion Principle

definition

When the Dependency Inversion Principle (DependenceInversionPrinciple, DIP) refers to the design code structure, layer module should not rely on the underlying module, both of which should depend abstraction. Abstract should not rely on details; details should depend on the abstract. By relying inversion can reduce the coupling between the class and class, and improve system stability, improve the readability and maintainability of the code, and the program can be modified to reduce the risks caused.

Examples

We read books, for example, create a Eamon categories:

/**
 * @author eamon.zhang
 * @date 2019-09-25 上午11:09
 */
public class Eamon {
    public void readNotreDame(){
        System.out.println("Eamon 在阅读 《巴黎圣母院》");
    }

    public void readTheOldManAndTheSea(){
        System.out.println("Eamon 在阅读 《老人与海》");
    }
}

Write a test class calls it:

public static void main(String[] args) {
    Eamon eamon = new Eamon();
    eamon.readNotreDame();
    eamon.readTheOldManAndTheSea();
}

Eamon is currently readers two books. But learning is endless, I want to read the "Dragon" after Eamon read these books. This time, business expansion, our code from the bottom to the top (call level) once to modify the code. Add readTianLongBaBu Eamon method in class (), and at the top have additional calls. Thus, after the system release, in fact, it is very unstable, modifying the code will also bring unexpected risks. Next, we optimized code, create a curriculum abstract IBook interfaces:

/**
 * @author eamon.zhang
 * @date 2019-09-25 上午11:20
 */
public interface IBook {
    void read();
}

Then write NotreDameBookcategories:

/**
 * @author eamon.zhang
 * @date 2019-09-25 上午11:22
 */
public class NotreDameBook implements IBook {
    public void read() {
        System.out.println("Eamon 在阅读 《巴黎圣母院》");
    }
}

Write   TheOldManAndTheSeaBook  categories:

/**
 * @author eamon.zhang
 * @date 2019-09-25 上午11:23
 */
public class TheOldManAndTheSeaBook implements IBook{
    public void read() {
        System.out.println("Eamon 在阅读 《老人与海》");
    }
}

Modify Eamonthe class

/**
 * @author eamon.zhang
 * @date 2019-09-25 上午11:09
 */
public class Eamon {
    public void read(IBook iBook){
        iBook.read();
    }
}

View call:

public static void main(String[] args) {
    Eamon eamon = new Eamon();
    eamon.read(new NotreDameBook());
    eamon.read(new TheOldManAndTheSeaBook());
}

This time we'll view the code, Eamon think of reading any book, for the book, I just need to create a new class, told Eamon through parameter passing mode, without the need to modify the underlying code. In fact, this is a very familiar way, called dependency injection. There constructor injection way way way and setter. We look constructor injection method:

/**
 * @author eamon.zhang
 * @date 2019-09-25 上午11:09
 */
public class Eamon {

    public Eamon(IBook iBook) {
        this.iBook = iBook;
    }

    private IBook iBook;

     public void read(){
        iBook.read();
    }
}

Look calling code:

public static void main(String[] args) {
    Eamon eamon = new Eamon(new NotreDameBook());
    eamon.read();
}

According to constructor injection mode, when you call, every time create an instance. So, if Eamon is a global singleton, then we can only choose to inject Setter way to continue to modify the code Eamon class:

/**
 * @author eamon.zhang
 * @date 2019-09-25 上午11:09
 */
public class Eamon {
    private IBook iBook;
    public void setBook(IBook iBook) {
        this.iBook = iBook;
    }
    public void read(){
        iBook.read();
    }
}

Look calling code:

public static void main(String[] args) {
    Eamon eamon = new Eamon();
    eamon.setBook(new NotreDameBook());
    eamon.read();

    eamon.setBook(new TheOldManAndTheSeaBook());
    eamon.read();
}

The final class diagram

image.png

Remember : abstract basis is much more stable than with the details of the basis to build up infrastructure, so we get after needs to be oriented programming interface, the first top-level details to re-design the code structure.

statement

Part of the text reference network!

Cover map source network intrusion deleted!

Original content, forwarding please indicate the source!

Guess you like

Origin www.cnblogs.com/eamonzzz/p/11584138.html