Skip to content

Instantly share code, notes, and snippets.

@lamont-granquist
Created March 8, 2022 19:39
Show Gist options
  • Save lamont-granquist/c16ea7660ed4af250ff407fd69175adf to your computer and use it in GitHub Desktop.
Save lamont-granquist/c16ea7660ed4af250ff407fd69175adf to your computer and use it in GitHub Desktop.
class Base {
// Base has:
// - implementation details
// - public interface
// - protected interface
virtual public void whatever(void) {
/// code
}
protected void subclass(void) {
// code
}
}
class Base2 {
}
class Foo: Base {
Foo() {
}
override public void whatever(void) {
}
public void method(void) {
base.subclass();
}
}
{
List<Base>.Add(new Foo())
}
interface IBase { // <--- public interface
void whatever(void);
}
interface IBaseInternal : IBase { // <-- internal interface
void sublcass(void);
}
class MySql : IBaseInternal {
public void whatever(void) {
// code
}
public void subclass(void) {
// code
}
}
class Mock : IBaseInternal {
public void whatever(void) {
// some internally breaking change
}
public void sublcass(void) {
// some internally breaking change
}
}
class Foo: IBase {
public IBaseInternal _base; // <-- this is composition
public Foo(IBaseInternal baze) { // <--- this is constructor based depeendency injection (also called "inversion of control")
this._base = baze;
}
public void whatever(void) {
_base.whatever();
}
public void method(void) {
_base.subclass();
}
}
{
List<IBase>.Add(new Foo(new MySql())) // <--- this is where the "other" code injects Base1 behavior into Foo objects
List<IBase>.Add(new Foo(new Mock()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment