Clean Architecture



Download 6,37 Mb.
Pdf ko'rish
bet32/259
Sana26.02.2022
Hajmi6,37 Mb.
#465587
1   ...   28   29   30   31   32   33   34   35   ...   259
Bog'liq
Clean Architecture A Craftsman’s Guide to Software Structure and Design by Robert C. Martin (z-lib.org)

I n h e r ita n c e ?
If OO languages did not give us better encapsulation, then they certainly gave 
us inheritance. 
Well—sort of. Inheritance is simply the redeclaration of a group of variables 
and functions within an enclosing scope. This is something C programmers
3
were able to do manually long before there was an OO language.
2. For example, Smalltalk, Python, JavaScript, Lua, and Ruby.
3. Not just C programmers: Most languages of that era had the capability to masquerade one data struc-
ture as another.
www.EBooksWorld.ir


Chapter 5 Object- Oriented Programming
38
Consider this addition to our original 
point.h
C
program:
namedPoint.h
struct NamedPoint;
struct NamedPoint* makeNamedPoint(double x, double y, char* name);
void setName(struct NamedPoint* np, char* name);
char* getName(struct NamedPoint* np);
namedPoint.c
#include "namedPoint.h"
#include
struct NamedPoint {
double x,y;
char* name;
};
struct NamedPoint* makeNamedPoint(double x, double y, char* name) {
struct NamedPoint* p = malloc(sizeof(struct NamedPoint));
p->x = x;
p->y = y;
p->name = name;
return p;
}
void setName(struct NamedPoint* np, char* name) {
np->name = name;
}
char* getName(struct NamedPoint* np) {
return np->name;
}
www.EBooksWorld.ir


Inheritance?
39
main.c
#include "point.h"
#include "namedPoint.h"
#include
int main(int ac, char** av) {
struct NamedPoint* origin = makeNamedPoint(0.0, 0.0, "origin");
struct NamedPoint* upperRight = makeNamedPoint 
(1.0, 1.0, "upperRight");
printf("distance=%f\n",
distance(
(struct Point*) origin, 
(struct Point*) upperRight));
}
If you look carefully at the 
main
program, you’ll see that the 
NamedPoint
data structure acts as though it is a derivative of the 
Point
data structure. 
This is because the order of the first two fields in 
NamedPoint
is the same as 
Point
. In short, 
NamedPoint
can masquerade as 
Point
because 
NamedPoint
is a pure superset of 
Point
and maintains the ordering of the 
members that correspond to 
Point
.
This kind of trickery was a common practice
4
of programmers prior to the 
advent of OO. In fact, such trickery is how C++ implements single 
inheritance. 
Thus we might say that we had a kind of inheritance long before OO 
languages were invented. That statement wouldn’t quite be true, though. We 
had a trick, but it’s not nearly as convenient as true inheritance. Moreover, 
multiple inheritance is a considerably more difficult to achieve by such 
trickery.
4. Indeed it still is.
www.EBooksWorld.ir



Download 6,37 Mb.

Do'stlaringiz bilan baham:
1   ...   28   29   30   31   32   33   34   35   ...   259




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish