SlideShare una empresa de Scribd logo
1 de 24
Descargar para leer sin conexión
Anti- Object Oriented Design Patterns
Alvaro Polo

DEVCON	
  2013	
  
Design Patterns

What’s a design pattern?

“A	
   general	
   reusable	
   solu-on	
   to	
   a	
   commonly	
  
occurring	
   problem	
   within	
   a	
   given	
   context	
   in	
  
so8ware	
  design”,	
  Wikipedia	
  

DEVCON	
  2013	
  
Design Patterns

But, what kind of commonly ocurring problem
are we talking about?

DEVCON	
  2013	
  
Design Patterns

No, this is all about software design

DEVCON	
  2013	
  
Problems unsolved by the language

_uppercase:
pushq
%rbp
.cfi_def_cfa_offset 16
.cfi_offset %rbp, -16
movq %rsp, %rbp
.cfi_def_cfa_register %rbp
movq %rdi, -8(%rbp)
movl %esi, -12(%rbp)
movl $0, -16(%rbp)
LBB0_1:
movl -16(%rbp), %eax
cmpl -12(%rbp), %eax
jge LBB0_3
movslq
-16(%rbp), %rax
movq -8(%rbp), %rcx
movb (%rcx,%rax), %dl
movb %dl, -17(%rbp)
movsbl
-17(%rbp), %esi
cmpl $97, %esi
jl
LBB0_2

DEVCON	
  2013	
  

movsbl -17(%rbp), %eax
cmpl $122, %eax
jg
LBB0_2
movsbl
-17(%rbp), %eax
addl $65, %eax
subl $97, %eax
movb %al, %cl
movslq
-16(%rbp), %rdx
movq -8(%rbp), %rsi
movb %cl, (%rsi,%rdx)
LBB0_2:
movl -16(%rbp), %eax
addl $1, %eax
movl %eax, -16(%rbp)
jmp LBB0_1
LBB0_3:
popq %rbp
ret
Problems unsolved by the language

_uppercase:
movsbl -17(%rbp), %eax
pushq
%rbp
cmpl $122, %eax
.cfi_def_cfa_offset 16
jg
end_if
.cfi_offset %rbp, -16uppercase(char *str, int strl) {
movsbl
-17(%rbp), %eax
void
movq %rsp, %rbp
addl i++) {
for (int i = 0; i < strl; $65, %eax
.cfi_def_cfa_register %rbp
char c = str[i]; subl $97, %eax
movq %rdi, -8(%rbp)
movb %al,
if (c >= 'a' && c <= 'z') %cl
movl %esi, -12(%rbp)
-16(%rbp), %rdx
str[i] = 'A' movslq 'a';
+ c movl $0, -16(%rbp) }
movq -8(%rbp), %rsi
begin_for:
movb %cl, (%rsi,%rdx)
}
movl -16(%rbp), %eax
end_if:
cmpl -12(%rbp), %eax
movl -16(%rbp), %eax
jge end_for
addl $1, %eax
movslq
-16(%rbp), %rax
movl %eax, -16(%rbp)
movq -8(%rbp), %rcx
jmp begin_for
movb (%rcx,%rax), %dl
end_for:
movb %dl, -17(%rbp)
popq %rbp
movsbl
-17(%rbp), %esi
ret
cmpl $97, %esi
jl
end_if

DEVCON	
  2013	
  
Problems caused by the language

We want to represent IPv4 addresses in our Java
program. Suggestions?
class IpV4Address {
private uint bits = 0;
public static IpV4Address fromCidr(String cidr) {
...
}
public bool isInSameNetwork(IpV4Address addr, Netmask netmask) {
...
}
...
};

DEVCON	
  2013	
  
Problems caused by the language

We want to represent IPv4 addresses in our
program. Suggestions?
class IpV4Address {
private uint bits = 0;
public static IpV4Address fromCidr(String cidr) {
...
}
public bool isInSameNetwork(IpV4Address addr, Netmask netmask) {
...
}
...
};

DEVCON	
  2013	
  
Problems caused by the language

Let	
  me	
  introduce	
  the	
  An@-­‐	
  Object	
  Oriented	
  Design	
  PaGerns	
  

DEVCON	
  2013	
  
Singleton Pattern

DEVCON	
  2013	
  
Singleton Pattern
public class Application extends Configurable {
private static Application instance;

OOP establishes some constraints:
ü Every object is bounded to a}lifecycle
private Application() { ...
ü Every classstaticas many instances as fit into
has Application getInstance() {
public
if
memory (instance == null)
instance = new Application();
return instance;
}
// The functionality of the application goes here
...

Singleton is fighting against these rules!
}

DEVCON	
  2013	
  
Singleton Pattern

What if we bend the rules? ...
object Application extends Configurable {
// The functionality of the application goes here
...
}

DEVCON	
  2013	
  
Strategy and Co.

DEVCON	
  2013	
  
Strategy and Co.
interface Player {
void attack(Player enemy);
// Other stuff the player does
...
}
interface Weapon {
void attack(Player enemy);
}

ü  Java is a pure-OOP programming
language
ü  Everything is modeled using
objects

class AbstractPlayer implements Player {
private Weapon weapon;
public setWeapon(Weapon weapon) {
this.weapon = weapon;
}
public void attack(Player enemy) {
weapon.attack(enemy);
}
}
class Axe implements Weapon { ... }
class Sword implements Weapon { ... }
class Bow implements Weapon { ... }

DEVCON	
  2013	
  

Strategy is fighting against the lack
of other mechanism to represent
behavior!
Strategy and Co.
What if we have a non-pure OOP language?
class player {
public:
typedef std::function<void(Player&)> weapon;
void set_weapon(weapon& w) { _weapon = w; }
void attack(player& enemy) {
_weapon(enemy);
}
private:
weapon _weapon;
};
void axe(player& enemy) { ... }
void sword(player& enemy) { ... }
void bow(player& enemy) { ... }
DEVCON	
  2013	
  
Strategy and Co.

The lack of functions is not a matter exclusive for strategy
pattern
ü  Observer pattern
ü  Factory (method) pattern
ü  Adapter pattern
ü  …

DEVCON	
  2013	
  
Visitor

DEVCON	
  2013	
  
Visitor
interface CarElementVisitor {
void visit(Wheel wheel);
void visit(Engine engine);
void visit(Body body);
void visit(Car car);
}
interface CarElement {
void accept(CarElementVisitor visitor);
}
class Wheel implements CarElement {
public void accept(CarElementVisitor visitor) {
visitor.visit(this);
}
}
class Engine implements CarElement {
public void accept(CarElementVisitor visitor) {
visitor.visit(this);
}
}
class Body implements CarElement {
public void accept(CarElementVisitor visitor) {
visitor.visit(this);
}
}

DEVCON	
  2013	
  

class Car implements CarElement {
public void accept(CarElementVisitor visitor) {
for(CarElement elem : elements)
elem.accept(visitor);
visitor.visit(this);
}
}
class Mechanic implements CarElementVisitor {
public void visit(Wheel wheel) { ... }
public void visit(Engine engine) { ... }
public void visit(Body body) { ... }
public void visit(Car car) { ... }
}
Visitor
Why not simply…?
interface CarElementVisitor {
void visit(Wheel wheel);
void visit(Engine engine);
void visit(Body body);
void visit(Car car);
}
class Car implements CarElement {
public void accept(CarElementVisitor visitor) {
for(CarElement elem : elements)
visitor.visit(elem);
visitor.visit(this);
}
elem is a
}

CarElement, and
visit(CarElement)

DEVCON	
  2013	
  

is not defined in
visitor!
Visitor
Call visit(Wheel w)
interface CarElement {
void accept(CarElementVisitor visitor);
}
class Wheel implements CarElement {
public void accept(CarElementVisitor visitor) {
visitor.visit(this);
}
}
class Engine implements CarElement {
public void accept(CarElementVisitor visitor) {
visitor.visit(this);
}
}
visit(Engine

Call

class Body implements CarElement {
public void accept(CarElementVisitor visitor) {
visitor.visit(this);
}
}

Call visit(Body b)
DEVCON	
  2013	
  

That’s why we need to
redefine accept() again
and again

e)
Visitor

What if we dispatch the message based on…
ü The receiver of the message, and…
ü The arguments of the message?
Let me introduce the double dispatch!
Visitor pattern is there to provide double dispatch
semantics not supported by the language
DEVCON	
  2013	
  
