11. DIP
HomeDesign PatternC++

11. DIP

DIP, Dependency Inversion Principle

10. Abstract & Concrete Class
09. Virtual Destructor
08. Template Method Pattern

DesignPattern

Intent

  • Use an interface of an abstract class.

Explain

One of Object Oriented Programming(OOP) principles is Single Responsibility Principle(SRP), a class has one responsibility. Therefore, each feature should be distributed in multiple classes. Code 1 shows simple example.

// 01_C_TightlyCoupling.cpp
#include <iostream>
using namespace std;

class Vim
{
public:
    void write(string& code)
    {
        cout << code << endl;
    }
};

class Developer
{
public:
    void writeCode(Vim* p, string code)
    {
        p->write(code);
    }
};

int main()
{
    Developer developer;
    Vim vim;

    developer.writeCode(&vim, "Hello, World!");

    return 0;
}
// Compile: clang++ -std=c++14
//              -o 01_C_TightlyCoupling 01_C_TightlyCoupling.cpp
C++
Code 1. Tightly coupling
Hello, World!

There are two classes, Vim and Developer. Responsibility to write a code is implemented in Vim class, and Developer class has a responsibility to trigger Vim instance to write a code with its pointer.

However, if Developer wants to write a code with another class, such as Emacs, the callers should be changed, main() and Developer. The reason is that they use the pointer of Vim directly. In this case, we say they are tightly coupled.

To make program flexible, an interface is necessary. An interface is a pure virtual function defined in abstract class. Code 2 shows example of an interface.

// 02_C_LooselyCoupling
#include <iostream>
using namespace std;

class IEditor
{
public:
    virtual void write(string& code) = 0;
    virtual ~IEditor() {}
};

class Developer
{
public:
    void writeCode(IEditor* p, string code)
    {
        p->write(code);
    }
};

class Vim : public IEditor
{
public:
    virtual void write(string& code)
    {
        cout << "Vim: " << code << endl;
    }
};

class Emacs : public IEditor
{
public:
    void write(string& code)
    {
        cout << "Emacs: " << code << endl;
    }
};

int main()
{
    Developer developer;
    Vim vim;
    Emacs emacs;

    developer.writeCode(&vim, "Hello, World!");
    developer.writeCode(&emacs, "Hello, World!");

    return 0;
}
// Compile: clang++ -std=c++14
//              -o 02_C_LooselyCoupling 02_C_LooselyCoupling.cpp
C++
Code 1. Loosely coupling
Vim: Hello, World!
Emacs: Hello, World!

IEditor is an abstract class defining interfaces, and Vim and Emacs are concrete classes inheriting abstract class. The concrete classes overrides and have implement of interfaces. As a result, Developer can use the concrete classes with the unified way, the pointer of IEditor class. Additionally, a caller does not need to consider new class which inherits the abstract class. In this case, we call they are loosely coupled.

This design is based on Dependency Inversion Principe(DIP). DIP guides a caller use an interface of an abstract class. Because of SRP, each class has its own responsibility. However, without an interface, it is not easy to support all of them. Fortunately, C++ supports DIP by providing an interface and an abstract class, so we can simply handle them with the pointer of abstract class.

Summary

  • DIP guides using an abstract class and an interface, instead of concrete class directly.
  • Interface and abstract class make loosely coupling, so a program becomes flexible.
Name

0 weights,1,abstract class,1,active function,3,adam,2,Adapter,1,affine,2,argmax,1,back propagation,3,binary classification,3,blog,2,Bucket list,1,C++,11,Casting,1,cee,1,checkButton,1,cnn,3,col2im,1,columnspan,1,comboBox,1,concrete class,1,convolution,2,cost function,6,data preprocessing,2,data set,1,deep learning,31,Design Pattern,12,DIP,1,django,1,dnn,2,Don't Repeat Your code,1,drop out,2,ensemble,2,epoch,2,favicon,1,fcn,1,frame,1,gradient descent,5,gru,1,he,1,identify function,1,im2col,1,initialization,1,Lab,9,learning rate,2,LifeLog,1,linear regression,6,logistic function,1,logistic regression,3,logit,3,LSP,1,lstm,1,machine learning,31,matplotlib,1,menu,1,message box,1,mnist,3,mse,1,multinomial classification,3,mutli layer neural network,1,Non Virtual Interface,1,normalization,2,Note,21,numpy,4,one-hot encoding,3,OOP Principles,2,Open Close Principle,1,optimization,1,overfitting,1,padding,2,partial derivative,2,pooling,2,Prototype,1,pure virtual function,1,queue runner,1,radioButton,1,RBM,1,regularization,1,relu,2,reshape,1,restricted boltzmann machine,1,rnn,2,scrolledText,1,sigmoid,2,sigmoid function,1,single layer neural network,1,softmax,6,softmax classification,3,softmax cross entropy with logits,1,softmax function,2,softmax regression,3,softmax-with-loss,2,spinBox,1,SRP,1,standardization,1,sticky,1,stride,1,tab,1,Template Method,1,TensorFlow,31,testing data,1,this,2,tkinter,5,tooltip,1,Toplevel,1,training data,1,vanishing gradient,1,Virtual Copy Constructor,1,Virtual Destructor,1,Virtual Function,1,weight decay,1,xavier,2,xor,3,
ltr
item
Universe In Computer: 11. DIP
11. DIP
DIP, Dependency Inversion Principle
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXvUavVsX4rYm8cgKQ8rzps7pHmmEVtkIb66G2o5KZFCfjvO96SCEUdaw_DMYzSbdVrB8XvnaKN8ljEE7EBjutI2XvsBL-2BLdijjJRIpAOEpvlhOW-hfWP3ir3wbE488AwdFEu7WKbLgO/s0/
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXvUavVsX4rYm8cgKQ8rzps7pHmmEVtkIb66G2o5KZFCfjvO96SCEUdaw_DMYzSbdVrB8XvnaKN8ljEE7EBjutI2XvsBL-2BLdijjJRIpAOEpvlhOW-hfWP3ir3wbE488AwdFEu7WKbLgO/s72-c/
Universe In Computer
https://kunicom.blogspot.com/2018/04/11-dip.html
https://kunicom.blogspot.com/
https://kunicom.blogspot.com/
https://kunicom.blogspot.com/2018/04/11-dip.html
true
2543631451419919204
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago 1 minutes ago 1 hour ago 1 hours ago Yesterday 1 days ago 1 weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy