* OOP :PROPERTIES: :ANKI_DECK: soft-eng::oop :END: ** What are the differences between =abstract class= and =interface= ? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_DECK: soft-eng::oop :ANKI_NOTE_ID: 1765335347931 :ANKI_NOTE_HASH: 30e4d60e7a9c538f74a21c49951381f1 :END: interfaces cannot have constructors; all their methods _must_ be abstract, and they can implement multiple interfaces. meanwhile, *abstract classes* are limited to implementing a single interface. ** What is *polymorphism*? :PROPERTIES: :ANKI_NOTE_TYPE: Pretty with Example :ANKI_NOTE_ID: 1765335347934 :ANKI_NOTE_HASH: d6429295b5db622c08c281ea14a3fec3 :END: Polymorphism (“many forms”) is the ability to treat different concrete objects as instances of a common type, while allowing each object to exhibit different behavior when the same operation is invoked. It occurs whenever distinct objects are accessed through the same interface or base type. Main forms: 1. Compile-time polymorphism: method overloading (resolved at compile time) 2. Run-time polymorphism: subtyping / method overriding (resolved via dynamic dispatch) *** Example - =Shape= reference pointing to =Circle= or =Rectangle= - Calling =draw()= invokes the implementation specific to the actual object ** What are creational patterns about? What about structural and behavioural? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347945 :ANKI_NOTE_HASH: 36fe2b9e08d175ddf29dca962a8de050 :END: creational deal with creating suitable objects. structural helps in designing a good way to realise relationships amongst entities. behavioural is about identifying common communication patterns among objects and increase this flexibility ** What does the Visitor Pattern do? :PROPERTIES: :ANKI_NOTE_TYPE: Pretty with Example :ANKI_NOTE_ID: 1765335347947 :ANKI_NOTE_HASH: 875d534be091a39e70c2186b07368695 :END: Separates algorithms from the object structure they operate on by placing the behaviour in a separate *visitor* class. Objects expose an =accept(visitor)= method, enabling new operations without modifying their classes. *** Example - Element interface: =Shape= with method =accept(Visitor v)= - Concrete elements: =Circle=, =Rectangle= - Visitor interface: =Visitor= with methods =visit(Circle)= and =visit(Rectangle)= - Concrete visitor: =AreaVisitor= computes the area of each shape ** Which Design Pattern best describes "Subclasses decide which concrete classes to create" :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347950 :ANKI_NOTE_HASH: d34b27063d7a2c49adfd84ecf6af5107 :END: Factory Method ** Which design pattern describes the following: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_PREPEND_HEADING: t :ANKI_NOTE_ID: 1765335347952 :ANKI_NOTE_HASH: 56053d9c8a5e1817c4c7ebbe0feca641 :END: "Subclasses decide how to implement steps in an algorithm" *** Back Template ** What is the Adapter Pattern? :PROPERTIES: :ANKI_NOTE_TYPE: Pretty with Example :ANKI_NOTE_ID: 1765335347954 :ANKI_NOTE_HASH: c15e7f3ba9ed695a6ede5a459bdf7ba9 :END: Acts as a wrapper that translates one interface into another without changing the adapted class. *** Example - Client expects interface =Target= - Existing class provides interface =Adaptee= - =Adapter= implements =Target= and internally delegates calls to =Adaptee= ** List the Creational design patterns: :PROPERTIES: :ANKI_NOTE_TYPE: Cloze :ANKI_NOTE_ID: 1766996953504 :ANKI_NOTE_HASH: 994170765743d0356ce20343223c7246 :ANKI_PREPEND_HEADING: t :END: *** Text List the Creational design patterns: - {{c1::Factory Method}} - {{c2::Abstract Factory}} - {{c3::Builder}} - {{c4::Prototype}} - {{c5::Singleton}} *** Back Extra ** List the Structural design patterns: :PROPERTIES: :ANKI_NOTE_TYPE: Cloze :ANKI_NOTE_ID: 1766998062778 :ANKI_NOTE_HASH: c7c9580a73240b435860270cf692da9e :ANKI_PREPEND_HEADING: t :END: *** Text List the Structural design patterns: - {{c1::Adapter}} - {{c2::Bridge}} - {{c3::Composite}} - {{c4::Decorator}} - {{c5::Façade}} - {{c6::Flyweight}} - {{c7::Proxy}} *** Back Extra ** List the Behavioural design patterns: :PROPERTIES: :ANKI_NOTE_TYPE: Cloze :ANKI_NOTE_ID: 1766998062781 :ANKI_NOTE_HASH: 0a154564543fc1f3591aa077c5db5652 :ANKI_PREPEND_HEADING: t :END: *** Text List the Behavioural design patterns: - {{c1::Chain of Responsibility}} - {{c2::Command}} - {{c3::Iterator}} - {{c4::Mediator}} - {{c5::Memento}} - {{c6::Observer}} - {{c7::State}} - {{c8::Strategy}} - {{c9::Template}} - {{c10::Visitor}} *** Back Extra ** List the Creational design patterns: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347962 :ANKI_NOTE_HASH: e6c09709c0516722d4a04190b37ada46 :END: - Factory Method - Abstract Factory - Builder - Prototype - Singleton ** List the Structural design patterns: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347963 :ANKI_NOTE_HASH: a316384fc105a7909387cdfe040e95fa :END: - Adapter - Bridge - Composite - Decorator - Façade - Flyweight - Proxy ** List the Behavioural design patterns: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347965 :ANKI_NOTE_HASH: 9e7b3e4d94cbd230ce3da689bb09b219 :END: - Chain of Responsibility - Command - Iterator - Mediator - Memento - Observer - State - Strategy - Template - Visitor ** A design pattern used to enhance a components' functionality dynamically at run-time is: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347966 :ANKI_NOTE_HASH: a3ac4828f28fb941c881738b5a9eccbf :END: Decorator ** What does DRY stand for? What about KISS? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347967 :ANKI_NOTE_HASH: 3523e06269bc1b64f8f7e48244b32f65 :END: Don't Repeat Yourself Keep It Simple, Stupid ** {{c1::Encapsulate}} what varies. :PROPERTIES: :ANKI_NOTE_TYPE: Cloze :ANKI_NOTE_ID: 1765542048007 :ANKI_NOTE_HASH: c0b73d772c72dd2a47c07ef38b4c860e :ANKI_PREPEND_HEADING: t :END: *** Text {{c1::Encapsulate}} what varies. Favour {{c2::composition}} over inheritance. *** Back Extra ** What are the access modifiers in Java? :java: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347970 :ANKI_NOTE_HASH: 5eb1affcf5d8b930db7d0dcd411f2e17 :END: - public - private - protected - no-modifier (default) ** What is the Observer Design Pattern? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347971 :ANKI_NOTE_HASH: 7ea7554074a16faebcaf66a82873d003 :END: the subject, when changed, iterates through its list of subscribers (observers), and calls their update methods. ** What are the 5 categories of Design Smells? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347972 :ANKI_NOTE_HASH: 4faa204f390e6ee3ac44b4ede75d55e6 :END: 1. Bloaters, 2. OO Abusers, 3. Change Preventers, 4. Dispensables, 5. Couplers ** What are the differences between =public=, =private=, and =protected= modifiers in Java? :java: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347973 :ANKI_NOTE_HASH: 8245cc0cad9ebb0a3a23393222b66e86 :END: =public= is visible to the world, =private= is visible to that class, and =protected= is visible to package and all subclasses. ** Give examples of /OO Abusers/ (design smell) :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347974 :ANKI_NOTE_HASH: 50b1bcf91b876c2dd320b0fb13d78b0f :END: - alternative classes with different interfaces - switch statements - refused bequest - temporary field ** Give examples of /Bloater/ design smells :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347975 :ANKI_NOTE_HASH: 321b7d0a0e7d93bb4491a034f4175ed1 :END: - long method - large class - primitive obsession - long parameter list - data clumps ** Give examples of /Bloater/ design smells :PROPERTIES: :ANKI_NOTE_TYPE: Cloze :ANKI_PREPEND_HEADING: t :ANKI_NOTE_ID: 1766997708939 :ANKI_NOTE_HASH: d3c71a38f47b89b974dec742833515d8 :END: *** Text Give examples of /Bloater/ design smells - {{c1::long method}} - {{c2::large class}} - {{c3::primitive obsession}} - {{c4::long parameter list}} - {{c5::data clumps}} *** Back Extra ** Give examples of /Dispensable/ (design smells) :PROPERTIES: :ANKI_NOTE_TYPE: Cloze :ANKI_PREPEND_HEADING: t :ANKI_NOTE_ID: 1766997635789 :ANKI_NOTE_HASH: d703ab8a5bfc54351bd77d977486a4fb :END: *** Text Give examples of /Dispensable/ (design smells) - {{c1::comments,}} - {{c2::data class,}} - {{c3::lazy class}} - {{c4::duplicate code}} - {{c5::dead code}} - {{c6::speculative generality}} *** Back Extra ** Give examples of /Dispensable/ (design smells) :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347977 :ANKI_NOTE_HASH: aae484c536171ce1fc8864536aebb7d5 :END: - comments, - data class, - lazy class - duplicate code - dead code - speculative generality ** What is the 'lazy class' design smell? What category does it fall in? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766191834232 :ANKI_NOTE_HASH: 8f175ea2647a433f3c9aeadb29ac801a :END: *** Back **Lazy Class:** a class doesn’t do enough to justify its existence (too little behaviour/responsibility). **Category:** Dispensables. ** What is the 'speculative generality' design smell? What category does it fall in? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766191834236 :ANKI_NOTE_HASH: 7143af872c42791e279c50610d3e38ce :END: *** Back **Speculative Generality:** extra abstraction/hooks “for future use” that aren’t needed now (unused params, abstract classes, extension points). **Category:** Dispensables. ** What is the 'data class' design smell? What category does it fall in? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766191834238 :ANKI_NOTE_HASH: 4ded2d88103cfefa7a3552cf74059ef1 :END: *** Back **Data Class:** a class that mostly holds fields + getters/setters, with little behaviour. **Category:** Dispensables. ** What is the 'comments' design smell? What category does it fall in? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766191834240 :ANKI_NOTE_HASH: b034fbf3dd57714d595311eccbf03fc7 :END: *** Back **Comments:** code needs lots of comments to explain “what” it does because it’s unclear; comments are compensating for poor names/structure. **Category:** Dispensables. ** Give examples of /change preventers/ (design smell) :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347978 :ANKI_NOTE_HASH: 22ddb3f424c3ef0ba18b9d5e3508b227 :END: - divergent change - parallel inheritance hierarchies - shot gun surgery ** What is the 'shotgun surgery' design smell? What category does it fall in? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766191834243 :ANKI_NOTE_HASH: 88210ebfe4940ac633339672bd2826d4 :END: *** Back **Shotgun Surgery:** one logical change forces edits in many classes/files. **Category:** Change Preventers. ** What is the 'parallel inheritance hierarchies' design smell? What category does it fall in? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766191834245 :ANKI_NOTE_HASH: 62812e165671b89598ea3f02dbcadb1c :END: *** Back **Parallel Inheritance Hierarchies:** adding a subclass in one hierarchy forces a matching subclass in another. **Category:** Change Preventers. ** What is the 'data clumps' design smell? What category does it fall in? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766191834247 :ANKI_NOTE_HASH: fd1340fdf52c194d0ebb10fc267a1d0d :END: *** Back **Data Clumps:** the same group of variables keeps appearing together (often passed together) — should be one object/value type. **Category:** Bloaters. ** What is the 'primitive obsession' design smell? What category does it fall in? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766191834249 :ANKI_NOTE_HASH: fd67b4c5c85b2ef2dbf0e996ffe3644a :END: *** Back **Primitive Obsession:** using primitives (string/int/etc.) instead of small domain types (e.g. Email, Money), so validation/meaning is scattered. **Category:** Bloaters. ** Give examples of /couplers/ (design smells) :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347979 :ANKI_NOTE_HASH: 7bb7007a0ba7de4e6445dad08d5bde8e :END: - feature envy - inappropriate intimacy - incomplete library class - middle man - message chains ** What is the 'incomplete library class' design smell? What category does it fall in? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766191834252 :ANKI_NOTE_HASH: 721e319a6f6f6e2ef56c4a1c8854eb31 :END: *** Back **Incomplete Library Class:** a library class is missing key behaviour you need, so you end up with awkward wrappers/workarounds everywhere. **Category:** Couplers. ** What is the 'middle man' design smell? What category does it fall in? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766191834253 :ANKI_NOTE_HASH: 017b61f0fa198f0dfa940219714941f3 :END: *** Back **Middle Man:** a class mostly delegates calls to another class and adds little/no value. **Category:** Coupler. ** What is the 'message chains' design smell? What category does it fall in? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766191834255 :ANKI_NOTE_HASH: d7f1b4f3b9fdb0598d0b25bc5ad18c14 :END: *** Back **Message Chains:** long call chains like =a.getB().getC().doThing()=; lots of knowledge of object structure. **Category:** Couplers. ** What is the 'feature envy' design smell? What category does it fall in? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766191834256 :ANKI_NOTE_HASH: aaff7f8bd5444aeefd313a0245b405a8 :END: *** Back **Feature Envy:** a method uses another class’s data/behaviour more than its own; it “wants to live” in the other class. **Category:** Couplers. ** What is the 'inappropriate intimacy' design smell? What category does it fall in? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766191834257 :ANKI_NOTE_HASH: 18a1ab1d21cef82df600c9aae1ee2c53 :END: *** Back **Inappropriate Intimacy:** two classes are too tightly coupled (reach into each other’s internals / lots of private-field access). **Category:** Couplers. ** What is the *Decorator Pattern*? What kind of pattern is it? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347980 :ANKI_NOTE_HASH: e9051845d59420ddb2c965131a1ddea2 :END: Structural; attaches additional responsibilities to an object dynamically. ** Explain Strategy vs. Template Method :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347981 :ANKI_NOTE_HASH: 75b7b72a32fb0f360f8b062ff90fdb40 :END: Template Method: - Uses inheritance - Algorithm structure is fixed - Subclasses override specific steps - Behaviour chosen at compile time Strategy: - Uses composition - Entire algorithm is replaceable - Behaviour delegated to a strategy object - Behaviour chosen at runtime ** What is the *Template* pattern? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347982 :ANKI_NOTE_HASH: 31834a8b69ff0722b79fe4a561169f01 :END: *** Back **Template (Template Method) pattern:** define the *skeleton* of an algorithm in a base class method, and let subclasses override specific steps (hooks) without changing the overall structure. Example idea: =parse()= calls =read() -> validate() -> transform() -> save()=, where subclasses customise =validate/transform=. ** What is the Singleton pattern? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347983 :ANKI_NOTE_HASH: 27828fbcc0e66b6155dd13ab7d60a61b :END: ensures only one instance of an object can be created. Note: can be broken with synchronised calls -- be sure to protect against this in production. ** What is the Factory Pattern? :PROPERTIES: :ANKI_NOTE_TYPE: Pretty with Example :ANKI_NOTE_ID: 1765335347984 :ANKI_NOTE_HASH: 56b2b1fd471a3e519bf5add50b2b240c :END: *** Back Creates objects based on runtime conditions without exposing the instantiation logic to the client. The client depends on an interface, not a concrete class. Promotes decoupling by removing direct dependencies on concrete implementations. *** Example - Interface: =Shape= - Concrete classes: =Circle=, =Rectangle= - Factory method: =createShape(type)= returns the appropriate =Shape= at runtime ** What is the Composite Pattern? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347985 :ANKI_NOTE_HASH: 32d69f1658763ec83edd71272206c699 :END: Compose objects into tree structures to represent part–whole hierarchies. Clients treat individual objects (leaves) and compositions (composites) uniformly. ** What is Divergent Change? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347987 :ANKI_NOTE_HASH: a80ae31da54067d5d254910bbf8e22d0 :END: when you keep changing one class for different reasons. (recall this violates the single responsibility principle) ** What category does the *refused bequest* design pattern fall in? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766641281891 :ANKI_NOTE_HASH: 744581b53e345834712bac346f55913d :END: OO Abuser ** What is Refused Bequest? :PROPERTIES: :ANKI_NOTE_TYPE: Pretty with Example :ANKI_NOTE_ID: 1766192025402 :ANKI_NOTE_HASH: 87c6d26d3800cc2305e693b3ecbb3b2f :END: *** Back a subclass inherits fields/methods it doesn’t want or can’t sensibly support (often overrides to “do nothing”, throw, or leaves inherited behaviour unused) - inheritance is the wrong relationship. *** Example =class Square extends Rectangle= If =Rectangle= has =setWidth(w)= and =setHeight(h)=, a =Square= can’t accept them independently: - =square.setWidth(5)= must also change height, breaking expectations of =Rectangle= users, or - it throws/ignores =setHeight=​/​=setWidth= (refusing the bequest). This is a sign to use composition or redesign the hierarchy. ** What is Dependency Inversion in SOLID? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347989 :ANKI_NOTE_HASH: 8586908279270b9c10410b1447879bf4 :END: rely on abstractions not implementations. ** The next state of a _finite state machine_ depends on 2 things: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347990 :ANKI_NOTE_HASH: d506a564126688150f61c278c9c6fedc :END: 1. its current state 2. its input ** What is class invariance? :PROPERTIES: :ANKI_NOTE_TYPE: Pretty with Example :ANKI_NOTE_ID: 1765335347991 :ANKI_NOTE_HASH: d23c0f71fb4d70b2e92159c5de68bb24 :END: A class invariant is a property or condition that must always hold true for all valid instances of a class, before and after any public method call. *** Example - =Stack=: size \leq 0 and size \leq capacity - =BankAccount=: balance \leq 0 - =Rectangle=: width > 0 and height > 0 - =SortedList=: elements are always in non-decreasing order ** What is the Liskov Principle in SOLID? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347992 :ANKI_NOTE_HASH: 89895bbe2c5d54bdd103c5f3572e7e0e :END: you should be able to treat a member of the subclass as an object of its super class. ** What does the /Law of Demeter/ state? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766192025405 :ANKI_NOTE_HASH: 87d9aeae4e9d1b239b50e242cfddefe6 :END: *** Back **Law of Demeter (principle of least knowledge):** a method should only call methods of: - itself - its own fields - its parameters - objects it creates (locals) So: avoid long "train wreck" chains like =a.getB().getC().doThing()= — talk only to your immediate collaborators. ** The Liskov Substitution principle states that you should be able to treat a {{c1::subclass}} as an {{c1::object}} of its {{c1::superclass}}. :PROPERTIES: :ANKI_NOTE_TYPE: Cloze :ANKI_NOTE_ID: 1765335347994 :ANKI_NOTE_HASH: 6d74b7e33fe92cd8da4c6e88026ce8a4 :END: ** Design by Contract depends on {{c1::preconditions}} and {{c1::postconditions}}. It is the other side of the coin to {{c1::defensive programming}}. :PROPERTIES: :ANKI_NOTE_TYPE: Cloze :ANKI_NOTE_ID: 1765541938458 :ANKI_NOTE_HASH: 5e3d91014cef06a976e21443778fc1c7 :END: ** How does polymorphism differ from inheritance? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766575482870 :ANKI_NOTE_HASH: 920adc83f0a9871964cc41158e333a41 :END: Inheritance is a mechanism for reusing and extending behaviour by creating a subclass from a superclass. Polymorphism is about using a common interface or base type to treat different objects uniformly, while allowing different behaviour at runtime. Inheritance enables polymorphism, but polymorphism can exist without inheritance (e.g. via interfaces or type classes). ** How does polymorphism differ from dynamic typing? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766575507771 :ANKI_NOTE_HASH: b8e391c89afef0f915075d7e1793f447 :END: Polymorphism is a property of a type system where multiple concrete implementations share a common interface and behaviour is resolved based on the object’s actual type. Dynamic typing is a language feature where type checking is deferred until runtime. A language can be statically typed and polymorphic (e.g. Java, C++), or dynamically typed without true subtype polymorphism (e.g. duck typing). ** In Java streams can be constructed from a {{c1::collection}}. :PROPERTIES: :ANKI_NOTE_TYPE: Cloze :ANKI_NOTE_ID: 1765335347995 :ANKI_NOTE_HASH: 00d57af197f8ee14457fe1d9e8d2d5a3 :END: ** What is Interface Segregation in SOLID? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347996 :ANKI_NOTE_HASH: 290f1ae6caccc227d51ecbeb616d1bd8 :END: more interfaces are better than a single general interface. ** You should strive for {{c1::low}} coupling and {{c1::high}} cohesion in code. :PROPERTIES: :ANKI_NOTE_TYPE: Cloze :ANKI_NOTE_ID: 1765335347997 :ANKI_NOTE_HASH: f41dc9598077474bc90f7ea9f828bcd3 :END: ** What is single responsibility in SOLID? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347998 :ANKI_NOTE_HASH: ddd2c15762f2383dec99ee07a60e9d0a :END: When you encapsulate functionality. Note: when you start modifying the same class for different reasons, it means you're probably violating this. ** What is *open-closed* in SOLID? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335347999 :ANKI_NOTE_HASH: 0f6f3f5173ca337a0ec0164a14e5ae1b :END: "open for extension, closed for modification" ** What does SOLID stand for? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765335348000 :ANKI_NOTE_HASH: 0329b23dd23e5c471a20ef8cf85c7026 :END: - Single Responsibility - Open-Closed - Liskov Principle - Interface Segregation - Dependency Inversion ** UML arrow class diagrams :PROPERTIES: :ANKI_NOTE_TYPE: Cloze :ANKI_PREPEND_HEADING: t :ANKI_NOTE_ID: 1766191441371 :ANKI_NOTE_HASH: 2434b08378b7d4a5eb198d553af57d43 :END: *** Text UML arrow class diagrams - {{c1::Inheritance (generalisation)}} {{c2:: \begin{center} \begin{tikzpicture}[scale=0.4,transform shape, every node/.style={draw, rectangle, minimum width=2cm}, node distance=3cm] \node (Animal) {Animal}; \node (Cat) [below=of Animal] {Cat}; % solid line + open triangle \draw[-{Triangle[open, length=4mm, width=4mm]}] (Cat.north) -- (Animal.south); \end{tikzpicture} \end{center} }} - {{c3::Implementation (realisation)}} {{c4:: \begin{tikzpicture}[ every node/.style={draw, rectangle, minimum width=2cm}, node distance=4cm] \node (FourLegged) {<> FourLegged}; \node (Cat) [right=of FourLegged] {Cat}; % dashed line + open triangle \draw[dashed, -{Triangle[open, length=4mm, width=4mm]}] (Cat.west) -- (FourLegged.east); \end{tikzpicture} }} - {{c5::Association}} {{c6:: \begin{tikzpicture}[ every node/.style={draw, rectangle, minimum width=2cm}, node distance=4cm] \node (Professor) {Professor}; \node (Student) [right=of Professor] {Student}; % solid line + simple arrow \draw[-{Stealth}] (Professor.east) -- (Student.west) node[midway, above, draw=none] {communicates with}; \end{tikzpicture} }} - {{c7::Dependency}} {{c8:: \begin{tikzpicture}[ every node/.style={draw, rectangle, minimum width=2cm}, node distance=4cm] \node (Professor) {Professor}; \node (Salary) [right=of Professor] {Salary}; % dashed line + simple arrow \draw[dashed, -{Stealth}] (Professor.east) -- (Salary.west) node[midway, above, draw=none] {depends on}; \end{tikzpicture} }} - {{c9::Composition}} {{c10:: \begin{tikzpicture}[ every node/.style={draw, rectangle, minimum width=2cm}, node distance=4cm] \node (University) {University}; \node (Department) [right=of University] {Department}; % larger filled diamond at WHOLE end + arrow to part \draw[{Diamond[length=6pt, width=6pt, fill=black]}-{Stealth}] (University.east) -- (Department.west) node[midway, above, draw=none] {consists of}; \end{tikzpicture} }} - {{c11::Aggregation}} {{c12:: \begin{tikzpicture}[ every node/.style={draw, rectangle, minimum width=2cm}, node distance=4cm] \node (Department) {Department}; \node (Professor) [right=of Department] {Professor}; % larger open diamond at WHOLE end + arrow to part \draw[{Diamond[open, length=6pt, width=6pt]}-{Stealth}] (Department.east) -- (Professor.west) node[midway, above, draw=none] {contains}; \end{tikzpicture} }} *** Back Extra ** What does UML stand for? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765542106730 :ANKI_NOTE_HASH: ef014a917aa93506060eb547879a7f09 :END: Unified Modelling Language ** A static method can be used as a {{c1::function}} of a standalone class. You {{c1::do not}} need to create an {{c1::instance}} of the class. :PROPERTIES: :ANKI_NOTE_TYPE: Cloze :ANKI_NOTE_ID: 1765542103633 :ANKI_NOTE_HASH: d8c2a3bfbd180f6d7109992597c5f04e :END: ** What are the two relationships between classes? When are they each used? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765541938467 :ANKI_NOTE_HASH: 0ea60e2178ecdee57bbe004ddad97bfa :END: 1. Inheritance ("is-a"): Used when a subclass is a specialised form of a superclass and can be substituted for it. Enables polymorphism, but should be used carefully to avoid tight coupling. 2. Composition ("has-a"): Used when a class is built from other objects to reuse behaviour. Promotes flexibility and loose coupling, and is generally preferred over inheritance. ** What are the three types of Design Patterns? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1765542124608 :ANKI_NOTE_HASH: bb107930f2fa1ea1b7fbd1b0afc304d9 :END: 1. Behavioural 2. Structural 3. Creational ** What does the Command Pattern do? :design:patterns: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766192025407 :ANKI_NOTE_HASH: a462f8859ec0e57f695bd9e441790475 :END: *** Back Encapsulates a request as an object (a "command"), so you can parameterise actions, queue/log them, and support undo/redo. Typical shape: =Command.execute()= (and sometimes =undo()=). ** What does the Iterator Pattern do? :design:patterns: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766192025409 :ANKI_NOTE_HASH: 290d99f5124959cb0cf1c50a9d585f5d :END: *** Back Provides a standard way to traverse a collection *without exposing its internal representation*. Lets clients iterate uniformly (e.g., =hasNext()/next()=), regardless of the underlying data structure. ** What is the State Pattern? :design:patterns: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766192025412 :ANKI_NOTE_HASH: 82bae57f012b4d13cceb1dda0752d167 :END: *** Back Lets an object change its behaviour when its internal state changes by delegating to state objects. Replaces big =if/switch= on state with polymorphism (e.g., =Context= holds a =State=). ** Explain Strategy vs. State Pattern :design:patterns: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766192025414 :ANKI_NOTE_HASH: ee922dd33972d6f22984289056b849e8 :END: *** Back - **Strategy:** choose an algorithm/behaviour from interchangeable options; usually picked by client/config and doesn’t automatically change. - **State:** behaviour depends on *current state*; the object transitions between states internally over time. ** Explain Decorator vs. Template Pattern. :design:patterns: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766192025417 :ANKI_NOTE_HASH: 6aa76f4b58e65cc0c36c3c1c7b0caa06 :END: *** Back - **Decorator:** add responsibilities at runtime by wrapping an object (composition); stackable wrappers. - **Template Method:** define the skeleton of an algorithm in a base class; subclasses override specific steps (inheritance, compile-time structure). ** Explain Strategy vs. Decorator pattern :design:patterns: :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766192025419 :ANKI_NOTE_HASH: df5f7277a5507ce79d9c7a9dd79937a7 :END: *** Back - **Strategy:** swap one behaviour/algorithm inside an object (usually one of many choices). - **Decorator:** *wrap* an object to add extra behaviour before/after delegating; can layer multiple additions. ** What is the difference between the Bloater design smell category and the Dispensable category? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766881230728 :ANKI_NOTE_HASH: 9c8c6b0fd8d6112cab51af0e80e448dd :END: you can straight up *delete* Dispensables. you cannot do so with Bloaters -- they still encode functionality. ** What is the difference between the "Couplers" design smell category and "Change Preventers"? :PROPERTIES: :ANKI_NOTE_TYPE: Basic :ANKI_NOTE_ID: 1766881230735 :ANKI_NOTE_HASH: 71b166dd242fadfdbb05dfc07d399d38 :END: *Couplers* make change more /difficult/ by tightly binding classes or modules together. *Change Preventers* make change more /widespread/: a single logical change forces modifications in many places.