Conclusions

DEVCON	
  2013	
  
Conclusions

ü  Design patterns are bad
ü  NOOOOOOOOOOO!!!!!!!
ü  Design patterns are useful, even when they fight against the
language
ü  We must learn from our errors and languages must evolve to
avoid them
And some opinions
ü  OOP as implemented by Java is almost over
ü  Learn Scala
DEVCON	
  2013	
  
Q&A

DEVCON	
  2013	
  

Más contenido relacionado

Similar a Anti- Object Oriented Design Patterns Reveal Problems

Protocol-Oriented Programming in Swift
Protocol-Oriented Programming in SwiftProtocol-Oriented Programming in Swift
Protocol-Oriented Programming in SwiftOleksandr Stepanov
 
How AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsHow AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsRan Mizrahi
 
Patterns Are Good For Managers
Patterns Are Good For ManagersPatterns Are Good For Managers
Patterns Are Good For ManagersAgileThought
 
How to code to code less
How to code to code lessHow to code to code less
How to code to code lessAnton Novikau
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Savio Sebastian
 
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014Susan Potter
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence APIIlio Catallo
 
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001Sven Ruppert
 
WebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open webWebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open webpjcozzi
 
SOLID Design Principles applied in Java
SOLID Design Principles applied in JavaSOLID Design Principles applied in Java
SOLID Design Principles applied in JavaIonut Bilica
 
Que hay de nuevo en Visual Studio 2013 y ASP.NET 5.1
Que hay de nuevo en Visual Studio 2013 y ASP.NET 5.1Que hay de nuevo en Visual Studio 2013 y ASP.NET 5.1
Que hay de nuevo en Visual Studio 2013 y ASP.NET 5.1Rodolfo Finochietti
 
Errors detected in the Visual C++ 2012 libraries
Errors detected in the Visual C++ 2012 librariesErrors detected in the Visual C++ 2012 libraries
Errors detected in the Visual C++ 2012 librariesPVS-Studio
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahNick Plante
 
Design patterns with Kotlin
Design patterns with KotlinDesign patterns with Kotlin
Design patterns with KotlinMurat Yener
 
HTML5 for the Silverlight Guy
HTML5 for the Silverlight GuyHTML5 for the Silverlight Guy
HTML5 for the Silverlight GuyDavid Padbury
 
Developing Native Mobile Apps Using JavaScript, ApacheCon NA 2014
Developing Native Mobile Apps Using JavaScript, ApacheCon NA 2014Developing Native Mobile Apps Using JavaScript, ApacheCon NA 2014
Developing Native Mobile Apps Using JavaScript, ApacheCon NA 2014Hazem Saleh
 

Similar a Anti- Object Oriented Design Patterns Reveal Problems (20)

Protocol-Oriented Programming in Swift
Protocol-Oriented Programming in SwiftProtocol-Oriented Programming in Swift
Protocol-Oriented Programming in Swift
 
How AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsHow AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design Patterns
 
Patterns Are Good For Managers
Patterns Are Good For ManagersPatterns Are Good For Managers
Patterns Are Good For Managers
 
How to code to code less
How to code to code lessHow to code to code less
How to code to code less
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2
 
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
 
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
 
WebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open webWebGL: GPU acceleration for the open web
WebGL: GPU acceleration for the open web
 
SOLID design principles applied in Java
SOLID design principles applied in JavaSOLID design principles applied in Java
SOLID design principles applied in Java
 
SOLID Design Principles applied in Java
SOLID Design Principles applied in JavaSOLID Design Principles applied in Java
SOLID Design Principles applied in Java
 
Que hay de nuevo en Visual Studio 2013 y ASP.NET 5.1
Que hay de nuevo en Visual Studio 2013 y ASP.NET 5.1Que hay de nuevo en Visual Studio 2013 y ASP.NET 5.1
Que hay de nuevo en Visual Studio 2013 y ASP.NET 5.1
 
Errors detected in the Visual C++ 2012 libraries
Errors detected in the Visual C++ 2012 librariesErrors detected in the Visual C++ 2012 libraries
Errors detected in the Visual C++ 2012 libraries
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and Pindah
 
Design patterns with Kotlin
Design patterns with KotlinDesign patterns with Kotlin
Design patterns with Kotlin
 
