Code Design
Code Design
• Concurrency design
https://forms.gle/nKn4mG1bVVX1U16x8
Review of code
class is having multiple resp like managing game, game logic. We can have seperate classes
Yes. There should be more classes;
Collecting user input
Run game logic
Store board state
Print board state
have constants
Remove multiple nested if else
cannot be extended if we want to play 4X4
code duplications for checking wining lines and can improve redability
Duplicate code in printBoard
Good
• SRP (***) • Prefer composition over
inheritance
• Low coupling (***)
• Boundary control entity (*)
• Unit testability
• LSP
• YAGNI
• ISP • KISS
• DRY • DDD
• • Aggregates
Bad
• Type check • Duplicate code
• Flag check
• Dead code
Data Task
Parallelism Parallelism
L1 L1 L1 L2 L1 L3
Dx Dy Dz
Concurrency
If
Supported
Blocking Non Blocking
I/O I/O
98% 2%
Data Task Thread
Thread Pool
Parallelism Parallelism
• Suspend
• Sleep
• SetThreadPriority
•
Size **
Functional
Interface
Function
Object
If Flag
== Logic changes
In each Path
if(error == true) if(salary > 5000)
Functional
Interface
Function
Interface/ Duck
Object
Refactoring ***
Rule Engine
Dual/ Multi Single
Dispatch Dispatch
One class
Function
Object for each change Interface/ Duck
Object
If If
{ {
Type changes Value changes
} }
If If
{ {
Type changes Value changes
} }
If If
{ {
Type changes Value changes
} }
1 2 3
Emp obj
Examples of Emp obj
… new Emp()
coupling …
obj.fun();
delete obj;
Abstraction
Approach for # Interface typing * # DI * # smart pointers
Low coupling # Duck typing # factory # virtual destructor
# Lamda
# wrapper
# Garbage collector
Xtreme Approach # reflection # reflection
Architecture
vs
Design
Architecture [Design]
vs
[Code] Design
Quality Approach
• Caching
• Performance
• Indexing
• Security
• Concurrency
• Maintainability
• Pooling
• Reliability
• Data Virtualization
• Availability
• Lazy Loading
• Robustness
• Reusability
• …
• Extensible
1 5
Domain
Understands
Architecture
Understands
Understand Create
Requirements Code Skeleton
For code (Design)
Knows
Code Design
Technology Low level design
Domain Module Design
proc, OO, fun Class Design
OO Design patterns Implementation Design
Fun Design patterns
Design Principles
Design Anti patterns
Idioms
fun(Bird bird){
Interface Bird{
fly ….
sing
buildNest }
}
fun(Bird bird){
if(….)
bird.fly(); Class Parrot{
…. Interface Bird{
}
}
}
interface LivingThing{
…
}
…
layEggs()
swim()
}
DataHandler
WinHandler MainframeHan
JavaHandler
dler
WinHandler MainframeHan
JavaHandler
dler
a+b 3 cpu cycles
P1 classA
Invoice Kst
O CO
Tax
P2 P3 classC
Kst
classB O
Invoice C Tax
O Tax
P4 P5 P6
Cst
classD classE Gst
O
O Tax
Proc OO Functional
Learning Curve ++ -- -
Development Effort ++ -- -
Less Coupling -- ++ ++
Concurrency -- -- ++
Functional Prog OO Prog
(Lego) (Lego)
C F
C F
F
C
F CO
O
C O C
F
C
F O
C C
F O
C
F
O
Interface typing (java, c++, C#) Duck typing (py, js)
Tight coupling
Compiled Languages Dynamic Languages
interface Bird{
void fly();
class Parrot }
{
class Parrot{
void fly(){ class Parrot implements Bird
void fly(){
… {
…
} void fly(){
}
} …
}
}
}
interface Bird{
void f1();
}
class Parrot
{
class Parrot implements class Parrot{ class Parrot{
void fly(){
Bird void f1(){ void fly(){
…
{ … …
}
void f1(){ } }
}
… } }
}
}
interface Bird{
void f1();
}
class Parrot
class CA{
{
class Parrot class Parrot{ class Parrot{ void f1(){
void fly(){
implements Bird void f1(){ void fly(){ …
…
{ … … }
}
void f1(){ } } }
}
… } }
}
}
Logic classes
<<entity/domain>>
M A B
Single dispatch Class A
Java, c++, py, ..
Obj1 .fun() Class B
Class C
Dual dispatch
Multi dispatch
Domain/Entity classes
(logic)
Product
Control
Domain/Entity classes
(logic)
Product
Boundary classes
Control classes
Workflow classes
Entity classes
Domain classes
Ag1
Ag1 Ag2
Step flow1 start
action1
branch1
action3 action2
stop
DDD
Aggregate
Aggregate
Aggregate
Aggregate
Module
Aggregate
B
A
C Root
DDD
Boundary class
Api (flexible)
Ui
Message
Timer
Domain class
(temple)
Boundary Control Entity
Boundary
Facade
Flow
Domain
Action
• Boundary
• Control
Agg • Entity
Agg
Order Item Root Root
… Agg
Root
Code segment Data segment Heap Stack
Util vtbl
0 : CA - 1, CC
1 : CB - 2,
2 : CC - 3 a
Util
vptr U
?
Runtime u.f(a)
u.vptr.vtbl[0](a)
Compile time
Code segment Data segment Heap Stack
CX vtbl[3]
0 : CA - 1,
void f(CA a) {} //1 CC
1 : CB - 2,
void f(CB b) {} //2
void f(CC c) {} //3 2 : CC - 3 a
CY vtbl[3]
void f(CA a) {} //4
void f(CB b) {} //5 0 : CA - 4,
void f(CC c) {} //6 1 : CB - 5,
2 : CC - 6
void f(CA a) {} //7 CZ
void f(CB b) {} //8
void f(CC c) {} //9
CZ vtbl[3] vptr x
0 : CA - 7,
1 : CB - 8, x.f(a)
2 : CC - 9
x.vptr.vtbl[0]
Compile time
Code segment Data segment Heap Stack
SA::Create() SA vtbl[1]
0
CA::Create()
CA vtbl[1]
0 ?
Runtime
CA
vptr account
account.Create()
account.vptr.vtbl[0]
Compile time
UI
Domain
Data Layer