Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interface and implementation design structure?

Tags:

I'm quite new to designing large programs in C++. I'm writing series of operations, each of which have their own class, which will be invoked by the ProcessMgr class.

I'm using ProcessMgr as an interface class, from which each operation can be invoked:

class ProcessMgr
{
 private:
   class OperationOne;
   class OperationTwo;
   class OperationThree;
}

class ProcessMgr::OperationOne 
{
 public:
   ...
};
class ProcessMgr::OperationTwo
{
 public:
  ...
};
class ProcessMgr::OperationThree
{
 public:
  ...
};

This enables me to control the types of access to the Operation classes, therefore not exposing much of their underlying code.

It's important that the user of this code can only interact with the Operation classes in specific ways and not have complete access to all the contents of the Operations classes.

My questions:

1) Is this a good approach to designing large programs? Are most libraries, such as CURL, structured in this way?

2) Are there better/more efficient methods of separating interface and implementation?

like image 248
Babra Cunningham Avatar asked Jan 28 '17 12:01

Babra Cunningham


1 Answers

A normal Interface in C++ (or other OOP languages) provides the definitions. The "operation-classes" must be derived from the interface, so that you decouple the implementation from the client. This principle is called the Dependency inversion principle(DIP).

A common UML-diagram from the DIP looks like the following: enter image description here

Since the Client is just familiar with the Interface, you can control the access to the specific subclasses. An Implementation could look like this:

class ProcessMgr {
    virtual void foo() = 0;
    virutal void bar() = 0;
}

class Operation1 : public ProcessMgr {
    virtual void foo() {  ...  }
    virtual void bar() {  ...  }
}

class Operation2 : public ProcessMgr {
    virtual void foo() {  ...  }
    virtual void bar() {  ...  }
}

The DIP is a principle from a series of very good principles, called SOLID. To design big projects, there is much more to do and learn. But the SOLID-principles are a good start to understand how to design software applications.

like image 85
Soeren Avatar answered Sep 22 '22 10:09

Soeren