SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Downloaden Sie, um offline zu lesen
Закон Деметры
Лекция 3
GRASP
General Responsibility Assignment Software Patterns
● Polymorphism
● Low Coupling
● High Cohesion
● …
● Information Expert
● Creator
● Controller
● Pure Fabrication
● ...
Polymorphism
ILamp
DaylightLamp
constructor(supported_colors:[color])
+get_supported_colors(): [color]
+set_intensity(int)
+get_intensity(): int
+set_color(string)
+get_color(): string
ColoredLamp
white
white, blue,
green, red
~ supported_colors_: [color]
Polymorphism
ILamp
DaylightLamp
+get_supported_colors(): [color]
+set_intensity(int)
+get_intensity(): int
+set_color(string)
+get_color(): string
ILamp
DaylightLamp
constructor(supported_colors:[color])
+get_supported_colors(): [color]
+set_intensity(int)
+get_intensity(): int
+set_color(string)
+get_color(): string
ColoredLamp
white
white, blue,
green, red
+get_supported_colors():
return [white]
~ supported_colors_: [color]
DaylightLamp
+get_supported_colors():
return [white, red, blue, green]
Low coupling, high cohesion
High coupling, low cohesion Low coupling, high cohesion
Law of Demeter
My vassal's vassal is not my vassal
(Вассал моего вассала - не мой вассал)
Law of Demeter
My vassal's vassal is not my vassal
(Вассал моего вассала - не мой вассал)
IRoom
constructor(max_lamps: dict)
+calculate_payment(): float
+add_lamp(lamp_type=daylight)
+get_lamps(): dict
+is_lamp_allowed(lamp_type): bool
~ max_lamps_: dict(lamp_type, int)
~ lamps_: dict(lamp_type, ILamp)
IFloor
constructor(rooms: [IRoom])
+get_rooms(): [IRoom]
~ rooms_: [IRoom]
ILamp
constructor(supported_colors:[color])
+get_supported_colors(): [color]
+set_intensity(int)
+get_intensity(): int
+set_color(string)
+get_color(): string
~ supported_colors_: [color]
IFloorLampPolicy
+evaluate(is_day: bool)
+switch_all(color: color, intensity: int)
+get_floor(): IFloor
+set_floor(floor: IFloor)
~ floor_: IFloor
Law of Demeter
Law of Demeter
IFloorLampPolicy
+evaluate(is_day: bool)
+get_floor(): IFloor
+set_floor(floor: IFloor)
~ floor_: IFloor
class IFloorLampPolicy:
def evaluate(self, is_day: bool):
pass
class CurfewPolicy(IFloorLampPolicy):
def evaluate(self, is_day: bool):
if is_day:
return
# Turn all light off in the floor
for room in self.get_floor().get_rooms():
for _, lamps in room.get_lamps().items():
for lamp in lamps:
lamp.set_color(white)
lamp.set_intensity(0)
CurfewPolicy
Law of Demeter
IFloorLampPolicy
+evaluate(is_day: bool)
+get_floor(): IFloor
+set_floor(floor: IFloor)
~ floor_: IFloor
class IFloorLampPolicy:
def evaluate(self, is_day: bool):
pass
class CurfewPolicy(IFloorLampPolicy):
def evaluate(self, is_day: bool):
if is_day:
return
# Turn all light off in the floor
for room in self.get_floor().get_rooms():
for _, lamps in room.get_lamps().items():
for lamp in lamps:
lamp.set_color(white)
lamp.set_intensity(0)
CurfewPolicy
Law of Demeter violation (High Coupling):
get_floor() -> get_rooms() -> get_lamps()
Law of Demeter violation
IFloorLampPolicy
+get_floor(): IFloor
~ floor_: IFloor
IRoom
+get_lamps(): dict
~ lamps_: dict(lamp_type, ILamp)
IFloor
+get_rooms(): [IRoom]
~ rooms_: [IRoom]
ILamp
Law of Demeter violation problems
IFloor
constructor(rooms: [IRoom])
+get_rooms(): [IRoom]
~ rooms_: [IRoom]
IFloor
constructor(rooms: [IRoom], corridor: IRoom)
+get_rooms(): [IRoom]
+get_corridor(): IToom
~ rooms_: [IRoom]
~ corridor_: IRoom
class IFloorLampPolicy:
def evaluate(self, is_day: bool):
pass
class CurfewPolicy(IFloorLampPolicy):
def evaluate(self, is_day: bool):
if is_day:
return
# Turn all light off in the floor
for room in self.get_floor().get_rooms():
for _, lamps in room.get_lamps().items():
for lamp in lamps:
lamp.set_color(white)
lamp.set_intensity(0)
Law of Demeter violation problems
IFloor
constructor(rooms: [IRoom])
+get_rooms(): [IRoom]
~ rooms_: [IRoom]
IFloor
constructor(rooms: [IRoom], corridor: IRoom)
+get_rooms(): [IRoom]
+get_corridor(): IToom
~ rooms_: [IRoom]
~ corridor_: IRoom
class IFloorLampPolicy:
def evaluate(self, is_day: bool):
pass
class CurfewPolicy(IFloorLampPolicy):
def evaluate(self, is_day: bool):
if is_day:
return
# Turn all light off in the floor
for room in self.get_floor().get_rooms():
for _, lamps in room.get_lamps().items():
for lamp in lamps:
lamp.set_color(white)
lamp.set_intensity(0)
Avoiding Law of Demeter violation
IRoom
+get_lamps(): dict
+apply_action(action: LampAction)
~ lamps_: dict(lamp_type, ILamp)
IFloor
+get_rooms(): [IRoom]
+apply_action(action: LampAction)
~ rooms_: [IRoom]
ILamp
IFloorLampPolicy
+get_floor(): IFloor
~ floor_: IFloor
ILampAction
+evaluate(lamp: ILamp)
LampOff
Avoiding Law of Demeter violation
class IRoom:
def apply_action(self, action: ILampAction):
for lamp_type, lamps in self.lamps_.items():
for lamp in lamps:
action.evaluate(lamp)
class IFloor:
def apply_action(self, action: ILampAction):
for room in self.rooms_:
room.apply_action(action)
self.corridor_.apply_action(action)
class CurfewPolicy(IFloorLampPolicy):
def evaluate(self, is_day: bool):
if is_day:
return
floor = self.get_floor()
floor.apply_action(LampOff())
class ILampAction:
def evaluate(self, lamp: ILamp):
pass
class LampOff(ILampAction):
def evaluate(self, lamp: ILamp):
lamp.set_color(white)
lamp.set_intensity(0)
class LampOn(ILampAction):
def evaluate(self, lamp: ILamp):
lamp.set_intensity(100)
class RedLampOff(ILampAction):
def evaluate(self, lamp: ILamp):
if lamp.get_color() == red:
lamp.set_intensity(0)
Avoiding Law of Demeter violation
IRoom
+get_lamps(): dict
+apply_action(action: ILampAction)
~ lamps_: dict(lamp_type, ILamp)
IFloor
+get_rooms(): [IRoom]
+apply_action(action: IRoomAction)
~ rooms_: [IRoom]
ILampILampAction
+evaluate(lamp: ILamp)
IRoomAction
+evaluate(room: IRoom)
IFloorLampPolicy
+get_floor(): IFloor
~ floor_: IFloor
The end

