Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

Recently, I have been learning six design principles and I have been enlightened. Now I will simply record my feelings.

define

The Open/Closed Principle (OCP) states that “objects in software (classes, modules, functions, etc.) should be Open for extension, but Closed for modification” [1], which means that an entity is allowed to change its behavior without changing its source code.

In simple terms, a software entity (such as a class, module, or function) should be open for extension but closed for modification. The point is abstraction.

advantages

  • Abstract good flexibility, wide adaptability, basically maintain the stability of software architecture

  • Improved reusability and maintainability

example

In this example, to keep it simple, cats have the ability to eat, and we use an action management tool to manage it.

Class ActionManager {func catEat(Cat: Cat) {print(cat.eat())}} class ActionManager {func catEat(Cat: Cat) {print(cat.eat())}Copy the code

This catEat method is called here

var action = ActionManager()
var cat = Cat()
action.catEat(cat: cat)
Copy the code

Naturally output “eat fish”, there is no problem with this nature.

All right, the product comes and says add requirements, the dog needs to eat, eat dog food.

So let’s create a Dog class, add eat, ActionManager, add dogEat.

Class Dog {func eat() -> String {return "eat Dog food"}} class ActionManager {func catEat(cat: Cat) { print(cat.eat()) } func dogEat(dog: Dog) { print(dog.eat()) } }Copy the code

We call the dogEat method directly

var action = ActionManager()

var dog = Dog()
action.dogEat(dog: dog)
Copy the code

Naturally, it also exports “dog food”, which, obviously, can also meet the demand for products.

However, this violates the open and close principle. If we have other pigs, rabbits, monkeys, etc. that need to be eaten, then we need to add methods to the ActionManager every time, which is obviously not the desired effect.

The open and close principle is the core of abstraction, ok, so we can create an abstract class and let the ActionManger call the abstract class ok, what other animals, we all inherit from the abstract class, kind of extend the open, so that later if not only food, but other general things, can be written in the abstract class.

Class Animal {func eat() -> String {return "eat meat"}} Animal {override func eat() -> String {return "eat fish"}} Animal {override func eat() -> String {return "eat dog food"}}Copy the code

Our ActionManger could also be written as

class ActionManager {

    func eat(animal: Animal) {
        print(animal.eat())
    }
}
Copy the code

At this point, our call becomes

var action = ActionManager()
var cat = Cat()
action.eat(animal: cat)

var dog = Dog()
action.eat(animal: dog)
Copy the code

The effect is that there is a bowl for animals to eat, but we don’t know which animal is eating. ActionManger provides the method of eat() which animal comes to eat.

If we write it this way, don’t we improve reusability and maintainability?

conclusion

When application requirements change, software entities including classes, modules and interfaces can be extended to meet new requirements without modifying them. That’s the open close principle.

If there are shortcomings in the above statement, please take note.