Object-Oriented Concepts

转载 2006年05月25日 10:41:00

Object-Oriented Concepts

14th May, 2006 @ 11:20pm GMT

Development, Javascript, PHP, Web / 3 Comments

Object-oriented programming is still a relatively new technology in the world of web development; and, as such, some of the concepts are widely misunderstood by many capable web developers. This can be best illustrated by the recent boom of interest in the Javascript object literal.

As Senior Web Developer at Rentokil Initial, I am required to be technical lead within the team. This also means making sure the technology is understood and used correctly. With this in mind, I’ve recently noticed that many of my team-mates obviously don’t understand the underlying concepts of OOP. For this reason, I’ve prepared this short tutorial on the concepts - to be followed by more detailed posts on execution in Javascript and in PHP.

What is Object-Oriented programming?

Object-oriented programming can best be describe as a programming paradigm; a methodology of programming adopted by a programmer. Another example of a programming paradigm would be procedural (or sometimes, imperative) programming.

Procedural programming involves viewing a computer program as a sequential list of computational steps (or procedures) to be carried out. In procedural programming, the list of procedures can be further broken down into subroutines and functions (not to be confused with mathematical functions which are used within functional programming).

This is very much an “old skool” approach to programming and is adopted by many high-level languages such as BASIC. It’s also worth pointing out that most Web-based languages, such as PHP, ASP and Javascript, can be written using this approach - in fact when I first started out as a web developer, I was using ASP script, PHP and Javascript in this very manner, blissfully unaware of the OOP potential there-in.

In OOP, a program is seen as comprising a collection of individual modules, or objects, that act on each other. Each of these objects could be seen as an independent program in itself, with a distinct role or responsibility.

OOP provides greater flexibility and easier maintainance across large systems and can sometimes make understanding and analysing complex procedures a lot easier.

It’s worth noting, at this point, that most OOP languages (such as C++ or Java) are stateful where as often procedural programming languages are stateless.

Enemy of the State

In computer science, a “state” is a particular set of instructions which will be executed in response to the machine’s input. An information system or protocol that relies upon state is said to be stateful. One that does not is said to be stateless.

OOP PHP is stateless because each script must be called on a page refresh where as Javascript is stateful because it can make use of event listeners - specific input - and the wonders of XMLHttpRequest - thus negating the need for refreshes.

Phew. That was all quite technical and filled with emphasis wasn’t it? OK, so let’s move on…

Objects and Classes

The term “Object,” that gives OOP it’s name, refers to a conceptual object that represents an item in our program or system. This could be anything from an online form or a computer file, to a real world object such as a car.

This representation consists of attributes - the characteristics of our object; and methods - a set of functions and calculations that are either performed to modify the object itself, or are involved in some external effect.

The term “Class” represents the definition (or classification - class) of our object. For example, if we were to write a class called “car”, we could create any number of instances of that class - say “Porsche”, “Ferrari” and “Jaguar”. Each of these instances is an Object. This illustrates that a class is effectively a set of objects that all share common attributes.

Keeping it “in the family”

One of the greatest advantages to using objects is encapsulation. This basically means that data within an object is only available/modifiable via the object’s methods - this is generally known as the interface of the object.

This resultant limitation of scope allows an object-oriented programmer the freedom to declare attributes, variables and methods without having to worry about clashes with those in other objects.

Encapsulation also means that, as long as we don’t alter the interface, we can change how an object works (to increase performance, add functionality etc) without affecting the rest of our system.

Now let’s take a look at how objects and classes can relate to each other.

Note: From this point on, the concepts are going to get a little more complicated. Please bear in mind that I’m writing an introduction here - I fully intend to go over these concepts again (with examples) whilst applying them to Javascript and PHP in the tutorials to follow.

Characteristics of a Class

When writing a class, there are a number of characteristics that are worth taking into account:

Constructor

The constructor of a class is a special operation that is run upon instantiation - when an object is created. They are often distinguished by having the same name as the class itself. It’s main purpose is to set-up the attributes of a class and to establish the class invariant - to basically make sure that the attributes of the class conform to the class interface. It cannot have a return type.

A properly written constructor should never contain any functionality that could fail, thus leaving the object in an invalid state.

Destructor