Weitere ähnliche Inhalte

Was ist angesagt?

What Shazam doesn't want you to know
What Shazam doesn't want you to knowWhat Shazam doesn't want you to know
What Shazam doesn't want you to knowRoy van Rijn
 
Python: The Iterator Pattern (Comprehensions)
Python: The Iterator Pattern (Comprehensions)Python: The Iterator Pattern (Comprehensions)
Python: The Iterator Pattern (Comprehensions)Damian T. Gordon
 
Data made out of functions
Data made out of functionsData made out of functions
Data made out of functionskenbot
 
Functional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with PythonFunctional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with PythonCarlos V.
 
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersKimikazu Kato
 
Beyond tf idf why, what & how
Beyond tf idf why, what & howBeyond tf idf why, what & how
Beyond tf idf why, what & howlucenerevolution
 
Python Training Tutorial for Frreshers
Python Training Tutorial for FrreshersPython Training Tutorial for Frreshers
Python Training Tutorial for Frreshersrajkamaltibacademy
 
String in python use of split method
String in python use of split methodString in python use of split method
String in python use of split methodvikram mahendra
 
Python language data types
Python language data typesPython language data types
Python language data typesHoang Nguyen
 
Introduction to Python and TensorFlow
Introduction to Python and TensorFlowIntroduction to Python and TensorFlow
Introduction to Python and TensorFlowBayu Aldi Yansyah
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Pedro Rodrigues
 
