Open Closed Principle (Open Closed Principle)

table of Contents

 

1, the basic description

2, application examples

2.1 Demand

2.2, the traditional way to achieve

2.2.1 Analysis

2.2.2 Class FIG.

2.2.3 Code

2.2.4, the traditional way to achieve analysis

2.3, follow the principle of opening and closing to achieve

2.3.1 Analysis

2.3.2 Class FIG.

2.3.3 Code

2.3.4, follow the principle of opening and closing to achieve Analysis


1, the basic description

  • Open Closed Principle (Open Closed Principle) programming is the most basic and most important design principles.
  • A software entity, such as: classes, modules and functions should be open for extension (for provider for), closed for modification (for consumer is). Abstract framework for building, implemented with extension detail .
  • When the software needs to be changed, as far as possible by extending to implement changes in the behavior of software entities, rather than by modifying to achieve change existing code.
  • Programming follow other principles, and the use of design patterns aim is to follow the principle of opening and closing (core).

2, application examples

2.1 Demand

A door made of plants, can produce glass doors, wooden doors, doors and the like iron.

Programming production details.

2.2, the traditional way to achieve

2.2.1 Analysis

Each door can be designed as a class, with a property type to distinguish between what the door.

Factory is a class, processing plant to produce more, according to the different type of door.

2.2.2 Class FIG.

2.2.3 Code

/**
 * 客户端
 */
public class Client {
    public static void main(String[] args) {
        DoorFactory factory = new DoorFactory();
        factory.makeDoor(new IronDoor());
        factory.makeDoor(new TimberDoor());
    }
}

/**
 * 门
 */
class Door{
    int type;
}

/**
 * 铁门
 */
class IronDoor extends Door{
    public IronDoor() {
        type = 1;
    }
}

/**
 * 木门
 */
class TimberDoor extends Door{
    public TimberDoor(){
        type = 2;
    }
}

/**
 * 制门工厂
 */
class DoorFactory{
    /**
     * 制作门
     */
    public void makeDoor(Door door){
        if(1 == door.type){
            makeIronDoor();
        }else if(2 == door.type){
            makeTimberDoor();
        }
    }

    public void makeTimberDoor(){
        System.out.println("制作木门");
    }
    public void makeIronDoor(){
        System.out.println("制作铁门");
    }
}

operation result:

2.2.4, the traditional way to achieve analysis

This way fulfill the requirements, but poor scalability.

For example: now doorstop factory glass door, then how to do it?

  • First of all to create a new category of glass.
  • Then the plant was added doorstop method of making glass door.
  • To modify the method of making the last door doorstop in the plant, is added if-else in the method to determine the glass door.

The problem comes, we are adding a new or reduce one door, the door must be prepared to modify the factory class, which is a serious violation of the opening and closing principle (OCP) .

Solution: The specific implementation to implementation layer (s), when used in reference abstraction layer (interface / abstract class)

2.3, follow the principle of opening and closing to achieve

2.3.1 Analysis

The door design or interface to an abstract class, an abstract method provides: production;

Specific individual doors (wooden doors, steel doors ...) implement a door or inheritance;

Doorstop production plant without regard to what the door, just call the method can be made door, the door of the details to make examples of themselves to achieve;

2.3.2 Class FIG.

2.3.3 Code

/**
 * 客户端
 */
public class Client {
    public static void main(String[] args) {
        DoorFactory factory = new DoorFactory();
        factory.makeDoor(new IronDoor());
        factory.makeDoor(new TimberDoor());
    }
}

/**
 * 门
 */
interface Door{
    void make();
}

/**
 * 铁门
 */
class IronDoor implements Door{
    @Override
    public void make() {
        System.out.println("制作铁门");
    }
}

/**
 * 木门
 */
class TimberDoor implements Door{
    @Override
    public void make() {
        System.out.println("制作木门");
    }
}

/**
 * 制门工厂
 */
class DoorFactory{
    /**
     * 制作门
     */
    public void makeDoor(Door door){
        door.make();
    }
}

As a way of operating results of the above.

2.3.4, follow the principle of opening and closing to achieve Analysis

Following the procedure of opening and closing the principles of design, although running exactly the same way as before, but: Open for the principles and improve the scalability of the program fundamentally, maintenance and reliability.

If you now want to make the glass door, you only need to, do not make any changes in a new category and to achieve Door glass of make methods.

This is: to expand (new door) open , to modify (doorstop factory) is closed.

 

Published 98 original articles · won praise 26 · views 70000 +

Guess you like

Origin blog.csdn.net/weixin_42425970/article/details/97370992
Recommended