Seven principles of OOP program

Open Closed Principle

Equivalent to the principle of opening and closing the ancestor of all the principles advocated closed for modification, open to expansion.

Richter substitution principle

When there are two classes inheritance, the subclass can not modify the methods and variables of the parent class ; Richter replace the replacement means: when the parent places have appeared, the parent can be replaced into sub-categories, but also for the program no impact, which followed the Richter substitution principle; when replaced by a subclass of program impact, indicating the subclass modify the parent class, it does not follow the Richter substitution principle;

Dependency Inversion Principle

Dependency Inversion Principle is an implementation of the principle of opening and closing, also advocated expanding open but closed for modification. Its core idea is the face of the programming interface, do not face specific implementation program .

From the C language Chinese network
From the C language Chinese network

This is a follow Dependency Inversion Principle of UML diagrams, the original words when customers buy goods, shopping this method to be passed into the corresponding shop, shop when you want to change, we must modify Cusromer this class of shopping method, and now as long as the definition of a shop interfaces, all the shops are methods to achieve this interface, shopping method as long as the customer class of incoming shop this interface class on it. Then the concrete realization, to where to buy, which was introduced to a shop on it, without modifying the method Cusromer this class;

//代码来之'C语言中文网'
public class DIPtest
{
    public static void main(String[] args)
    
{
        Customer wang=new Customer();
        System.out.println("顾客购买以下商品:"); 
        wang.shopping(new ShaoguanShop()); 
        wang.shopping(new WuyuanShop());
    }
}
//商店
interface Shop
{
    public String sell()//卖
}
//韶关网店
class ShaoguanShop implements Shop
{
    public String sell()
    
{
        return "韶关土特产:香菇、木耳……"
    } 
}
//婺源网店
class WuyuanShop implements Shop
{
    public String sell()
    
{
        return "婺源土特产:绿茶、酒糟鱼……"
    }

//顾客
class Customer
{
    public void shopping(Shop shop)
    
{
        //购物
        System.out.println(shop.sell()); 
    }
}

//输出
顾客购买以下商品:
韶关土特产:香菇、木耳……
婺源土特产:绿茶、酒糟鱼……
复制代码

Single Responsibility

Single Responsibility requires a class is only responsible for a duty ; this sounds simple, but the practice is very difficult to grasp. Because of this responsibility in China is a very abstract concept, Chinese culture is a very rich country, like the Zen >> << design patterns in this book mentioned examples: For example, Chinese chopsticks, either when he knife to split the food, can also function as a fork to fork to take food, while in a foreign country, a fork is a fork, used to take food, the knife is used to separate the food; so this single responsibility requires software developers have a wealth of practical experience . Otherwise, it is difficult to grasp;

Demeter

Demeter, also known as the principle of a minimum know, a kind of external exposure to something better.

  1. From the perspective of dependent persons, it should rely only dependent objects.
  2. 从被依赖者的角度说,只暴露应该暴露的方法。

个人理解:当A类需要调用B类的三个方法才能实现的功能时,B类可以对这三个方法进行一个封装,然后只暴露封装的这个方法给A,这样A就只需要调用B的这个封装的方法就可以了,当B的三个方法中有修改的时候,只要修改B这个对外封装的方法就可以,而A调用者却不用改变,因为A只知道调用这个方法可以实现功能,而不用具体管B内部是怎么实现的,降低了程序的耦合度;

接口隔离原则

这个和单一职责有点类似,不过还是不一样的.

  • 单一职责原则注重的是职责,而接口隔离原则注重的是对接口依赖的隔离。
  • 单一职责原则主要是约束类,它针对的是程序中的实现和细节;接口隔离原则主要约束接口,主要针对抽象和程序整体框架的构建。

官方定义:要求程序员尽量将臃肿庞大的接口拆分成更小的和更具体的接口,让接口中只包含客户感兴趣的方法,降低程序耦合度。

这个法则也要根据实际的业务场景来应用,如果粒度控制的太小,就会导致类的急剧增加,明明一个功能只要三四个类,如果粒度小的话,就会变成十几个,甚至几十个,虽然这样程序耦合度低,比较灵活,但是维护难啊.如果粒度大,耦合度就会高,程序不灵活.所以这个原则要求技术人员有足够的实践,经验和领悟;

合成复用原则

它要求在软件复用时,要尽量先使用组合或者聚合等关联关系来实现,其次才考虑使用继承关系来实现。如果要使用继承关系,则必须严格遵循里氏替换原则。合成复用原则同里氏替换原则相辅相成的,两者都是开闭原则的具体实现规范。

如果不了解什么是组合和聚合的话可以看看这个篇文章<<组合、聚合与继承的爱恨情仇>>,讲的挺好的

总结

In programming, try to follow the seven principles of OOP. However, there is a saying that good, the rules are dead, people are living . This sometimes means that the seven principles is not a panacea, and sometimes some business scenarios If you follow these principles, but has become difficult to maintain, so everything must proceed from reality, 23 kinds of design patterns is the same, do not die by the rules Come.

Guess you like

Origin juejin.im/post/5df59fda51882512360d57cd