I'll start with the first thing that comes to mind. It seems to be not a pattern, but sometimes you have to use it in OOP. First, I write an example in java. Let's say we need to create a class for two types of implementation. For example, we need to create a class to connect to the site via http and https. This is a small example, for example, the same method can be used when you want to draw for android in Opengl 2.0 es and Opengl 3.0 es. In this case, there will be more code, and if you do it in the way that I will give, then the code will look normal. But this method, of course, was not invented by me, I myself read it from the book sometime. And so https and http. To do this, you need to create an interface. And assign the required class to this interface, depending on the type of protocol. I don't remember exactly, but I seem to have read somewhere that OOP sets the brain.That this allows you to write competent, beautiful code. Perhaps so. But I almost don't program in OOP, and that's why maybe I write so much code, and a real programmer will make more concise code. But I want to show you an example. This is the java interface.
import java.net.*;
import java.io.*;
interface IProtocol {
public void connect ( URL url );
public URLConnection getConnection ( ) throws IOException;
}
Here we are declaring two functions, one for connecting and the other for getting URLConnection. Now there are two classes for this to work.
import java.net.*;
import java.io.*;
public class Http implements IProtocol {
public URL url;
public void connect ( URL url ) {
this.url = url;
}
public HttpURLConnection getConnection ( ) throws IOException {
return ( HttpURLConnection ) url.openConnection( );
}
}
import java.net.*;
import javax.net.ssl.*;
import java.io.*;
public class Https implements IProtocol {
public URL url;
public void connect ( URL url ) {
this.url = url;
}
public HttpsURLConnection getConnection ( ) throws IOException {
return ( HttpsURLConnection ) url.openConnection ( );
}
}
How much code you need to write in connect and getConnection is not important. For example, I chose a little code, but it can be a lot, if for example Opengl es to program. But it's convenient. So, the main function is left.
import java.net.*;
import java.io.*;
public class Main {
public static void main ( String[] args ) {
URL url = null;
try {
url = new URL ( "https://www.google.com" );
} catch ( MalformedURLException e ) {
return;
}
String protocol = url.getProtocol ( );
IProtocol prot = null;
switch ( protocol ) {
case "http": prot = new Http ( ); break;
case "https": prot = new Https ( ); break;
default: return;
}
prot.connect ( url );
URLConnection conn = null;
try {
conn = prot.getConnection ( );
} catch ( IOException e ) {
return;
}
conn.setDoOutput ( true );
}
}
You can use curl in C and not write a lot of code, but how could this example be solved using C? C has pointers - that's its power. And here's an example in C - the main function. main.c
file
#include <stdio.h>
#include "conn.h"
struct conn conn;
#define HTTP_PROTOCOL 1
#define HTTPS_PROTOCOL 2
#define ERROR_PROTOCOL -1
static int get_protocol ( void ) {
return HTTP_PROTOCOL;
}
int main ( int argc, char **argv ) {
switch ( get_protocol ( ) ) {
case HTTP_PROTOCOL: init_http ( &conn ); break;
case HTTPS_PROTOCOL: init_https ( &conn ); break;
case ERROR_PROTOCOL: return -1;
}
conn.connect ( "www.google.com" );
char *data = conn.read ( );
}
The conn structure is declared in another file. conn.h
file
#ifndef __CONN__
#define __CONN__
struct conn {
void ( *connect ) ( const char *url );
char *( *read ) ( void );
};
void init_http ( struct conn *conn );
void init_https ( struct conn *conn );
#endif
There is a whole scoped file allocated for init_http, which is needed for http.
file http.c
#include "conn.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
static int sockfd;
static void connect_http ( const char *url ) {
sockfd = socket ( AF_INET, SOCK_STREAM, 0 );
}
static char *read_http ( void ) {
return NULL;
}
void init_http ( struct conn *conn ) {
conn->connect = connect_http;
conn->read = read_http;
}
Ssl is additionally required for init_https. Therefore, this file will contain all the data that is needed for this connection.
file https.c
#include "conn.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <openssl/ssl.h>
#include <stdio.h>
static int sockfd;
static SSL *ssl;
static void connect_https ( const char *url ) {
sockfd = socket ( AF_INET, SOCK_STREAM, 0 );
}
static char *read_https ( void ) {
return NULL;
}
void init_https ( struct conn *conn ) {
conn->connect = connect_https;
conn->read = read_https;
}
If you like the article, then I will write a sequel in the future. While I still probably don't know OOP well enough to write more examples, I think that if I don't give up OOP, then beginner readers will be interested in seeing the differences when writing in structured and OOP programming.