Six Design principles

Single responsibility principle

Richter’s substitution principle

Dependency inversion principle

Interface Isolation Principle

Demeter’s rule

Open and closed principle

define

A client should not rely on interfaces it does not need; The dependency of one class on another should be based on the smallest interface.

The sample

Take searching for quality listings

Design a quality housing condition

Public interface IGoodHouse {/** * large */ void bigArea(); /** * goodSchool(); /** * convenientTraffic(); }Copy the code

The realization of high quality housing supply

@Service public class GoodsHouseImpl implements IGoodHouse { @Override public void bigArea() { System.out.println(" large area "); } @override public void goodSchool() {system.out.println (" good "); } @override public void convenientTraffic() {system.out.println (" convenientTraffic "); }}Copy the code

Design a house search

public abstract class AbstractSearchHouse {

    protected IGoodHouse goodsHouse;

    public AbstractSearchHouse(IGoodHouse goodsHouse) {
        this.goodsHouse = goodsHouse;
    }

    public abstract void search();
}
Copy the code

The realization of house search

public class SearchHouse extends AbstractSearchHouse { public SearchHouse(IGoodHouse goodsHouse) { super(goodsHouse); } @Override public void search() { super.goodsHouse.bigArea(); super.goodsHouse.goodSchool(); super.goodsHouse.convenientTraffic(); }}Copy the code

search

public class Client { public static void main(String[] args) { IGoodHouse goodHouse = new GoodsHouseImpl(); AbstractSearchHouse searchHouse = new SearchHouse(goodHouse); searchHouse.search(); }}Copy the code

The results of

There is a problem here. For different people, the conditions for high-quality housing are different, so the interface naturally cannot meet the needs of different people to search for high-quality housing.

To optimize the

Split the interface of quality housing

Public interface IBigArea {/** * large */ void bigArea(); } public interface IGoodSchool {/** ** */ void goodSchool(); } Public interface IConvenientTraffic {/** * convenientTraffic */ void convenientTraffic(); }Copy the code

The realization of high quality housing supply

@Service public class GoodHouseImpl implements IBigArea, IGoodSchool, IConvenientTraffic {@override public void bigArea() {system.out.println (" large area "); } @override public void goodSchool() {system.out.println (" good "); } @override public void convenientTraffic() {system.out.println (" convenientTraffic "); }}Copy the code

Design a search for quality listings

public abstract class AbstractSearchHouse {

    protected IBigArea bigArea;
    protected IGoodSchool goodSchool;
    protected IConvenientTraffic convenientTraffic;

    public AbstractSearchHouse(IBigArea bigArea, IGoodSchool goodSchool, IConvenientTraffic convenientTraffic) {
        this.bigArea = bigArea;
        this.goodSchool = goodSchool;
        this.convenientTraffic = convenientTraffic;
    }

    public abstract void search();
}
Copy the code

The realization of house search

Customers search based on their own definition of a good listing

public class SearchHouse extends AbstractSearchHouse { public SearchHouse(IBigArea bigArea, IGoodSchool goodsHouse, IConvenientTraffic convenientTraffic) { super(bigArea, goodsHouse, convenientTraffic); } @Override public void search() { super.bigArea.bigArea(); super.goodSchool.goodSchool(); super.convenientTraffic.convenientTraffic(); }}Copy the code

search

public class Client { public static void main(String[] args) { IGoodHouse goodHouse = new GoodsHouseImpl(); AbstractSearchHouse searchHouse = new SearchHouse(goodHouse); searchHouse.search(); }}Copy the code

The results of

After the transformation, users can search according to their own definition of high-quality housing sources.

Pay attention to

  • Keep interfaces small, but limited. It is true that refinement of interfaces can increase programming flexibility, but if it is too small, it can result in too many interfaces and complicate the design. So do it in moderation.

  • Customizing the service for classes that depend on the interface exposes only the methods that the calling class needs and hides those it doesn’t. Only by focusing on providing custom services for a module can minimal dependencies be established.

  • Improve cohesion and reduce external interactions. Make the interface do the most with the fewest methods.