结构化与OOP编程

美好的一天。在本文中,我不想展示什么是更好的结构化或对象编程,而是展示如何在此编写。也许对于那些只想开始编程但不知道选择哪种语言以及哪种更方便的人,这将是一个选择。我以C和JAVA为例。



我首先想到的是第一件事。它似乎不是一种模式,但是有时您必须在OOP中使用它。首先,我用Java编写一个示例。假设我们需要为两种类型的实现创建一个类。例如,我们需要创建一个类以通过http和https连接到站点。这是一个小例子,例如,当您想在Opengl 2.0 es和Opengl 3.0 es中为android绘制时,可以使用相同的方法。在这种情况下,将会有更多的代码,如果您按照我给出的方式进行操作,那么代码将看起来很正常。但是,这种方法当然不是我发明的,我自己有时会从书中读到的。因此,https和http。为此,您需要创建一个接口。并根据协议类型为该接口分配所需的类。我记不清了,但是我似乎读过一些有关OOP可以动脑子的文章。这样,您就可以编写出色的代码。也许是这样。但是我几乎不使用OOP编程,所以也许我编写了很多代码,但是真正的程序员会编写更加简洁的代码。但我想给你看一个例子。这是java接口。



import java.net.*;
import java.io.*;

interface IProtocol {
	public void connect ( URL url );
	public URLConnection getConnection ( ) throws IOException;
}


在这里,我们声明了两个函数,一个用于连接,另一个用于获取URLConnection。现在有两个类可以使用。



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 ( );
	}
}


您需要在connect和getConnection中编写多少代码并不重要。例如,我选择了一些代码,但是如果要对Opengl进行编程,则可能很多。但是很方便。因此,主要功能仍然保留。



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 );

	}
}


在C语言中,您可以使用curl而不用编写很多代码,但是如何使用C语言解决这个示例呢?C有指针-这就是它的力量。这是C语言中的一个示例-main函数。main.c



文件

#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 ( );
}


conn结构在另一个文件中声明。conn.h



文件

#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


对于init_http,将分配整个范围的文件,这是http所需的。



文件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;
}


另外,init_https需要Ssl。因此,此文件将包含此连接所需的所有数据。



文件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;
}


如果您喜欢这篇文章,那么我以后会写续集。目前,我可能不太了解OOP,无法编写更多示例,但是我认为,如果我不放弃OOP,那么初学者将有兴趣了解结构化和OOP编程中的差异。



All Articles