Saturday, June 28, 2008
How to implement the Least Knowledge Principle
1. The object itself
2. The object that is parameterised to the method
3. The object that a method created or instantiated
4. Any component of object
The Least Knowledge Principle
Thursday, June 26, 2008
SOLID will make you a solid programmer
The below article is referenced from http://mmiika.wordpress.com/oo-design-principles/
S.O.L.I.D. Class Design Principles
Collected by Robert C. Martin for his book “Applying Principles and Patterns”,
Single Responsibility Principle (SRP)
A class should have only one reason to change. For example, if a change to the business rules causes a class to change, then a change to the database schema, GUI, report format, or any other segment of the system should not force that class to change.
- http://davidhayden.com/blog/dave/archive/2005/05/29/1066.aspx
- http://c2.com/cgi/wiki?SingleResponsibilityPrinciple
- Head First Design patterns page 185, 336, 339, 367
- http://msdn.microsoft.com/en-us/magazine/cc546578.aspx
Open/Closed Principle (OCP)
Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.
- http://davidhayden.com/blog/dave/archive/2005/06/04/1096.aspx
- http://en.wikipedia.org/wiki/Open/closed_principle
- Head First Design patterns page 86-87, 407
- http://c2.com/cgi/wiki?OpenClosedPrinciple
Liskov substitution principle (LSP)
Subtypes must be substitutable for their base types. If class A inherits from class B, then wherever you can use A you should be able to use B. E.g. remember that square is not necessarily a rectangle! When extending: Preconditions cannot be straightened, Postconditions cannot be loosened, visible Invariants cannot be changed (?). Invariants: users depend on this both before and after sending a message. Use a proper set-based inheritance relationship. Not following set semantics is very risky. Subsumption Rule: A reference to a subtype can be used in any context where a reference to a super type is expected. This principle extremely limits what SHOULD be done with the pure extension (inheritance) mechanism. Do not follow at your own risk.
- http://davidhayden.com/blog/dave/archive/2005/06/10/1226.aspx
- Agile Principles and Patterns page ?
- http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple
- http://en.wikipedia.org/wiki/Liskov_substitution_principle
- http://javaboutique.internet.com/tutorials/JavaOO/
Interface Segregation Principle (ISP)
The dependency of one class to another one should depend on the smallest possible interface.
- http://davidhayden.com/blog/dave/archive/2005/06/15/1482.aspx
- http://c2.com/cgi/wiki?InterfaceSegregationPrinciple
- http://www.google.com/search?q=interface+segration+principle
- http://doodleproject.sourceforge.net/articles/2001/interfaceSegregationPrinciple.html
Dependency Inversion Principle (DIP)
Depend upon abstractions (interfaces), not upon concrete classes.
Thursday, June 19, 2008
Positioning dom element
This is the default way that an element is positioned; it simply follows
the normal flow of the document. The top and left properties have no effect when an element
has static positioning. eg. position: static; top: 0px; left: 0px;
Relative positioning:
This means the positioning is very similar to static positioning, as the
element will continue to follow the normal flow of the document until instructed to do
otherwise. However, setting the top or left properties will cause the element to be shifted
in relation to its original (static) position. eg. position: relative; top: -50px; left: 50px;
Absolute positioning:
Positioning an element absolutely completely breaks it out of the
normal flow of page layout. An element that’s been positioned absolutely will be displayed
in relation to the first parent element that has a nonstatic position. If no parent
exists, it’s positioned in relation to the entire document. eg. absolute; top: 20px;
left: 0px;
Fixed Positioning:
Fixed positioning works by positioning an element relative to the
browser window. Setting an element’s top and left to 0 pixels will display that element
in the top-left corner of the browser for as long as the user is on that page, completely
ignoring any use of the browser’s scrollbars. position: fixed; top: 20px; right: 0px;
OffsetParent:
offsetParent: This is the parent that an element is positioned within. However,
in practice, the element that offsetParent refers to depends on the browser (for
example, in Firefox it refers to the root node, and in Opera, the immediate parent).
offsetLeft and offsetTop: These parameters are the horizontal and vertical offsets of the
element within the context of the offsetParent. Thankfully, this is always accurate in
modern browsers.
Friday, June 13, 2008
Concept of reference in javascript
// Set items to an array (object) of strings
var items = new Array( "one", "two", "three" );
// Set itemsRef to a reference to items
var itemsRef = items;
for(el in itemsRef) alert(itemsRef[el])
The output will be "one", "two", "three".
Let add some more element to an array through push() method
items.push("four");
items.push("five");
for(el in itemsRef) alert(itemsRef[el])
The output will "one", "two", "three", "four", "five"
The itmesRef has a reference to items, thus anychange to items will also change in itemsRef
Thursday, June 12, 2008
Event in javascript
In javascript event also an object that can be attached to any element. In IE event object is a global that can be access anywhere in the page (e = window.event), whereby other browser beside IE, the event object (e) is need to passed in with call back function
eg. element.onclick = function( /*For none IE browser*/e){
if(e) {
alert("this is none IE event object" + e)
}else{
e = window.event;
alert("this is IE event object" + e)
}
}
2. Stop Event Capturing and Bubbling
IE : window.event.cancelBubble = true
Mozilla: e.stopPropogation()
3. Stop Default Browser event
A browser event is an event that fired by the browser which you don't really realize about it, like when you right click on any element, the browser will fire its event by default. To stop that let look at this.
IE : window.event.returnValue = false;
Mozilla : e.preventDefault();
4. Attach Event to An Element
There are two ways which you can attach event to any dom element.
1. Using classical way
element.onclick = function(){alert("event click happens")};
2. Using attachEvent/addEventListener
Mozilla : dom.addListener("click",function(){}, false/true );
Dom Object in javascript
1. nodeType key word
- element.nodeType = 9 meaning the rood of the element
- element.nodeType = 3 meaning the element is a text
- element.nodeType = 1 match any element like
Tuesday, June 10, 2008
assingment operator " = "
e.g var number1;
var number2 = 3 ;
number1 = number2;
number2 = 5 ;
alert(number1) ;
Guest what? 3 or 5.
The right answer is 5 not 3, since its reference has been changed to 5.
Monday, June 9, 2008
Remove child node for parent node
e.g var parentDom = document.CreateElement("div");
var child1 = document.CreateElement(''div'');
var child2 = document.CreateElement(''div'');
var child3 = document.CreateElement(''div'');
//Append all the child to parentDom
parentDom.appendChild(child1);
parentDom.appendChild(child2);
parentDom.appendChild(child3);
//Let use 'removeChild' key word and FOR LOOP to remove them
for(var i = 0 ; i < parentDom.childNodes.length ; i++){
parentDom.removeChild(parentDom.childNode[i]);
}
You sure, this will work ? oops it won't, because when you remove the first element the second element will be moved to the first index, that make the second index blank which will cause the error.
//It will work like this
//Let use 'removeChild' key word and FOR LOOP to remove them
for(var i = 0 ; i < parentDom.childNodes.length ; i++){
parentDom.removeChild(parentDom.childNode[0]);
}
Create artificial event to the dom object
//Generate an artificial click event on "test". Fires alert("hi")
var clickevent=document.createEvent("MouseEvents")
clickevent.initEvent("click", true, true)
document.getElementById("test").dispatchEvent(myevent)
Thursday, June 5, 2008
The use of keyword 'continue' in for 'if' statement
var j = 3;
for( var i = 0 ; i < 5 ; i++ ){
if( i == j) continue;
}
Memorise the index for click event in FOR loop
Here is the solution, we use closure here to solve this problem
//User activities -- switching contents
for (var i = 0; i < tabs.length; i++) {
(function() {
var j = i;
eventObj.addDomListener(
self._tabContainerDoms[j],
'click',
function() {
self._tabContainerDoms[j].style.height = parseInt(self._tabContainerDoms[j].style.height) + 1 + "px";
}
);
})();
}