Open/Closed Principle (OCP)

This is the ‘O’ in SOLID

Simply put:

  • Open to extension is to allow new behavior to be added in the future
  • Closed to modification is not to change the source/binary code

Definition

The Open / Closed Principle states that software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.” – Wikipedia

  • Dr. Bertrand Meyer originated the term in his 1988 book, Object-Oriented Software Construction

How do we open things for extension?

The key is to rely on abstractions, so this is interfaces or an abstract base class. Its however very important not to simply apply OCP to every class unless you know based that the business rules are likely to change.

Its best to focus on “Refactoring to a better design”, this means you should write your code with the least amount of complexity first and when needed refactor it and apply a principal such as OCP. So if it changes once, accept it. If it changes again consider refactoring.

PROS

  • Existing core classes are not changed so you are less likely to introduce regression bugs
  • You are adding new classes each time, so you can test and have faith you will not upset things

CONS

  • This add complexity and must be used with caution, dont use OCP to try impress your boss

Typical Approaches

  • Parameters
    • By passing parameters to your methods you allow their behavior to change.
  • Inheritance
    • By using the override keyword in a concrete class you can over-ride the behavior of virtual or abstract methods.
  • Composition / Dependency Injection
    • By injecting an instance of a service defined by its interface the consumer is open to extension but closed for modification.
    • A great way to define a list of possible instances to be injected is the Factory Pattern.

Sample Code

Shopping cart examples based on work from Steve Smith

Great examples by Steve Smith aka Ardalis

Other Examples from Brad Vincent

References

Tell, Dont Ask

Tell-Don’t-Ask is a principle that helps people remember that object-orientation is about bundling data with the functions that operate on that data. It reminds us that rather than asking an object for data and acting on that data, we should instead tell an object what to do. This encourages to move behavior into an object to go with the data.

  • Don’t interrogate objects for their internals – move behavior to the object
  • Tell the object what you want it to do

References

SOLID (object-oriented design)

SOLID (object-oriented design)

In object-oriented computer programming, the term SOLID is a mnemonic acronym for five design principles intended to make software designs more understandable, flexible and maintainable.

These patterns and principles work together and each problem domain will require a different approach and implementation (or no implementation of the principle at all)

References & Influences

Stick sizes

Sizes

These sizes have never made any sense to me as I had an e flite apprentice which had a 1500mm wing but Horizonhobby say its a 15 size, it was electric so who knows! (15-size, 840Kv brushless, 30A ESC, 3S 3200mAh)

I think the sizes come from the older balsa models so the pilot knew which size internal combustion glow engine to use, though it was and still is common for pilots to fit a BIGGER engine :D

A ‘stick’ is a shoulder wing sport / trainer model aircraft originally designed by Phil Kraft in 1966, his was called the Das Ugly Stick and had a round cowl.

The original concept of the Ugly Stik was to design a radio controlled aircraft which could be built in an absolute minimum of time. Its purpose was towards a flying test bed for new proportional control developments and an all around shop airplane which could be used as a loaner for visiting flyers, testing repaired equipment, and any use which required an airplane which could be considered as expendable.”

June 1966

Notable difference between the Das Ugly and a modern Ultra stick is the shape of the tail, the flat trailing edges on the ailerons and elevator & it has no cowling.

Ultra Stick