Seven principles of design patterns (1.2)

--- --- restore content begins

The core idea of ​​the design principles

The purpose of design principles is to build a more stable and robust software. And the software like such a requirement

  • Loosely coupled
  • Scalability

1. Single Responsibility Principle


What is the single principle of responsibility?

In the Java language: a class is only responsible for one function.

The purpose of the single responsibility principle

By reducing the complexity of the class of the class to increase readability, scalability

Single particle duties (classes and methods)

Typically a single compliance with the principle of duty in the case of the class size, but when the code logic is sufficiently simple, can maintain a single method on the principle of duty.



2. Interface Segregation Principle


Interface and Interface Isolation

One of the roles of the interface specification is based, loose coupling type, so as to realize loosely coupled software.

In other words, the interface isolation is also a realization of the principle of single responsibility.

The purpose of the interface segregation principle

Interface segregation principle requirement is a clear interface functions as an interface subdivision.

Refined interface directly cause the entire system flexibility, but will also bring structural complexity.

For example, the 10 pre-defined interfaces, in order to meet isolation and interface into the interface 20. Then the class will become more flexible in the realization of the interface, but the interface will lead to an increase in the complexity of the structure.




3. Dependency Inversion Principle


Depends on the abstract, do not rely on specific.


What is the Dependency Inversion?

Class dependent changes in dependence on the interface between classes.

[From Baidu Encyclopedia]

purpose

Decoupling, enhancement module reusability.

Thought the Dependency Inversion Principle Spring IOC container mechanism echoes class interface reduces the dependence of the coupling between classes, the IOC and dependency class for the container likewise solves the problem of the high coupling between classes.


Dependence passed three ways

  • Interface transfer
  • Passing the resulting
  • setter transfer


//接口传递
interface Car
{
    void drive();
}

interface Person
{
    void lunch(Car car);
}


//构造方法传递

interface Bicycle
{
    void ride();
}

class Tom
{
    private Bicycle bicycle;

    Tom(Bicycle bicycle)
    {
        this.bicycle=bicycle;

    }

}

//setter方法传递

interface Weather
{
    void sun();
}

class Today
{
    private Weather weather;

    public void setWeather(Weather weather) {
        this.weather = weather;
    }
}



4. Richter substitution principle


Advantages and Disadvantages of inheritance

Lee: code-reuse, more convenient to implement the extensions.
Disadvantages: enhancing the coupling between classes


What is the Richter substitution principle?

In subclasses try not to override the parent class method.

If you had to rewrite, you can extract the common base class inheritance. It may also be obtained by polymerizing, dependent, an alternative embodiment of a combination of inheritance.

class A
{
    public void a1(){
        
    }
    public void a2(){
        
    }
    
}

class B extends A
{
    @Override
    public void a1()
    {
        
    }
    
}

//提取基类后

class Base
{
    public void a2
    {
        
    }
}
class A extends Base
{
    public void a1(){

    }
}

class B extends Base
{
    public void a1(){

    }
}



The principle of opening and closing


What is the principle of opening and closing?

Open for extension, but closed for modification.

In layman's terms, when the software functionality needs to be extended, to maximize the code, rather than modifying the original code.


for example

abstract class Action
{
    abstract public void action();
}

class Describe
{
    public void describe(Action action)
    {
        action.action();
    }
}

class A extends Action
{
    public void action()
    {
        System.out.println("跑步");
    }
}
class B extends Action
{
    public void action()
    {
        System.out.println("瑜伽");
    }
}
class C extends Action
{
    public void action()
    {
        System.out.println("健身");
    }
}

class X extends Action
{
    public void action()
    {
        System.out.println("xxx");
    }
}



public class Main
{
    public static void main(String[] args)
    {
        Describe describe = new Describe();
        describe.describe(new A());
        describe.describe(new B());
        describe.describe(new C());
        describe.describe(new X());
    }
}

Define an abstract class, including action method, ABC class has its own behavior, override methods inherited after the abstract class. These simply add a new image as X when the need to extend the class of new features on it. Substantially without changing any position, but it extends the functionality.


6. Dmitry principle (the principle of least knowledge)


What is Dimitris principle?

An object should be kept to a minimum understanding of other objects.


Manifestations

Only direct communication with friends.

Direct friends: In addition to appear in the local variables of the class, are direct friends.

In other words: the Law of Demeter requires local variable is best not to appear strange class.

purpose

Reduce the coupling between classes. (Coupling can only minimize, not eliminate)


7. Synthesis of multiplexing principles

Try to use synthetic / polymeric instead of inheritance.

Synthesis / polymerization: establish contact manner between the class of some sort. Coupled lower than inheritance.