The Destructor of a class is the opposite of the constructor in that it is run when an object is destroyed. It’s function is to clean up and to free the resources which were used by the object during run-time and unlink it from other objects or resources. This should result in the invalidation of any references in the process.

Relationships

There are a number of relationships that can be used when interaction is needed between objects. These are as follows:

Inheritance

Inheritance allows a (sub)class to inherit all the attributes and methods of a parent class - in effect, extending the parent. It can best be described as an “is a” relationship. For instance, if we had a class of “fruit”, we could extend it by defining classes of “apple”, “orange” and “banana”. Each of these subclasses could be described in the following way:

apple "is a" fruit
orange "is a" fruit
banana "is a" fruit

Because each of our subclasses extends the “fruit” class, it has all the attributes and methods of a “fruit” plus any specific characteristics of it’s own.

Here’s a more web-development-oriented example using form elements and presented in pseudocode:

class formElement
{
Attributes:
id
name
class
}

class input extends formElement
{
Attributes:
value
type
}

class textarea extends formElement
{
Attributes:
cols
rows
}

In this example the two classes “input” and “textarea” have inherited from “formElement”. This means they inherit formElement’s attributes like so:

input is a formElement

input
{
Attributes:
id
name
class
value
type
}

textarea is a formElement

textarea
{
Attributes:
id
name
class
cols
rows
}

However, as the parent (super) class, formElement stays exactly the same:

formElement
{
Attributes:
id
name
class
}

As you can imagine, this relationship can be incredibally useful. Some languages even allow multiple inheritance where a class can have more than one parent (super) class. Sadly this isn’t the case in either Javascript or PHP, however it is possible to obtain the same effect using other types of relationship.

Composition - Association and Aggregation

Composition is a slightly different sort of relationship - this is where it could be said that a class was “composed” of other classes. For instance, a wall is “composed” of bricks and a molecule is “composed” of atoms. Neither of these examples could be described as inheritance - the statement, “a wall is a brick” simply isn’t true. Composition can be described as “has a” and “uses a” relationships; a wall “has a” brick or a wall “uses a” brick.

Let’s take a look at a “has a” relationship in pseudocode. Let’s first define some simple classes:

class brick
{
}

class wall
{
Attributes:
brick1
brick2
brick3
brick4

Methods:
// Constructor
wall()
{
this.brick1 = new brick();
this.brick2 = new brick();
this.brick3 = new brick();
this.brick4 = new brick();
}
}

You can see that “wall” contains a number of brick attributes. We want each of these attributes to be a “brick” object. To do this we simply instantiate them within the constructor of the “wall” class. Each of these brick classes will function as a normal class but also as an attribute of “wall.” This is known as association.

Now let’s take a look at a “uses a” relationship:

class person
{
}

class car
{
Attributes:
driver

Methods:
// Constructor
car(driver)
{
this.driver = driver;
}
}

me = new person();
myMotor = new car(me);

In this example, we can see that the “person” class has been instantiated into the object “me”. This object is then passed into an instantiation of the “car” class. This means that the object “myMotor” (our instantiation of the “car” class) uses the object “me”. This relationship is known as aggregation.

Update: Thanks to Nate Logan for spotting a small error in that last example and emailing me. :-)

The main difference between these two relationships is simple; in association the composing classes are destroyed when the parent is destroyed, however, in aggregation they are not. This ultimately means that, when aggregation is adopted as a relationship, the composing classes and objects can be re-used in other classes and objects within the system.

Polymorphism

An object-oriented programming language must support polymorphism; meaning different classes can have different behaviours for the same attribute or method. This can best be illustrated with an example:

class formElement
{
Attributes:
id
name
class

Methods:
getHtml()
{
// returns generic form element HTML
}
}

class textarea extends formElement
{
Attributes:
cols
rows

Methods:
getHtml()
{
// returns textarea form element HTML
}
}

As you can see in the example, both classes have the method “getHtml” but the “textarea” class is a subclass of the “formElement” class. This results in the “getHtml” method being overloaded. Overloading is a good example of polymorphism in action - an object-oriented language must support polymorphism in order to know which “getHtml” method applies to which object.

Polymorphism, at it’s most basic, describes the fact that a given function may have different specifications, depending on the object to which it is applied.