Monoids, monoids, monoids
Monoids, monoids, monoidsMonoids, monoids, monoids
Monoids, monoids, monoidsLuka Jacobowitz
 
Pythonbrasil - 2018 - Acelerando Soluções com GPU
Pythonbrasil - 2018 - Acelerando Soluções com GPUPythonbrasil - 2018 - Acelerando Soluções com GPU
Pythonbrasil - 2018 - Acelerando Soluções com GPUPaulo Sergio Lemes Queiroz
 
Python Training v2
Python Training v2Python Training v2
Python Training v2ibaydan
 

Was ist angesagt? (20)

What Shazam doesn't want you to know
What Shazam doesn't want you to knowWhat Shazam doesn't want you to know
What Shazam doesn't want you to know
 
Python: The Iterator Pattern (Comprehensions)
Python: The Iterator Pattern (Comprehensions)Python: The Iterator Pattern (Comprehensions)
Python: The Iterator Pattern (Comprehensions)
 
Data made out of functions
Data made out of functionsData made out of functions
Data made out of functions
 
Python Tidbits
Python TidbitsPython Tidbits
Python Tidbits
 
Code Generation
Code GenerationCode Generation
Code Generation
 
Functional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with PythonFunctional Programming inside OOP? It’s possible with Python
Functional Programming inside OOP? It’s possible with Python
 
Session1
Session1Session1
Session1
 
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning Programmers
 
Beyond tf idf why, what & how
Beyond tf idf why, what & howBeyond tf idf why, what & how
Beyond tf idf why, what & how
 
Python Training Tutorial for Frreshers
Python Training Tutorial for FrreshersPython Training Tutorial for Frreshers
Python Training Tutorial for Frreshers
 
Erlang
ErlangErlang
Erlang
 
String in python use of split method
String in python use of split methodString in python use of split method
String in python use of split method
 
Py3k
Py3kPy3k
Py3k
 
NumPy/SciPy Statistics
NumPy/SciPy StatisticsNumPy/SciPy Statistics
NumPy/SciPy Statistics
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Introduction to Python and TensorFlow
Introduction to Python and TensorFlowIntroduction to Python and TensorFlow
Introduction to Python and TensorFlow
 
Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)Introduction to the basics of Python programming (part 1)
Introduction to the basics of Python programming (part 1)
 
Monoids, monoids, monoids
Monoids, monoids, monoidsMonoids, monoids, monoids
Monoids, monoids, monoids
 
Pythonbrasil - 2018 - Acelerando Soluções com GPU
Pythonbrasil - 2018 - Acelerando Soluções com GPUPythonbrasil - 2018 - Acelerando Soluções com GPU
Pythonbrasil - 2018 - Acelerando Soluções com GPU
 
Python Training v2
Python Training v2Python Training v2
Python Training v2
 

Ähnlich wie GRASP Patterns and Law of Demeter Explained

Object Orientation vs. Functional Programming in Python
Object Orientation vs. Functional Programming in PythonObject Orientation vs. Functional Programming in Python
Object Orientation vs. Functional Programming in PythonPython Ireland
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Qiangning Hong
 
Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonAnoop Thomas Mathew
 
Zope component architechture
Zope component architechtureZope component architechture
Zope component architechtureAnatoly Bubenkov
 
The Ring programming language version 1.5 book - Part 5 of 31
The Ring programming language version 1.5 book - Part 5 of 31The Ring programming language version 1.5 book - Part 5 of 31
The Ring programming language version 1.5 book - Part 5 of 31Mahmoud Samir Fayed
 
R. herves. clean code (theme)2
R. herves. clean code (theme)2R. herves. clean code (theme)2
R. herves. clean code (theme)2saber tabatabaee
 
The Ring programming language version 1.3 book - Part 18 of 88
The Ring programming language version 1.3 book - Part 18 of 88The Ring programming language version 1.3 book - Part 18 of 88
The Ring programming language version 1.3 book - Part 18 of 88Mahmoud Samir Fayed
 
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...Yashpatel821746
 
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...Yashpatel821746
 
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...Yashpatel821746
 
4Developers 2018: An Arma 3 mod success story - Creating a new game mode for ...
4Developers 2018: An Arma 3 mod success story - Creating a new game mode for ...4Developers 2018: An Arma 3 mod success story - Creating a new game mode for ...
4Developers 2018: An Arma 3 mod success story - Creating a new game mode for ...PROIDEA
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...DRVaibhavmeshram1
 
python beginner talk slide
python beginner talk slidepython beginner talk slide
python beginner talk slidejonycse
 
The Ring programming language version 1.5.1 book - Part 174 of 180
The Ring programming language version 1.5.1 book - Part 174 of 180 The Ring programming language version 1.5.1 book - Part 174 of 180
The Ring programming language version 1.5.1 book - Part 174 of 180 Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210Mahmoud Samir Fayed
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingHock Leng PUAH
 

Ähnlich wie GRASP Patterns and Law of Demeter Explained (20)

Object Orientation vs. Functional Programming in Python
Object Orientation vs. Functional Programming in PythonObject Orientation vs. Functional Programming in Python
Object Orientation vs. Functional Programming in Python
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010
 
Functional python
Functional pythonFunctional python
Functional python
 
Python Part 1
Python Part 1Python Part 1
Python Part 1
 
Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in Python
 
Zope component architechture
Zope component architechtureZope component architechture
Zope component architechture
 
The Ring programming language version 1.5 book - Part 5 of 31
The Ring programming language version 1.5 book - Part 5 of 31The Ring programming language version 1.5 book - Part 5 of 31
The Ring programming language version 1.5 book - Part 5 of 31
 
R. herves. clean code (theme)2
R. herves. clean code (theme)2R. herves. clean code (theme)2
R. herves. clean code (theme)2
 
The Ring programming language version 1.3 book - Part 18 of 88
The Ring programming language version 1.3 book - Part 18 of 88The Ring programming language version 1.3 book - Part 18 of 88
The Ring programming language version 1.3 book - Part 18 of 88
 
C3 w2
C3 w2C3 w2
C3 w2
 
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
8799.pdfOr else the work is fine only. Lot to learn buddy.... Improve your ba...
 
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
Or else the work is fine only. Lot to learn buddy.... Improve your basics in ...
 
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
PYTHONOr else the work is fine only. Lot to learn buddy.... Improve your basi...
 
4Developers 2018: An Arma 3 mod success story - Creating a new game mode for ...
4Developers 2018: An Arma 3 mod success story - Creating a new game mode for ...4Developers 2018: An Arma 3 mod success story - Creating a new game mode for ...
4Developers 2018: An Arma 3 mod success story - Creating a new game mode for ...
 
Porting to Python 3
Porting to Python 3Porting to Python 3
Porting to Python 3
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
python beginner talk slide
python beginner talk slidepython beginner talk slide
python beginner talk slide
 
The Ring programming language version 1.5.1 book - Part 174 of 180
The Ring programming language version 1.5.1 book - Part 174 of 180 The Ring programming language version 1.5.1 book - Part 174 of 180
The Ring programming language version 1.5.1 book - Part 174 of 180
 
The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem Solving
 

Mehr von Alexander Granin

Concurrent applications with free monads and stm
Concurrent applications with free monads and stmConcurrent applications with free monads and stm
Concurrent applications with free monads and stmAlexander Granin
 
Hierarchical free monads and software design in fp
Hierarchical free monads and software design in fpHierarchical free monads and software design in fp
Hierarchical free monads and software design in fpAlexander Granin
 
Final tagless vs free monad
Final tagless vs free monadFinal tagless vs free monad
Final tagless vs free monadAlexander Granin
 
The present and the future of functional programming in c++
The present and the future of functional programming in c++The present and the future of functional programming in c++
The present and the future of functional programming in c++Alexander Granin
 
О разработке десктопных приложений / About desktop development
О разработке десктопных приложений / About desktop developmentО разработке десктопных приложений / About desktop development
О разработке десктопных приложений / About desktop developmentAlexander Granin
 
Принципы и практики разработки ПО 2 / Principles and practices of software de...
Принципы и практики разработки ПО 2 / Principles and practices of software de...Принципы и практики разработки ПО 2 / Principles and practices of software de...
Принципы и практики разработки ПО 2 / Principles and practices of software de...Alexander Granin
 