Rcp by example
Rcp by exampleRcp by example
Rcp by example
 
HTML5 for the Silverlight Guy
HTML5 for the Silverlight GuyHTML5 for the Silverlight Guy
HTML5 for the Silverlight Guy
 
Griffon Presentation
Griffon PresentationGriffon Presentation
Griffon Presentation
 
Going web native
Going web nativeGoing web native
Going web native
 
Developing Native Mobile Apps Using JavaScript, ApacheCon NA 2014
Developing Native Mobile Apps Using JavaScript, ApacheCon NA 2014Developing Native Mobile Apps Using JavaScript, ApacheCon NA 2014
Developing Native Mobile Apps Using JavaScript, ApacheCon NA 2014
 

Último

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 

Último (20)

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

Anti- Object Oriented Design Patterns Reveal Problems

  • 1. Anti- Object Oriented Design Patterns Alvaro Polo DEVCON  2013  
  • 2. Design Patterns What’s a design pattern? “A   general   reusable   solu-on   to   a   commonly   occurring   problem   within   a   given   context   in   so8ware  design”,  Wikipedia   DEVCON  2013  
  • 3. Design Patterns But, what kind of commonly ocurring problem are we talking about? DEVCON  2013  
  • 4. Design Patterns No, this is all about software design DEVCON  2013  
  • 5. Problems unsolved by the language _uppercase: pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset %rbp, -16 movq %rsp, %rbp .cfi_def_cfa_register %rbp movq %rdi, -8(%rbp) movl %esi, -12(%rbp) movl $0, -16(%rbp) LBB0_1: movl -16(%rbp), %eax cmpl -12(%rbp), %eax jge LBB0_3 movslq -16(%rbp), %rax movq -8(%rbp), %rcx movb (%rcx,%rax), %dl movb %dl, -17(%rbp) movsbl -17(%rbp), %esi cmpl $97, %esi jl LBB0_2 DEVCON  2013   movsbl -17(%rbp), %eax cmpl $122, %eax jg LBB0_2 movsbl -17(%rbp), %eax addl $65, %eax subl $97, %eax movb %al, %cl movslq -16(%rbp), %rdx movq -8(%rbp), %rsi movb %cl, (%rsi,%rdx) LBB0_2: movl -16(%rbp), %eax addl $1, %eax movl %eax, -16(%rbp) jmp LBB0_1 LBB0_3: popq %rbp ret
  • 6. Problems unsolved by the language _uppercase: movsbl -17(%rbp), %eax pushq %rbp cmpl $122, %eax .cfi_def_cfa_offset 16 jg end_if .cfi_offset %rbp, -16uppercase(char *str, int strl) { movsbl -17(%rbp), %eax void movq %rsp, %rbp addl i++) { for (int i = 0; i < strl; $65, %eax .cfi_def_cfa_register %rbp char c = str[i]; subl $97, %eax movq %rdi, -8(%rbp) movb %al, if (c >= 'a' && c <= 'z') %cl movl %esi, -12(%rbp) -16(%rbp), %rdx str[i] = 'A' movslq 'a'; + c movl $0, -16(%rbp) } movq -8(%rbp), %rsi begin_for: movb %cl, (%rsi,%rdx) } movl -16(%rbp), %eax end_if: cmpl -12(%rbp), %eax movl -16(%rbp), %eax jge end_for addl $1, %eax movslq -16(%rbp), %rax movl %eax, -16(%rbp) movq -8(%rbp), %rcx jmp begin_for movb (%rcx,%rax), %dl end_for: movb %dl, -17(%rbp) popq %rbp movsbl -17(%rbp), %esi ret cmpl $97, %esi jl end_if DEVCON  2013  
  • 7. Problems caused by the language We want to represent IPv4 addresses in our Java program. Suggestions? class IpV4Address { private uint bits = 0; public static IpV4Address fromCidr(String cidr) { ... } public bool isInSameNetwork(IpV4Address addr, Netmask netmask) { ... } ... }; DEVCON  2013  
  • 8. Problems caused by the language We want to represent IPv4 addresses in our program. Suggestions? class IpV4Address { private uint bits = 0; public static IpV4Address fromCidr(String cidr) { ... } public bool isInSameNetwork(IpV4Address addr, Netmask netmask) { ... } ... }; DEVCON  2013  
  • 9. Problems caused by the language Let  me  introduce  the  An@-­‐  Object  Oriented  Design  PaGerns   DEVCON  2013  
  • 11. Singleton Pattern public class Application extends Configurable { private static Application instance; OOP establishes some constraints: ü Every object is bounded to a}lifecycle private Application() { ... ü Every classstaticas many instances as fit into has Application getInstance() { public if memory (instance == null) instance = new Application(); return instance; } // The functionality of the application goes here ... Singleton is fighting against these rules! } DEVCON  2013  
  • 12. Singleton Pattern What if we bend the rules? ... object Application extends Configurable { // The functionality of the application goes here ... } DEVCON  2013  
  • 14. Strategy and Co. interface Player { void attack(Player enemy); // Other stuff the player does ... } interface Weapon { void attack(Player enemy); } ü  Java is a pure-OOP programming language ü  Everything is modeled using objects class AbstractPlayer implements Player { private Weapon weapon; public setWeapon(Weapon weapon) { this.weapon = weapon; } public void attack(Player enemy) { weapon.attack(enemy); } } class Axe implements Weapon { ... } class Sword implements Weapon { ... } class Bow implements Weapon { ... } DEVCON  2013   Strategy is fighting against the lack of other mechanism to represent behavior!
  • 15. Strategy and Co. What if we have a non-pure OOP language? class player { public: typedef std::function<void(Player&)> weapon; void set_weapon(weapon& w) { _weapon = w; } void attack(player& enemy) { _weapon(enemy); } private: weapon _weapon; }; void axe(player& enemy) { ... } void sword(player& enemy) { ... } void bow(player& enemy) { ... } DEVCON  2013  
  • 16. Strategy and Co. The lack of functions is not a matter exclusive for strategy pattern ü  Observer pattern ü  Factory (method) pattern ü  Adapter pattern ü  … DEVCON  2013  
  • 18. Visitor interface CarElementVisitor { void visit(Wheel wheel); void visit(Engine engine); void visit(Body body); void visit(Car car); } interface CarElement { void accept(CarElementVisitor visitor); } class Wheel implements CarElement { public void accept(CarElementVisitor visitor) { visitor.visit(this); } } class Engine implements CarElement { public void accept(CarElementVisitor visitor) { visitor.visit(this); } } class Body implements CarElement { public void accept(CarElementVisitor visitor) { visitor.visit(this); } } DEVCON  2013   class Car implements CarElement { public void accept(CarElementVisitor visitor) { for(CarElement elem : elements) elem.accept(visitor); visitor.visit(this); } } class Mechanic implements CarElementVisitor { public void visit(Wheel wheel) { ... } public void visit(Engine engine) { ... } public void visit(Body body) { ... } public void visit(Car car) { ... } }
  • 19. Visitor Why not simply…? interface CarElementVisitor { void visit(Wheel wheel); void visit(Engine engine); void visit(Body body); void visit(Car car); } class Car implements CarElement { public void accept(CarElementVisitor visitor) { for(CarElement elem : elements) visitor.visit(elem); visitor.visit(this); } elem is a } CarElement, and visit(CarElement) DEVCON  2013   is not defined in visitor!
  • 20. Visitor Call visit(Wheel w) interface CarElement { void accept(CarElementVisitor visitor); } class Wheel implements CarElement { public void accept(CarElementVisitor visitor) { visitor.visit(this); } } class Engine implements CarElement { public void accept(CarElementVisitor visitor) { visitor.visit(this); } } visit(Engine Call class Body implements CarElement { public void accept(CarElementVisitor visitor) { visitor.visit(this); } } Call visit(Body b) DEVCON  2013   That’s why we need to redefine accept() again and again e)
  • 21. Visitor What if we dispatch the message based on… ü The receiver of the message, and… ü The arguments of the message? Let me introduce the double dispatch! Visitor pattern is there to provide double dispatch semantics not supported by the language DEVCON  2013  
  • 23. Conclusions ü  Design patterns are bad ü  NOOOOOOOOOOO!!!!!!! ü  Design patterns are useful, even when they fight against the language ü  We must learn from our errors and languages must evolve to avoid them And some opinions ü  OOP as implemented by Java is almost over ü  Learn Scala DEVCON  2013