Why use Object-Oriented programming?

Object-orientation can help keep projects simple by breaking them down in to managable chunks. Those chunks can then be re-used in other projects, thus saving time in the long-run. In fact, adopting an object-oriented approach can be the foundation of a truly successful team environment; through promoting modularity, an object-oriented environment breeds improved code reusability and maintainability.

But don’t just take my word on this; try it for yourself! To better help you to do that, I’m going to follow up this introduction to object-oriented programming with tutorials on it’s use within Javascript and PHP.

So in closing, I hope you’ve learnt something new from this overview. Please feel free to post comments and questions and I’ll endeavour to reply to the best of my ability.

Like this post? Digg it, Del.icio.us it, Ma.gnolia it!

Object-Oriented Programming Concepts——面向对象编程的一些概念

什么是对象? 对象是理解面向对象技术的关键。现在看看你的周围你会发现很多现实世界中对象的例子:你的狗,你的桌子,你的电视机,你的自行车。 现实世界中的对象有两个特点:他们都有状态和...
  • u012654963
  • u012654963
  • 2017年07月03日 08:25
  • 331

操作系统概念 第九版 Operating System Concepts, 9th Edition 中文译稿(不定时更新)

前文: 本译稿不定时更新,翻译自Operating System Concepts, 9th Edition. 欢迎您的阅读,欢迎随时交流,仅供学习,请勿用作商业用途. 自行翻译的,水平不够存在错漏在...
  • advanced_delighted
  • advanced_delighted
  • 2016年03月06日 15:20
  • 3506

视觉SLAM漫谈 (三): 研究点介绍

转自:http://www.robovs.com/discussion-on-visual-slam-iii-the-site-introduction/ 编者按:继视觉SLAM漫谈(一)(二)后,...
  • eric41050808
  • eric41050808
  • 2016年06月20日 01:24
  • 459

Concepts精选片段

040 Before a transaction that modifies data is committed, the following has occurred: Oracle has ge...
  • babymouse1212
  • babymouse1212
  • 2017年05月02日 10:48
  • 96

图——基本概念(Graph - Concepts)

图——基本概念(Graph - Concepts)序言(Preface) Graph algorithms are very practical as a lot of problems in re...
  • UoM_XiaoShuaiShuai
  • UoM_XiaoShuaiShuai
  • 2017年06月11日 09:38
  • 131

concepts阅读总结4——事务

1、事务: 语句级回滚和可恢复空间的分配问题: 语句级的回滚一般发生在一条sql语句执行出现错误的时候,这一条语句的回滚不会影响整个事务,在此语句之前的ddl语句隐式提交的工作都是有效的;...
  • chengxuefei_bj
  • chengxuefei_bj
  • 2013年02月21日 20:53
  • 148

【Java】Java学习笔记之一 ----面向对象(Object-Oriented)

面向对象
  • GO_D_OG
  • GO_D_OG
  • 2017年07月17日 17:53
  • 126

CFNetwork编程指南(一)——CFNetwork概念(CFNetwork Concepts)

CFNetwork是一个低级别高性能的框架,使你能够精细的控制协议栈。它是BSD套接字的扩展,标准套接字抽象API提供对象来简化任务,例如与FTP和HTTP服务器通信或解析DNS主机。CFNetwor...
  • HDFQQ188816190
  • HDFQQ188816190
  • 2016年09月26日 18:13
  • 290

面向对象数据库(Object Oriented Databases)

前面说几句费话。现在正在从事面向对象数据库在国内的推广工作,如果有兴趣可以与我联系。如果有任何问题可以私信我,也可以到我们网站上 面向对象数据库交流社区 来向我提问,我一定以最快的速度解答。 ...
  • grlsir
  • grlsir
  • 2014年09月30日 09:28
  • 2161

Gatling官网教程翻译之Concepts

名词解释,原始文章地址:http://gatling.io/docs/2.1.7/general/concepts.htmlVirtual User – 虚拟用户一些负载测试工具,像ab、wrk,在u...
  • dongmu1986
  • dongmu1986
  • 2016年01月20日 21:10
  • 1689
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Object-Oriented Concepts
举报原因:
原因补充:

(最多只允许输入30个字)