Принципы и практики разработки ПО / Principles and practices of software deve...
Принципы и практики разработки ПО / Principles and practices of software deve...Принципы и практики разработки ПО / Principles and practices of software deve...
Принципы и практики разработки ПО / Principles and practices of software deve...Alexander Granin
 
Design of big applications in FP
Design of big applications in FPDesign of big applications in FP
Design of big applications in FPAlexander Granin
 
GitHub - зеркало разработчика
GitHub - зеркало разработчикаGitHub - зеркало разработчика
GitHub - зеркало разработчикаAlexander Granin
 
The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++Alexander Granin
 
Functional programming in C++ LambdaNsk
Functional programming in C++ LambdaNskFunctional programming in C++ LambdaNsk
Functional programming in C++ LambdaNskAlexander Granin
 
Transition graph using free monads and existentials
Transition graph using free monads and existentialsTransition graph using free monads and existentials
Transition graph using free monads and existentialsAlexander Granin
 
Software transactional memory. pure functional approach
Software transactional memory. pure functional approachSoftware transactional memory. pure functional approach
Software transactional memory. pure functional approachAlexander Granin
 
Вы не понимаете ФП / You don't understand FP
Вы не понимаете ФП / You don't understand FPВы не понимаете ФП / You don't understand FP
Вы не понимаете ФП / You don't understand FPAlexander Granin
 
Functional "Life": parallel cellular automata and comonads
Functional "Life": parallel cellular automata and comonadsFunctional "Life": parallel cellular automata and comonads
Functional "Life": parallel cellular automata and comonadsAlexander Granin
 
Functional microscope - Lenses in C++
Functional microscope - Lenses in C++Functional microscope - Lenses in C++
Functional microscope - Lenses in C++Alexander Granin
 
Дизайн больших приложений в ФП
Дизайн больших приложений в ФПДизайн больших приложений в ФП
Дизайн больших приложений в ФПAlexander Granin
 
Линзы - комбинаторная манипуляция данными
Линзы - комбинаторная манипуляция даннымиЛинзы - комбинаторная манипуляция данными
Линзы - комбинаторная манипуляция даннымиAlexander Granin
 
Линзы - комбинаторная манипуляция данными (Dev2Dev)
Линзы - комбинаторная манипуляция данными (Dev2Dev)Линзы - комбинаторная манипуляция данными (Dev2Dev)
Линзы - комбинаторная манипуляция данными (Dev2Dev)Alexander Granin
 

Mehr von Alexander Granin (20)

Concurrent applications with free monads and stm
Concurrent applications with free monads and stmConcurrent applications with free monads and stm
Concurrent applications with free monads and stm
 
Hierarchical free monads and software design in fp
Hierarchical free monads and software design in fpHierarchical free monads and software design in fp
Hierarchical free monads and software design in fp
 
Final tagless vs free monad
Final tagless vs free monadFinal tagless vs free monad
Final tagless vs free monad
 
Monadic parsers in C++
Monadic parsers in C++Monadic parsers in C++
Monadic parsers in C++
 
The present and the future of functional programming in c++
The present and the future of functional programming in c++The present and the future of functional programming in c++
The present and the future of functional programming in c++
 
О разработке десктопных приложений / About desktop development
О разработке десктопных приложений / About desktop developmentО разработке десктопных приложений / About desktop development
О разработке десктопных приложений / About desktop development
 
Принципы и практики разработки ПО 2 / Principles and practices of software de...
Принципы и практики разработки ПО 2 / Principles and practices of software de...Принципы и практики разработки ПО 2 / Principles and practices of software de...
Принципы и практики разработки ПО 2 / Principles and practices of software de...
 
Принципы и практики разработки ПО / Principles and practices of software deve...
Принципы и практики разработки ПО / Principles and practices of software deve...Принципы и практики разработки ПО / Principles and practices of software deve...
Принципы и практики разработки ПО / Principles and practices of software deve...
 
Design of big applications in FP
Design of big applications in FPDesign of big applications in FP
Design of big applications in FP
 
GitHub - зеркало разработчика
GitHub - зеркало разработчикаGitHub - зеркало разработчика
GitHub - зеркало разработчика
 
The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++The Present and The Future of Functional Programming in C++
The Present and The Future of Functional Programming in C++
 
Functional programming in C++ LambdaNsk
Functional programming in C++ LambdaNskFunctional programming in C++ LambdaNsk
Functional programming in C++ LambdaNsk
 
Transition graph using free monads and existentials
Transition graph using free monads and existentialsTransition graph using free monads and existentials
Transition graph using free monads and existentials
 
Software transactional memory. pure functional approach
Software transactional memory. pure functional approachSoftware transactional memory. pure functional approach
Software transactional memory. pure functional approach
 
Вы не понимаете ФП / You don't understand FP
Вы не понимаете ФП / You don't understand FPВы не понимаете ФП / You don't understand FP
Вы не понимаете ФП / You don't understand FP
 
Functional "Life": parallel cellular automata and comonads
Functional "Life": parallel cellular automata and comonadsFunctional "Life": parallel cellular automata and comonads
Functional "Life": parallel cellular automata and comonads
 
Functional microscope - Lenses in C++
Functional microscope - Lenses in C++Functional microscope - Lenses in C++
Functional microscope - Lenses in C++
 
Дизайн больших приложений в ФП
Дизайн больших приложений в ФПДизайн больших приложений в ФП
Дизайн больших приложений в ФП
 
Линзы - комбинаторная манипуляция данными
Линзы - комбинаторная манипуляция даннымиЛинзы - комбинаторная манипуляция данными
Линзы - комбинаторная манипуляция данными
 
Линзы - комбинаторная манипуляция данными (Dev2Dev)
Линзы - комбинаторная манипуляция данными (Dev2Dev)Линзы - комбинаторная манипуляция данными (Dev2Dev)
Линзы - комбинаторная манипуляция данными (Dev2Dev)
 

Kürzlich hochgeladen

🐬 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
 
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
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
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
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
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
 
#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
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

Kürzlich hochgeladen (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
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
 
#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
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
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
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

GRASP Patterns and Law of Demeter Explained

  • 2. GRASP General Responsibility Assignment Software Patterns ● Polymorphism ● Low Coupling ● High Cohesion ● … ● Information Expert ● Creator ● Controller ● Pure Fabrication ● ...
  • 4. Polymorphism ILamp DaylightLamp +get_supported_colors(): [color] +set_intensity(int) +get_intensity(): int +set_color(string) +get_color(): string ILamp DaylightLamp constructor(supported_colors:[color]) +get_supported_colors(): [color] +set_intensity(int) +get_intensity(): int +set_color(string) +get_color(): string ColoredLamp white white, blue, green, red +get_supported_colors(): return [white] ~ supported_colors_: [color] DaylightLamp +get_supported_colors(): return [white, red, blue, green]
  • 5. Low coupling, high cohesion High coupling, low cohesion Low coupling, high cohesion
  • 6. Law of Demeter My vassal's vassal is not my vassal (Вассал моего вассала - не мой вассал)
  • 7. Law of Demeter My vassal's vassal is not my vassal (Вассал моего вассала - не мой вассал)
  • 8. IRoom constructor(max_lamps: dict) +calculate_payment(): float +add_lamp(lamp_type=daylight) +get_lamps(): dict +is_lamp_allowed(lamp_type): bool ~ max_lamps_: dict(lamp_type, int) ~ lamps_: dict(lamp_type, ILamp) IFloor constructor(rooms: [IRoom]) +get_rooms(): [IRoom] ~ rooms_: [IRoom] ILamp constructor(supported_colors:[color]) +get_supported_colors(): [color] +set_intensity(int) +get_intensity(): int +set_color(string) +get_color(): string ~ supported_colors_: [color] IFloorLampPolicy +evaluate(is_day: bool) +switch_all(color: color, intensity: int) +get_floor(): IFloor +set_floor(floor: IFloor) ~ floor_: IFloor Law of Demeter
  • 9. Law of Demeter IFloorLampPolicy +evaluate(is_day: bool) +get_floor(): IFloor +set_floor(floor: IFloor) ~ floor_: IFloor class IFloorLampPolicy: def evaluate(self, is_day: bool): pass class CurfewPolicy(IFloorLampPolicy): def evaluate(self, is_day: bool): if is_day: return # Turn all light off in the floor for room in self.get_floor().get_rooms(): for _, lamps in room.get_lamps().items(): for lamp in lamps: lamp.set_color(white) lamp.set_intensity(0) CurfewPolicy
  • 10. Law of Demeter IFloorLampPolicy +evaluate(is_day: bool) +get_floor(): IFloor +set_floor(floor: IFloor) ~ floor_: IFloor class IFloorLampPolicy: def evaluate(self, is_day: bool): pass class CurfewPolicy(IFloorLampPolicy): def evaluate(self, is_day: bool): if is_day: return # Turn all light off in the floor for room in self.get_floor().get_rooms(): for _, lamps in room.get_lamps().items(): for lamp in lamps: lamp.set_color(white) lamp.set_intensity(0) CurfewPolicy Law of Demeter violation (High Coupling): get_floor() -> get_rooms() -> get_lamps()
  • 11. Law of Demeter violation IFloorLampPolicy +get_floor(): IFloor ~ floor_: IFloor IRoom +get_lamps(): dict ~ lamps_: dict(lamp_type, ILamp) IFloor +get_rooms(): [IRoom] ~ rooms_: [IRoom] ILamp
  • 12. Law of Demeter violation problems IFloor constructor(rooms: [IRoom]) +get_rooms(): [IRoom] ~ rooms_: [IRoom] IFloor constructor(rooms: [IRoom], corridor: IRoom) +get_rooms(): [IRoom] +get_corridor(): IToom ~ rooms_: [IRoom] ~ corridor_: IRoom class IFloorLampPolicy: def evaluate(self, is_day: bool): pass class CurfewPolicy(IFloorLampPolicy): def evaluate(self, is_day: bool): if is_day: return # Turn all light off in the floor for room in self.get_floor().get_rooms(): for _, lamps in room.get_lamps().items(): for lamp in lamps: lamp.set_color(white) lamp.set_intensity(0)
  • 13. Law of Demeter violation problems IFloor constructor(rooms: [IRoom]) +get_rooms(): [IRoom] ~ rooms_: [IRoom] IFloor constructor(rooms: [IRoom], corridor: IRoom) +get_rooms(): [IRoom] +get_corridor(): IToom ~ rooms_: [IRoom] ~ corridor_: IRoom class IFloorLampPolicy: def evaluate(self, is_day: bool): pass class CurfewPolicy(IFloorLampPolicy): def evaluate(self, is_day: bool): if is_day: return # Turn all light off in the floor for room in self.get_floor().get_rooms(): for _, lamps in room.get_lamps().items(): for lamp in lamps: lamp.set_color(white) lamp.set_intensity(0)
  • 14. Avoiding Law of Demeter violation IRoom +get_lamps(): dict +apply_action(action: LampAction) ~ lamps_: dict(lamp_type, ILamp) IFloor +get_rooms(): [IRoom] +apply_action(action: LampAction) ~ rooms_: [IRoom] ILamp IFloorLampPolicy +get_floor(): IFloor ~ floor_: IFloor ILampAction +evaluate(lamp: ILamp) LampOff
  • 15. Avoiding Law of Demeter violation class IRoom: def apply_action(self, action: ILampAction): for lamp_type, lamps in self.lamps_.items(): for lamp in lamps: action.evaluate(lamp) class IFloor: def apply_action(self, action: ILampAction): for room in self.rooms_: room.apply_action(action) self.corridor_.apply_action(action) class CurfewPolicy(IFloorLampPolicy): def evaluate(self, is_day: bool): if is_day: return floor = self.get_floor() floor.apply_action(LampOff()) class ILampAction: def evaluate(self, lamp: ILamp): pass class LampOff(ILampAction): def evaluate(self, lamp: ILamp): lamp.set_color(white) lamp.set_intensity(0) class LampOn(ILampAction): def evaluate(self, lamp: ILamp): lamp.set_intensity(100) class RedLampOff(ILampAction): def evaluate(self, lamp: ILamp): if lamp.get_color() == red: lamp.set_intensity(0)
  • 16. Avoiding Law of Demeter violation IRoom +get_lamps(): dict +apply_action(action: ILampAction) ~ lamps_: dict(lamp_type, ILamp) IFloor +get_rooms(): [IRoom] +apply_action(action: IRoomAction) ~ rooms_: [IRoom] ILampILampAction +evaluate(lamp: ILamp) IRoomAction +evaluate(room: IRoom) IFloorLampPolicy +get_floor(): IFloor ~ floor_: IFloor