Professional Documents
Culture Documents
inheritence
delegation
muter: getter/setter
encapsulation
hiding
abstraction
impliciet/explucit dependency
dependency injection
S.O.L.I.D
desgin patterns
anti pattterns
refactoring
don't program to class, program to interface
make class data private/ use public interface to access to them
static example:
company name, global names
4840000
403333.33333333333333333333333333
275
1757295542
09226288184
mASOMEH5333
94/7/30
Abbott’s Technique
java
To configure the language level in IntelliJ go to File > Project Structure >
Project Settings > Project > Project language level To ensure the chagnes are
applied, restart IntelliJ, although in my case it wasn't necessary
reference
chapters from a more general introduction to programming
Code Complete
Head First Object-Oriented Analysis and Design
Building Skills in Object-Oriented Design
Practical Object-Oriented Design in Ruby
Clean Code: A Handbook of Agile Software Craftsmanship
The Clean Coder: A Code of Conduct for Professional Programmers
Agile Software Development, Principles, Patterns, and Practices
UML for Java Programmers
Check out all of them here Amazon.com: Robert C. Martin: Books, Biography, Blog,
Audiobooks, Kindle
Design Patterns: Elements of Reusable Object-Oriented Software
Test Driven Development: By Example (Kent Beck)
Refactoring: Improving the Design of Existing Code: Martin Fowler
Applying UML and Patterns: Craig Larman
Delegation
-Assume that there are no classes exists. I create objects in the memory which
contains both the data and the methods definition (code).
So one can invoke the required method by directly calling the object.
For example : I create an object Point P1 which contains the coordinates (x,y) as
data and draw() as the method. P1's X = 100, Y = 100
Suppose I want to create another object Point P2, in the location X = 200, Y = 100.
In this case I create another object P2 with just X = 200 and a delegation pointer
to P1
Now if I want to draw P2, I invoke draw method on P2. But P2 does not have the code
for draw method so it delegates to P1 and tell if you have draw method then execute
it and use my X value.
Now P1 has the draw method and it uses P2's X value and P1's Y value to draw the
point.
This is a process of delegation. Suppose I invoke erase() method on P2. It will
delegate to P1, but P1 does not have the method and also it does not have any more
delegation pointer, hence it will result in a runtime exception.
Now I create an instance which is an object (like P P1 = new P()). Now object P1
will contain the copy of all the data member i.e. X and Y. But it does not contain
the code for draw method.
If I call P1.draw(), it will delegate this call to the Class which contains the
code for draw. Now the Point is drawn. Now there is a catch, if I create so many
instances which will share the same draw method code, then how a class can identify
the correct data of an instance? Now comes the concept of 'this' pointer. When
P1.draw() is called, 'this' pointer or 'self' object is passed as one of the
argument. So the class will use 'this' pointer to identify the correct data and
execute the method. This is how delegation happens in OOP.
For example:
When we write method
class P
{
int x;
int y;
void draw()
{
Graphics.drawPoint(x,y);
}
}
The draw() method will be transformed as :
So when P1.draw() is invoked. It pass the 'self' as the argument like P1.draw(P1)
and the Point is drawn using the data of the self object.
The same delegation process happens in the case of inheritance it checks the
method exits in the subclass, if does not, then it delegates to super class
Consider if you were modeling an employee that has an address. You might make
address an attribute of employee.
However, if address had some behavior of its own, you would likely model address as
its own concept and delegate responsibility to it.
The classic example of delegation is modeling the sale of a product. The simple
model would have the product have a behavior sell. However instead, a sale is more
commonly delegated to as a separate concept, reflecting the complexity inherent in
that sale concept. A call to the product’s sell operation, would then delegate to
the sale object.
That is in reality about delegation. Consider what the GOF then go on to say …
-Some parents want to go out. They hire a babysitter and tell her to look after the
baby.
But what if the baby gets sick?
There's a note on the fridge door. "In case of emergency phone Helen 555-3141"
This is a pretty good analogy for delegation. Helen is the delegate.
//within the parent class
babysitter.delegate = helen
[babysitter lookAfter:baby]
When assigning actions to an object, you can provide that object with a delegate.
The delegate will be called when the object needs to inform something or do
something beyond the scope of the object.