[Yugong series] December 2021: object oriented design principles - open closed principle or OCP

Article catalog

preface

There are seven commonly used object-oriented design principles. These seven design principles are based on maintainability and reusability. These principles do not exist in isolation. They are interdependent and complementary. Following these design principles can effectively improve the reusability and maintainability of the system.

Tip: the following is the main content of this article. The following cases can be used for reference

1, Open closed principle or OCP

The open and close principle is also called the open and close principle, that is, software entities should be open to expansion and closed to modification. The opening and closing principle means that the software entity should try its best to expand the software without modifying the original code. The principle of opening and closing is the cornerstone of object-oriented design.

2, Use steps

Example

public interface IMobilePhone {

    decimal Price { get; set; }
    string Model { get; set; }
    Color Color { get; set; }

}
public enum Color {
    Black,
    White
}

First, use the imobililephone interface to establish a mobile phone contract, and expose three attributes. The Price attribute is the mobile phone Price, the Model attribute is the mobile phone Model, and the Color attribute is the mobile phone appearance Color. Next, we use this interface to implement an ApplePhoneX class.

Note: the mobile phone interface imobililephone should not be named IPhone, and the ApplePhoneX class should not be named IPhoneX, because it is easy to cause misunderstanding.

public class ApplePhoneX : IMobilePhone {

    public virtual double Price {
        get => 8799;
        set => Price = value;
    }

    public virtual string Model {
        get => "IPhone X";
        set => Model = value;
    }

    public virtual Color Color {
        get => Color.Black;
        set => Color = value;
    }

}

The following is a possible code for the caller:

IMobilePhone mobilePhone = new ApplePhoneX();
var price = mobilePhone.Price;

Now the demand has changed. Because the iPhone 9 is about to be launched, cook decided to discount and promote the iPhone X. The price of the black iPhone x is 6500.00 yuan and the price of the white iPhone x is 6450.00 yuan. It is easy to think of a way to modify the imobililephone interface and increase the DiscountPrice attribute, which may be as follows:

public interface IMobilePhone {

    double Price { get; set; }
    string Model { get; set; }
    Color Color { get; set; }
    double DiscountPrice { get; set; }//increase

}
public class ApplePhoneX : IMobilePhone {

    public virtual double Price {
        get => 8799;
        set => Price = value;
    }

    public virtual string Model {
        get => "IPhone X";
        set => Model = value;
    }

    public virtual Color Color {
        get => Color.Black;
        set => Color = value;
    }

    public virtual double DiscountPrice {//increase
        get => Color == Color.Black ? 6500.00 : 6450.00;
        set => DiscountPrice = value;
    }

}
public class HuaweiPhone : IMobilePhone {
    //Need modification
}
public class SmartisanPhone : IMobilePhone {
    //Need modification
}

However, this modification will affect all classes that implement the imobililephone interface, such as Huawei phone class and SmartisanPhone class. As a contract, the interface should be a stable existence and cannot be easily modified, otherwise it will obviously violate the opening and closing principle. Here is a solution for reference:

public class DiscountApplePhoneX : ApplePhoneX {

    public override double Price {
        get => Color == Color.Black ? 6500.00 : 6450.00;
        set => Price = value;
    }

}
IMobilePhone mobilePhone = new DiscountApplePhoneX();
var price = mobilePhone.Price;

summary

This new requirement is solved by adding a DiscountApplePhoneX class inherited from ApplePhoneX and overriding the Price method. All the original codes do not need to be changed, as long as their use is modified where discount phones are used, which conforms to the opening and closing principle.

Posted on Fri, 03 Dec 2021 09:55:48 -0500 by mjohnson025