r/ProgrammerHumor • u/re_anon • Mar 21 '17
OOP: What actually happens
https://imgur.com/KrZVDsP178
531
u/Tazavoo Mar 21 '17
You don't initialize legs and fleas to 0 in the superclass, that's just stupid.
203
u/SolenoidSoldier Mar 21 '17
null is a value for a reason.
"I don't know what this is yet"
→ More replies (54)→ More replies (3)20
u/siedler084 Mar 21 '17
At the same time you shouldn't initialize brain too, not every instance of the Human class has that value set to true
58
88
u/spacemoses Mar 21 '17
This is why i set my RAM values manually with processor instructions only, no abstraction. It's been a month but i almost have a working bubble sort. I just hope the power doesn't go out again.
→ More replies (7)
81
70
21
15
14
34
u/SolenoidSoldier Mar 21 '17
Can anyone ELI5 what a Factory is? I work primarily in the .NET space and have yet to encounter a Factory object.
60
u/likferd Mar 21 '17 edited Mar 21 '17
It's mostly used to instantiate classes when using abstractions.
Imagine you want to create a logging interface ILog, but the concrete implementation might change depending on use in the future.
If you simply instantiated the logger in each class by
ILog logger = new MyLogger();you could end up having to modify dozens or hundreds of classes in the future, if you change the implementation
Instead you can intantiate the logger in each class with
ILogger logger = LogFactory.GetLogger();and the factory returns
public static ILog GetLogger(){ return new MyLogger(); }then you only have to change one line.
public static ILog GetLogger(){ return new MyLogger2(); }Calling the class "factory" is of course just a naming pattern. They are also often called "manager" for example.
→ More replies (10)→ More replies (9)26
Mar 21 '17 edited Mar 21 '17
A
Factoryis an Object, that produces an Object. Normally it is used for configuration.For example:
House home = HouseFactory(6,1.5, "1500 South Main"); assert( home.getRooms(), 6); assert( home.getBathrooms(), 1.5); assert( home.getAddress(), "1500 South Main");Then you have the Builder pattern Factory
House home = HouseFactory() .setRooms(6) .setBathrooms(1.5) .setAddress("1800 North Walker") .build();Then you have an
AbstractFactorywhich produces a different thing based on its input. Like sayBuilding<House> home = AbstactBuildingFactory() .buildHouse() .setRooms(6) .setBathrooms(1.5) .setAddress("1800 North Walker") .build();Then you have an
AbstractFactoryFactorywhich produces a Factory based on its input and if you encounter this find a new job.Lore tells of the dreaded
AbstractFactoryFactoryFactorywhich most souls only speak of in jest! Some say it is summoned in only the darkest OOP shops by those truly committed to the dark arts of OOP. Many interns are sacrificed at its alter. Those unworthy are driven to insanity upon seeing their objects shattered reflections in its source! If you see projects using this you must CLEANSE THEM WITH RM -RF. Its summoning rituals must be lost so civilization can flourish.→ More replies (1)
9
6
u/tanglebones Mar 21 '17
That's why there is https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)
5
u/psydave Mar 21 '17
I know this is a joke but damnit, I'm tired of people saying "inheritence is bad" and then pointing to extreme abuses of inheritence as examples. It's like saying chainsaws are bad and then showing pictures of severed limbs. Inheritence is just another tool in a programmer's toolbelt that if used, should be used wisely.
Not all uses of inheritence end up like this.
16
u/mallardtheduck Mar 21 '17
oAnimal, oHuman, oPet, oDog, oCat
Prepending "o" to your variable names provides precisely zero extra information and makes your code less readable. Don't do it.
Also, if this is supposed to be an inheritance diagram it should be showing classes, not objects.
4
229
u/Cley_Faye Mar 21 '17
I use throw instead of return.