Monday, April 1, 2013

Design Patterns - Decorator Pattern

Introduction

A design pattern is a specific way of solving a particular problem. They are not simply about reusing code; they are more abstract and generalized than that. They are about using reusing ideas.

The Decorator Pattern

The two concrete widgets created for the observer pattern have a different appearance. If you needed to add a new style of widget, you would subclass Widget and implement the draw() method to write the HTML. Say that needed to add a feature to all existing widgets, such as border. You could go into each draw() method and add some more HTML to each, but then the border would be hard-coded and all widgets would be forced to have a border. You could create a new set of subclasses of existing concrete widgets that implemented the border in the draw() method. If you had only two widgets, as in the Observer example, this might be an option - you would end up with four widgets. However, if you had five widgets to start with, it might not seem so appealing. Suddenly you have a lot of widget classes to worry about.

There is another way to handle situations like this, one that doesn't require you to create a new subclass for every widget you want to add a border to. Using the decorator pattern allows you to add features or functionality to existing objects without using inheritance. Next figure shows the class diagram for the decorator pattern.

The class diagram indicates that Decorators are a type of Widget, too. This is good because we don't want to change the way we access a Widget, whether it's decorated or not.

All text and images above is from the book:"Professional PHP5" ISBN: 0-7645-7282-2 (Lecky-Thompson et al., 2005)

Thursday, March 21, 2013

Design Patterns - Composite Pattern

Introduction

A design pattern is a specific way of solving a particular problem. They are not simply about reusing code; they are more abstract and generalized than that. They are about using reusing ideas.

Composite Pattern

The next figure shows the general case for the composite:

The composite pattern has two parts: the Component abstract class and the Composite, which is a concrete implementation of the Component class. All Composite objects descend from the abstract Component class. Any Component can contain other Components. A Component with no children can be thought of as an empty Composite.

Moving back from the general case, it will be necessary to make a change in the design and switch the Instrument interface to an abstract class. Interfaces and abstract classes are similar because neither one can be used directly to instantiate an object. The key difference is that an abstract class can have some fully implemented methods, whereas interfaces just have method declarations.

All text and images above is from the book:"Professional PHP5" ISBN: 0-7645-7282-2 (Lecky-Thompson et al., 2005)

Monday, March 11, 2013

Commands Vi/Vim editor

As system administrator, I have needed to edit configuration files of some services such as DNS (Domain Name System), LDAP (Lightweight Directory Access Protcol) and etc.

The editor I was using frequently was nano and rarely I used pico. All those editors were used on Ubuntu. When I decided to fit in CentOS, I noticed that I had to install package nano. But I did not want to! I wanted to vary and then I searched and found editor Vi (also it is equal to Vim, although Vim has more features than Vi). I confess, at first time, it was very confused. I did not know what commands to edit file and much less how to save the changes and exit. So I googled and found some commands useful to edit files.

Hereafter, you will see a list of commands to Vi/Vim editor:

CommandDescription
:wqSave the file and exit
:wSave the file
:qQuit and Vi/Vim refuses to exit if any changes was made
:!qQuit without changes to save
:aAppend after cursor
:iInsert after cursor
ESCExit insert/overwrite mode and go back to command mode

Tuesday, February 19, 2013

How to create tabs with JavaScript and HTML

If you want to create tabs on your website, it is good idea and you should do it! But how to create tabs? Good question!

To create tabs you will use HTML elements and some code of JavaScript. Hereafter will show up HTML code.

<!-- tabs -->
<ul class="tabs">
<li><a href="#">Tab One</a></li>
<li><a href="#">Tab Two</a></li>
</ul>

<!-- panes -->
<div class="panes">
<div>Pane One</div>
<div>Pane Two</div>
</div>

Next you need JavaScript code to bind tabs to panes and enable the tabbing effect. Here is JavaScript code you need to.

$(function(){
$("ul.tabs").tabs("div.panes > panes ");
});

After HTML code and JavaScript Code, you can use your imagination to coding CSS. I like this snippet because it is minimal, easy and fast to see results.

Tuesday, February 5, 2013

Configuring TCP/IP with CentOS

The configuration of network TCP/IP allows prepare a server Linux to communicate on network IP, trough setting network interfaces, addresses IP and remaining parameters of configuration. On this article, I will analyse how the way can commands to be used on Linux server.

I had a chance to analyse the main files of network configuration used on Linux CentOS and I saw that a lot of those files appears on most Linux distro. Also it is important to have a knowledge of some commands like ifconfig and route. Why is it important? Because those commands will allow you to analyse and solve problems of network configuration in future.