Design Patterns-six principles-open and closed principles

Source: Internet
Author: User

Open closed Principle)

The core of the open/closed principle is: open to expansion and close to modification.

The vernacular means that when we change a software (such as extending other functions), we should use the extension method to achieve the software change, instead of modifying the original code to achieve the change.


The principle of opening and closing is an abstract summary of the first five principles. The first five principles are specific implementations of the principle. Therefore, if the principle of opening and closing is used, it is actually a bit false because it does not have a fixed pattern, however, the ultimate guarantee is to improve the reusability and maintainability of the program.

To use this principle, we also need to combine its idea of "opening to expansion, closing to modification" and other five design principles to develop projects based on experience.

In general, this means that if you want to study it, you need to read other books.

The following is a simple example of using the open and closed principle. Although it is inaccurate, it means this (understanding)

Define an interface to find beauty

Package COM. loulijun. chapter6; public interface ifindgirl {// age public int getage (); // name Public String getname (); // appearance Public String getface (); // body 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){ = name;this.age = age;this.face = face;this.figure = figure;}@Overridepublic int getAge() {return age;}@Overridepublic String getFace() {return face;}@Overridepublic String getFigure() {return figure;}@Overridepublic String getName() {return name;}}

Scenario: 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", "165/47 kg"); girls. add (New findgirl ("high circle", 33, "fashion", "165/48 kg"); girls. add (New findgirl ("zhangzetian", 19, "pure", "168/47 kg");} public static void main (string ARGs []) {system. out. println ("---------- beauty here ----------"); For (ifindgirl GIRL: Girls) {system. out. println ("name:" + girl. getname () + "Age:" + girl. getage () + "appearance:" + girl. getface () + "Body:" + girl. getfigure ());}}}

Running result:

---------- Beauty here ----------
Name: Zhang hanyun age: 23 appearance: Cute shape: 165/47 kg
Name: Gao Yuanyuan age: 33 looks: Fashion shape: 165/48 kg
Name: Zhang Zetian age: 19 appearance: clear shape: 168/47 kg

However, if you want to separate a foreign beauty category (such as adding a nationality), you can modify the interface, modify the implementation class, and expand the implementation class.

If you modify an interface, it means modifying the implementation class. This changes the project too much and is not recommended.

If you modify the implementation class, this can solve the problem, but it is obviously far-fetched. If you need other changes, the logic will appear chaotic.

Therefore, extension is the simplest method.

How to expand:

You can define an iforeigner interface to inherit from ifindgirl, add the nationality attribute getcountry () to the iforeigner interface, and then implement this interface. Then you only need to make a slight modification in the scenario class.

Package com. loulijun. chapter6; public interface iforeigner extends ifindgirl {// nationality Public String getcountry ();}

Implementation 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){ = name;this.age = age; = country;this.face =face;this.figure = figure;}@Overridepublic String getCountry() {// TODO Auto-generated method stubreturn country;}@Overridepublic int getAge() {// TODO Auto-generated method stubreturn age;}@Overridepublic String getFace() {// TODO Auto-generated method stubreturn face;}@Overridepublic String getFigure() {// TODO Auto-generated method stubreturn figure;}@Overridepublic String getName() {// TODO Auto-generated method stubreturn name;}}

In the scenario class, you only need to modify the following code, and the others remain unchanged.

Girls. Add (New foreignergirl ("avirl", 28, "us", "sensory", "160/45 kg "));

However, these design principles are not absolute, but determined based on project requirements and actual needs.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.