Design principles - open and closed principles

Definition: A software entity such as classes, modules and functions should be open for extension and closed for modification.

Origin of the problem: During the life cycle of the software, when the original code of the software needs to be modified due to changes, upgrades and maintenance, etc., errors may be introduced into the old code, or we may have to refactor the entire function. And the original code needs to be retested.

Solution: When the software needs to change, try to achieve the change by extending the behavior of the software entity, rather than modifying the existing code to achieve the change.

         The open-closed principle is the most basic design principle in object-oriented design, which guides us how to build a stable and flexible system. The open-closed principle may be the most vaguely defined of the six principles of design patterns. It only tells us that it is open to extension and closed to modification, but how to make it open to extension and closed to modification does not clearly tell us. . In the past, if someone told me "you must follow the open-closed principle when designing", I would think that he didn't say anything, but it seemed that he said everything. Because the open-closed principle is really too empty.

 

example

Define an interface to find beauty

 

package com.loulijun.chapter6;
 
public interface IFindGirl
 {
    //age
    public int getAge();
    //Name
    public String getName();
    // looks
    public String getFace();
    //figure
    public String getFigure();
}

 

implement this interface

 

package com.loulijun.chapter6;
 
public class FindGirl implements IFindGirl
 {
    private String name;
    private int age;
    private String face;
    private String figure;
     
    public FindGirl(String name, int age, String face, String figure) {
        this.name = name;
        this.age = age;
        this.face = face;
        this.figure = figure;
    }
  
    @Override
    public int getAge() {
        return age;
    }
 
    @Override
    public String getFace() {
        return face;
    }
 
    @Override
    public String getFigure() {
        return figure;
    }
 
    @Override
    public String getName() {
        return name;
    }
}

 Scene: On the street

 

package com.loulijun.chapter6;
 
import java.text.NumberFormat;
import java.util.ArrayList;
 
public class Street
 {
    private final static ArrayList<IFindGirl> girls = new ArrayList<IFindGirl>();
    // static initialization block
    static
    {
        girls.add(new FindGirl("Zhang Hanyun",23,"Cute","165cm/47kg"));
        girls.add(new FindGirl("Gao Yuanyuan",33,"Fashion","165cm/48kg"));
        girls.add(new FindGirl("Zhang Zetian",19,"Pure type","168cm/47kg"));
    }
    public static void main(String args[]) {
        System.out.println("----------Beauty is here----------");
        for(IFindGirl girl:girls) {
            System.out.println("姓名:" + girl.getName() +
                                         " 年龄:" + girl.getAge() +
                                         " looks:" + girl.getFace() +
                                         " Figure: "+girl.getFigure());
        }
    }
}

 

 operation result:

----------Beauty is here----------
Name: Zhang Hanyun Age: 23 Appearance: Cute Body: 165cm/47kg
Name: Gao Yuanyuan Age: 33 Appearance: Fashionable Body: 165cm/48kg
Name: Zhang Zetian Age: 19 Appearance: Pure Body: 168cm/47kg

 

 

But if you want to separate a foreign beauty category (such as adding a nationality), you can do it by modifying the interface, modifying the implementation class, and extending.

If you modify the interface, it also means modifying the implementation class, which will change the project too much, so it is not recommended.

 

If you modify the implementation class, although this can solve the problem, it is obviously a bit far-fetched. If other changes are required, it will appear logically chaotic

So, it's the easiest way to do it by extension

How to expand:

You can define an IForeigner interface that inherits from IFindGirl, add the nationality attribute getCountry() to the IForeigner interface, and then implement this interface, and then you only need to make a little modification in the scene class.

package com.loulijun.chapter6;
 
public interface IForeigner extends IFindGirl {
    //Country of Citizenship
    public String getCountry();
}

implement interface

package com.loulijun.chapter6;
 
public class ForeignerGirl
implements IForeigner
 {
    private String name;
    private int age;
    private String country;
    private String face;
    private String figure;
     
    public ForeignerGirl(String name, int age, String country, String face, String figure) {
        this.name = name;
        this.age = age;
        this.country = country;
        this.face = face;
        this.figure = figure;
    }
    @Override
    public String getCountry() {
        // TODO Auto-generated method stub
        return country;
    }
 
    @Override
    public int getAge() {
        // TODO Auto-generated method stub
        return age;
    }
 
    @Override
    public String getFace() {
        // TODO Auto-generated method stub
        return face;
    }
 
    @Override
    public String getFigure() {
        // TODO Auto-generated method stub
        return figure;
    }
 
    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return name;
    }
}

然后在场景类中只需要修改如下代码即可,其他不变

girls.add(new ForeignerGirl("Avirl",28,"美国","性感型","160cm/45kg"));

 不过这些设计原则到不是绝对的,而是根据项目需求,实际需求来定夺使用

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326417440&siteId=291194637