我如何摆脱程序编程的习惯,转而面向对象编程? [英] How do i get out of the habit of procedural programming and into object oriented programming?

查看:79
本文介绍了我如何摆脱程序编程的习惯,转而面向对象编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能获得一些提示,以帮助我摆脱这些年来对过程编程的不良习惯的理解.每次我尝试在OOP中进行项目时,我最终都会恢复为程序性的.我想我并不完全相信OOP(即使我认为我已经听说过所有关于它的好消息!).

所以我想我经常执行的常见编程任务的任何实用示例,例如用户身份验证/管理,数据解析,CMS/Blogging/eComs都是我经常做的事情,但我却做不到让我领悟到如何在OOP中完成这些工作,而又远离程序,尤其是在我构建的系统趋于正常运行的情况下.

我认为开发的一个弊端是我确实经常重用我的代码,并且经常需要更多的重写和改进,但有时我将其视为软件开发的自然发展.

但是我想改变!给我的程序员同伴,帮助:)关于如何摆脱这种讨厌的习惯的任何提示?

解决方案

当找不到合适的理由或动机时,使用面向对象的编程有什么意义?

您必须对将概念作为对象进行构思和操纵的需求感到有动力.有些人觉得需要感知概念,流程或功能,而不是对象,然后便会朝着面向概念,思想或功能流程的编程的方向努力.

大约13年前,我之所以从c切换到c ++仅仅是因为有些想法我需要,但是c很难执行.简而言之,我的需要激励了我面向对象的编程.

面向对象的思维方式

首先,您具有字节,字符,整数和浮点数.

然后,您的程序开始被各种变量(本地变量和静态变量)所困扰. 然后,您决定将它们分组为结构,因为您发现所有通常都会传递的变量.

数据汇总

因此,像打印机的信息一样,所有变量都应包含在打印机"结构中:

{id, name, location,
 impactType(laser|inkjet|ribbon),
  manufacturer, networkAddr},
  etc.

因此,现在,当您在打印机信息上调用一个接一个的函数时,您将没有带有一长串参数的函数或具有大量串扰可能性的大量静态变量.

信息合并

但是数据整合还不够好.我仍然必须依靠一堆函数来处理数据.因此,我有一个聪明的主意,或者将函数指针合并到Printer结构中.

{id, name, location,
 impactType(laser|inkjet|ribbon),
 manufacturer, networkAddr,
 *print(struct printer),
 *clean(struct printer)
}

当数据包含有关如何处理/感知数据的过程时,数据就会变成信息.

信息量化

现在,激光打印机,色带打印机和喷墨打印机并非都具有相同的信息集,但是它们都具有最常见的信息分母(LCD)集:

任何打印机共有的信息:ID,名称,位置等

仅在色带打印机中找到的信息:用过的周期,色带(织物|玻璃纸),色带等

仅在喷墨中找到的信息:墨盒等

仅在激光中发现的信息:...

对于我和许多面向对象的群体,我们宁愿将所有通用信息量化为一个通用信息封装,而不是为每种打印机类型定义单独的结构/封装.

然后,我们宁愿使用一个框架来管理每种类型打印机的所有功能引用,因为并非所有打印机都以相同的方式打印或清洗.

因此,远离对象的偏好/动机告诉您,如果不使用对象,您的编程工作会更轻松?您希望自己管理所有这些结构上的复杂性.您一定没有编写足够的软件来感受这种感觉.

懒惰的必要性

有人说-必要性是创造力的源泉. (同样,对金钱的热爱是邪恶的根源.)

但是对于我和我的同伙-面对必需品时的懒惰是创造力的父母. (以及金钱的匮乏是邪恶的另一母子.)

因此,我敦促您对编程保持一种懒惰的态度,以使最短路径的原则融入您的生活,您会发现,除了毕业之外,别无选择,只能朝着面向对象编程的方向前进. /p>

I'm hoping to get some tips to kinda help me break out of what i consider after all these years a bad habit of procedural programming. Every time i attempt to do a project in OOP i end up eventually reverting to procedural. I guess i'm not completely convinced with OOP (even though i think i've heard everything good about it!).

So i guess any good practical examples of common programming tasks that i often carry out such as user authentication/management, data parsing, CMS/Blogging/eComs are the kinda of things i do often, yet i haven't been able to get my head around how to do them in OOP and away from procedural, especially as the systems i build tend to work and work well.

One thing i can see as a downfall to my development, is that i do reuse my code often, and it often needs more rewrites and improvement, but i sometimes consider this as a natural evolution of my software development.

Yet i want to change! to my fellow programmers, help :) any tips on how i can break out of this nasty habbit?

解决方案

What is the point in using object-oriented programming when you cannot find good reasons or motivation to do so?

You must be motivated by the need to conceive and manipulate ideas as objects. There are people who feel the need to be perceptive of concepts, flow or functions rather than objects and they are then motivated towards programming oriented towards concepts, ideas, or functional flow.

Some 13 years ago, I switched to c++ from c simply because there were ideas I needed but c would not easily perform. In short, my need motivated my programming oriented towards objects.

The object-oriented mind-set

First, you have bytes, chars, integers and floats.

Then your programme starts being cluttered with all kinds of variables, local and static. Then you decide to group them into structs because you figured that all the variables which are commonly passed around.

Conglomeration of data

So like printer's info should have all its variables enclosed into the Printer struct:

{id, name, location,
 impactType(laser|inkjet|ribbon),
  manufacturer, networkAddr},
  etc.

So that now, when you call function after function over printer info, you don't have functions with a long list of arguments or a large collection of static variables with huge possibilities of cross-talk.

Incorporation of information

But data conglomeration is not good enough. I still have to depend on a bunch of functions to process the data. Therefore, I had a smart idea or incorporating function pointers into the Printer struct.

{id, name, location,
 impactType(laser|inkjet|ribbon),
 manufacturer, networkAddr,
 *print(struct printer),
 *clean(struct printer)
}

Data graduates into information when data contains the processes on how to treat/perceive the data.

Quantization of information

Now laser, ribbon and inkjet printers do not all have the same set of information but they all have a most common set of denominators (LCD) in information:

Info common to any printer: id, name, location, etc

Info found only in ribbon printers: usedCycles, ribbon(fabric|cellophane), colourBands, etc

Info found only in inkjet: ink cartridges, etc

Info found only in lasers: ...

For me and many object-oriented cohorts, we prefer to quantize all the common info into one common information encapsulation, rather than define a separate struct/encapsulation for each printer type.

Then, we prefer to use a framework which would manage all the function referencing for each type of printer because not all printers print or are cleaned the same way.

So your preference/motivation oriented away from objects is telling you that your programming life is easier if you do not use objects? That you prefer to manage all those structural complexities yourself. You must not have written enough software to feel that way.

The necessity of laziness

Some people say - necessity is the mother of creativity. (as well as, Love of money is the root of evil).

But to me and my cohorts - laziness in the face of necessity are the parents of creativity. (as well as the lack of money is the other parent of evil).

Therefore, I urge you to adopt a lazy attitude towards programming so that the principle of the shortest path would kick into your life and you'll find but have no other choice than to graduate towards orienting yourself towards programming with objects.

这篇关于我如何摆脱程序编程的习惯,转而面向对象编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