References:
[six principles of design patterns - Interface Segregation Principle (ISP) https://www.cnblogs.com/muzongyan/archive/2010/08/04/1792528.html]
[Dependency Inversion Principle] https: / /baike.baidu.com/item/%E4%BE%9D%E8%B5%96%E5%80%92%E7%BD%AE%E5%8E%9F%E5%88%99/6189149?fr= aladdin # 2
[Object-oriented principle of one https://www.cnblogs.com/alunchen/p/7110116.html] dependency inversion principle
[of dependency inversion principle (DIP) and Ioc, DI, an understanding of some of the containers Ioc] https : //www.cnblogs.com/lichenwei/p/3943960.html
[Richter] Substitution principle https://www.sogou.com/link?url=hedJjaC291OfPyaFZYFLI4KQWvqt63NBG8sHIIPQJ6S7Q4PYkUMkig ..

--- end --- restore content

The core idea of ​​the design principles #

The purpose of design principles is to build a more stable and robust software. And the software like such a requirement

  • Loosely coupled
  • Scalability

1. Single Responsibility Principle


What is the single principle of responsibility?

In the Java language: a class is only responsible for one function.

The purpose of the single responsibility principle

By reducing the complexity of the class of the class to increase readability, scalability

Single particle duties (classes and methods)

Typically a single compliance with the principle of duty in the case of the class size, but when the code logic is sufficiently simple, can maintain a single method on the principle of duty.



2. Interface Segregation Principle


Interface and Interface Isolation

One of the roles of the interface specification is based, loose coupling type, so as to realize loosely coupled software.

In other words, the interface isolation is also a realization of the principle of single responsibility.

The purpose of the interface segregation principle

Interface segregation principle requirement is a clear interface functions as an interface subdivision.

Refined interface directly cause the entire system flexibility, but will also bring structural complexity.

For example, the 10 pre-defined interfaces, in order to meet isolation and interface into the interface 20. Then the class will become more flexible in the realization of the interface, but the interface will lead to an increase in the complexity of the structure.




3. Dependency Inversion Principle


Depends on the abstract, do not rely on specific.


What is the Dependency Inversion?

Class dependent changes in dependence on the interface between classes.

[From Baidu Encyclopedia]

purpose

Decoupling, enhancement module reusability.

Thought the Dependency Inversion Principle Spring IOC container mechanism echoes class interface reduces the dependence of the coupling between classes, the IOC and dependency class for the container likewise solves the problem of the high coupling between classes.


Dependence passed three ways

  • Interface transfer
  • Passing the resulting
  • setter transfer


//接口传递
interface Car
{
    void drive();
}

interface Person
{
    void lunch(Car car);
}


//构造方法传递

interface Bicycle
{
    void ride();
}

class Tom
{
    private Bicycle bicycle;

    Tom(Bicycle bicycle)
    {
        this.bicycle=bicycle;

    }

}

//setter方法传递

interface Weather
{
    void sun();
}

class Today
{
    private Weather weather;

    public void setWeather(Weather weather) {
        this.weather = weather;
    }
}



4. Richter substitution principle


Advantages and Disadvantages of inheritance

Lee: code-reuse, more convenient to implement the extensions.
Disadvantages: enhancing the coupling between classes


What is the Richter substitution principle?

In subclasses try not to override the parent class method.

If you had to rewrite, you can extract the common base class inheritance. It may also be obtained by polymerizing, dependent, an alternative embodiment of a combination of inheritance.

class A
{
    public void a1(){
        
    }
    public void a2(){
        
    }
    
}

class B extends A
{
    @Override
    public void a1()
    {
        
    }
    
}

//提取基类后

class Base
{
    public void a2
    {
        
    }
}
class A extends Base
{
    public void a1(){

    }
}

class B extends Base
{
    public void a1(){

    }
}



The principle of opening and closing


What is the principle of opening and closing?

Open for extension, but closed for modification.

In layman's terms, when the software functionality needs to be extended, to maximize the code, rather than modifying the original code.


for example

abstract class Action
{
    abstract public void action();
}

class Describe
{
    public void describe(Action action)
    {
        action.action();
    }
}

class A extends Action
{
    public void action()
    {
        System.out.println("跑步");
    }
}
class B extends Action
{
    public void action()
    {
        System.out.println("瑜伽");
    }
}
class C extends Action
{
    public void action()
    {
        System.out.println("健身");
    }
}

class X extends Action
{
    public void action()
    {
        System.out.println("xxx");
    }
}



public class Main
{
    public static void main(String[] args)
    {
        Describe describe = new Describe();
        describe.describe(new A());
        describe.describe(new B());
        describe.describe(new C());
        describe.describe(new X());
    }
}

Define an abstract class, including action method, ABC class has its own behavior, override methods inherited after the abstract class. These simply add a new image as X when the need to extend the class of new features on it. Substantially without changing any position, but it extends the functionality.


6. Dmitry principle (the principle of least knowledge)


What is Dimitris principle?

An object should be kept to a minimum understanding of other objects.


Manifestations

Only direct communication with friends.

Direct friends: In addition to appear in the local variables of the class, are direct friends.

In other words: the Law of Demeter requires local variable is best not to appear strange class.

purpose

Reduce the coupling between classes. (Coupling can only minimize, not eliminate)


7. Synthesis of multiplexing principles

Try to use synthetic / polymeric instead of inheritance.

Synthesis / polymerization: establish contact manner between the class of some sort. Coupled lower than inheritance.





References:
[six principles of design patterns - Interface Segregation Principle (ISP) https://www.cnblogs.com/muzongyan/archive/2010/08/04/1792528.html]
[Dependency Inversion Principle] https: / /baike.baidu.com/item/%E4%BE%9D%E8%B5%96%E5%80%92%E7%BD%AE%E5%8E%9F%E5%88%99/6189149?fr= aladdin # 2
[Object-oriented principle of one https://www.cnblogs.com/alunchen/p/7110116.html] dependency inversion principle
[of dependency inversion principle (DIP) and Ioc, DI, an understanding of some of the containers Ioc] https : //www.cnblogs.com/lichenwei/p/3943960.html
[Richter] Substitution principle https://www.sogou.com/link?url=hedJjaC291OfPyaFZYFLI4KQWvqt63NBG8sHIIPQJ6S7Q4PYkUMkig ..

Guess you like

Origin www.cnblogs.com/noneplus/p/11323761.html