C++ में Interface Design
In this page:
class InterfaceName {
public:
virtual return_type method_name() = 0;
virtual ~InterfaceName() {}
};
class Implementation : public InterfaceName {
public:
return_type method_name() override { }
};
Pure Interfaces
C++ में एक interface conventionally पूरी तरह pure virtual functions से बनी एक abstract class से represented होता है इसके अपने कोई data members के बिना — यह एक contract establish करता है जिसे इसे implement करने वाली हर derived class को fulfill करना ज़रूरी है।
उदाहरण: Pure Interfaces
// Include std::cout and std::cin
#include <iostream>
// Define the type Printable
class Printable {
// Members below can be accessed from outside the class
public:
virtual void print() = 0;
};
// Define the type Document
class Document : public Printable {
// Members below can be accessed from outside the class
public:
void print() override { std::cout << "Printing document" << std::endl; }
};
// Program execution starts in main()
int main() {
Document d;
d.print();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Interfaces Implement करना
एक interface implement करने के लिए, एक class interface class से inherit करती है और इसके हर pure virtual method के लिए एक concrete override supply करती है; सब implement होने के बाद ही derived class instantiable बनती है।
उदाहरण: Implementing Interfaces
// Include std::cout and std::cin
#include <iostream>
// Define the type Drawable
class Drawable {
// Members below can be accessed from outside the class
public:
virtual void draw() = 0;
};
// Define the type Circle
class Circle : public Drawable {
// Members below can be accessed from outside the class
public:
void draw() override { std::cout << "Circle" << std::endl; }
};
// Program execution starts in main()
int main() {
Circle c;
c.draw();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
कई Interfaces
एक class को एक base class तक limit करने वाली languages के उलट, C++ multiple inheritance allow करता है, इसलिए एक single class एक साथ कई distinct interfaces implement कर सकती है, हर एक अपना अलग contract contribute करते हुए जो class को satisfy करना ज़रूरी है।
उदाहरण: Multiple Interfaces
// Include std::cout and std::cin
#include <iostream>
// Define the type Printable
class Printable {
// Members below can be accessed from outside the class
public:
virtual void print() = 0;
};
// Define the type Savable
class Savable {
// Members below can be accessed from outside the class
public:
virtual void save() = 0;
};
class Document : public Printable, public Savable {
public:
void print() override { std::cout << "Printing" << std::endl; }
void save() override { std::cout << "Saving" << std::endl; }
};
// Program execution starts in main()
int main() {
Document d;
d.print();
d.save();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Loose Coupling
Concrete classes की बजाय interfaces के against design करना loose coupling promote करता है: सिर्फ एक interface pointer के जरिए interact करने वाला code यह जानने की ज़रूरत नहीं कि इसके पीछे कौन सा concrete implementation है, इसलिए बाद में एक अलग implementation swap in करना program के बाकी हिस्से में ripple नहीं करता।
उदाहरण: Loose Coupling
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Define the type Logger
class Logger {
// Members below can be accessed from outside the class
public:
virtual void log(std::string msg) = 0;
};
// Define the type ConsoleLogger
class ConsoleLogger : public Logger {
// Members below can be accessed from outside the class
public:
void log(std::string msg) override { std::cout << msg << std::endl; }
};
void process(Logger &logger) {
logger.log("Processing complete");
}
// Program execution starts in main()
int main() {
ConsoleLogger logger;
process(logger);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Dependency Inversion
Design की यह style Dependency Inversion principle support करती है, जो कहता है high-level modules को low-level concrete classes पर सीधे depend करने की बजाय abstract interfaces पर depend करना चाहिए — overall system को extend करना और isolation में test करना आसान बनाते हुए।
उदाहरण: Dependency Inversion
// Include std::cout and std::cin
#include <iostream>
// Define the type Notifier
class Notifier {
// Members below can be accessed from outside the class
public:
virtual void notify() = 0;
};
// Define the type EmailNotifier
class EmailNotifier : public Notifier {
// Members below can be accessed from outside the class
public:
void notify() override { std::cout << "Email sent" << std::endl; }
};
class App {
Notifier ¬ifier;
public:
App(Notifier &n) : notifier(n) {}
void run() { notifier.notify(); }
};
// Program execution starts in main()
int main() {
EmailNotifier email;
App app(email);
app.run();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- सिर्फ pure virtual functions से बनी एक interface class का instance बनाने की कोशिश करना।
- concrete class में सारे interface functions implement करना भूल जाना।
- एक interface में data members add करना, जो interface और implementation मिला देता है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: