diff --git a/lectures/week7.html b/lectures/week7.html index 902bdf6..8cf7c96 100644 --- a/lectures/week7.html +++ b/lectures/week7.html @@ -1,1792 +1,1924 @@
Design Patterns: Elements of Reusable Object-Oriented Software by E. Gamma, R. Helm, R. Johnson and J. Vlissides (the gang of four)
Design patterns have four essential elements:
Pattern name: associates problem with solutions&consequences
$\Rightarrow$ Improves communication and documentation
Problem: Scenario/conditions of when to apply a pattern
Solution: Description of the arrangement of classes and objects
Consequences: Results and trade-offs. (allows to know design alternatives)
General description: Gives access to elements of a container without exposing its internal representation
Our problem (Particles):
struct List {
using Element = Planet;
// define a sub type
struct ListIterator;
int size();
ListIterator begin();
ListIterator end();
};
struct List::ListIterator {
Element &operator*();
void operator++();
bool operator!=(ListIterator &e);
};
struct List {
struct ListIterator;
int size();
void add(Element e) { container.push_back(e); };
ListIterator begin();
ListIterator end();
private:
std::vector<Element> container;
};
struct List::ListIterator {
ListIterator(Element ¤t) { this->ptr = ¤t; }
Element &operator*() { return *this->ptr; };
void operator++() { ++this->ptr; };
bool operator!=(ListIterator e) { return e.ptr != this->ptr; };
private:
Element *ptr;
};
List::ListIterator List::begin() { return ListIterator(this->container[0]); };
List::ListIterator List::end() {
return ListIterator(this->container[this->size()]);
};
int main() {
int p1 = 1;
List list;
for (auto &&v : list) {
std::cout << v << std::endl;
}
}
List list;
for (auto &&v : list) {
std::cout << v << std::endl;
}
General description: Ensure that a class has only one instance, and provides a global point of access
Our problem (Particles):
class Singleton {
private:
Singleton();
friend Singleton &get();
};
Singleton *unique_instance = nullptr; // no encapsulation
Singleton &get() { // not part of Singleton::
if (unique_instance == nullptr)
unique_instance = new Singleton();
return *unique_instance;
}
class A {
public:
static int a;
};
int A::a = 1;
A a1;
A a2;
a1.a = 2; // also modifies a2.a
class B {
public:
static int foo();
};
// do not need an instance to be called
B::foo();
class A:
# in the scope of the class
a = 1
def __init__(self):
pass
a1 = A()
a2 = A()
a1.a = 2 # also modifies a2.a
class B :
@staticmethod
def foo(cls):
print(cls)
# do not need an instance to be called
B.foo()
struct Particles {
static Particles &get();
private:
Particles();
static Particles obj;
};
struct Planets : public Particles {
static Particles &get();
};
Particles &obj = Planets::get(); // creation
Particles &obj2 = Particles::get(); // access
General description: Provides an interface for creating dependent/coherent objects without specifying their concrete classes.
Our problem (Particles)
The motion of the particles is made with the Newmark/Velocity Verlet explicit integration scheme:
+$\forall$ particle $i$ with $v_i$ its velocity, $p_i$ its position, $f_i$ the force applying and $a_i$ its acceleration
+in the particle's interaction forces:
+with $\mathbf{r}_{ij} = p_j - p_i$, $\mathbf{d}_{ij} = \frac{\mathbf{r}_{ij}}{r_{ij}}$, $R_i$ the radius and $K$ a spring stiffness.
+in their unit system: astronomical units (AU), SI units...
+
Let's do that together !