You are on page 1of 4

OOP

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

when we use enum ?


-category types such as: guitar builders, guitar types, car builders, guitar
material, ...

when use strings?


-serial numbers/codes/ since no calculate done on them and take less memory then to
numbers
-stuff models as guitar models

4840000

403333.33333333333333333333333333
275

1757295542

09226288184

mASOMEH5333

94/7/30

EXAMPLES DESIGN PROJECTS:


-inventory guitar
-inventory of any product
-cofeeShop managment
-simulators of ducks/brids/animals/cars/weapons

Step by Step (text editor)(for each itrate: pleasure customer/oop desgin/reuse)


-more essintal futures: qui and wrting text
-itrate 2: open/save
-itrate 3: find text
-itrate 4: replace
-itrate x
delegate: object send responsibility to other object, this done through program to
interface not class rule
object.equal(anotherObject) is an example

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 lets come to Object Oriented Programming:


In OOP, we have the concept of class. So what I do is I provide the data member
definition and method definition in the class.
Let say we create a class Point P with data member as X and Y along with draw
method.

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 :

void draw(this obj)


{
Graphics.drawPoint(obj.x, obj.y);
}

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

-In object-oriented design, it is the delegation of an object’s responsibility to


another object. It is typically implemented using composition. Delegation can be
considered the way you use composition as an alternative to inheritance.

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.

You may have heard the saying …

“Favor object composition over class inheritance” [1]

That is in reality about delegation. Consider what the GOF then go on to say …

“Delegation is a way of making composition as powerful for reuse as inheritance


[Lie86, JZ91]. In delegation, two objects are involved in handling a request: a
receiving object delegates operations to its delegate. This is analogous to
subclasses deferring requests to parent classes. But with inheritance, an inherited
operation can always refer to the receiving object through the this member variable
in C++ and self in Smalltalk. To achieve the same effect with delegation, the
receiver passes itself to the delegate to let the delegated operation refer to the
receiver

-Think of objects as independent agents or individuals that have taken upon


themselves certain responsibilities, exactly as people do in the "real" world.
What we want to avoid is objects becoming too complex. As in the real world, when
the tasks become too complex, you ask for help. This is what delegation between
objects is: ask someone else to help you. Or, in other words: delegate part of your
responsibility to another object that specialises in a part of what you need to
accomplish
An example is drawing. You might be a drawing object, collaborating with a set of
other objects such as figures and text. The actual drawing is mostly delegated to
those objects. You have the responsibility to draw, you delegate that to your
collaborators and ask them to draw (in this case polymorphism comes into play as
well, but that as an aside...). You become simpler. Simpler objects is better. Your
collaborators can become simpler as well, for example the text object only needs to
know how to draw textual elements (working with other objects as well, such as
fonts!). The figure objects do not need such knowledge
The result of delegation is a well-balanced set of simple objects, each playing a
part in an overall potentially very complex task.
When implementing all this in a programming language, your collaborator objects
usually are implemented as members of your class. But not always, they might be
more transient, for example local variables inside a method. No matter how they are
implemented, the actual delegation is done by sending a request in the form of a
message calling a method in the class of the receiver, to do the work.

-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]

//within the babysitter class


if (baby.isSick)
{
[delegate callAbout: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.

You might also like