1/73
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Article 1
What is the root of the intrinsic issues of Unity’s framework?
how Unity makes injecting dependencies upon entities hard
What is a dependency?
an object needed by another object to execute its code
If Object A needs Object B to execute its code, then B is a ______ of A and when its passed in we say B is ______ into A
dependency, injected
What is the Unity code framework designed around?
Bilas’ Data Driven GameObject System
What are the benefits to a component-based entity framework?
push users to favor composition over inheritance (build complex behavior by combining simple objects (composition) instead of creating deep class hierarchies (inheritance).
keeps classes small, clean, and focused, modular + single responsibility!!
What are the entities in Unity?
GameObjects
What are the components based on in Unity?
MonoBehavior implementations
What does the Entity Component structure rely on?
components define the whole entity’s logic
What are the five principles that must be obeyed in order to use a Gameobjects framework properly?
Monobehaviors must operate within the GameObjects they are attached to as this allows for encapsulation (Encapsulation means hiding the internal details of how something works and only showing what’s necessary to the outside.) Components must handle only the entity they are attached to
Monobehaviors are modular (Modular software engineering means breaking a program into smaller, independent parts (modules) that each do one specific job.) and single responsibility classes that can be reused by other game objects
behavior of a gameobject should be added through new MonoBehaviors instead of adding code to an existing Monobehavior
GameObjects should not be created without a view (Don't create empty GameObjects just to use as managers or containers.)
Monobehaviors can know other components on the same GameObject using GetComponent but shoudln’t know monobehaviors from other gameobjects
Instead of specializing an entity _____ through ______, components allow us to expand behavior ______
vertically, polymorphism, horizontally
Why should GameObjects without a view not be used?
GameObjects all have transforms implying they should be in the world to use that transform
What is the problem with Unity asking to write monobehaviors for all cases?
the logic might not be based on an entity
What’s wrong with Gameobject.Find()
slow function
somebody could’ve renamed the object
can lead to runtime errors not caught in compiletime (really hard to tell which GameObject it can’t find if multiple .Finds)
What’s wrong with Object.FindObjectOfType?
bad version of Service Locator pattern as we’re not allowed to abstract the interface (eg. dependency injection)
Unity discourages use of interfaces and instead pushes user to use Monobehaviors even when the use of Monobehavior not necessary
what is wrong with the singleton pattern
so many reasons (break encapsulation, hide dependencies, impossible to write proper unit tests, implementation of service instead of abstraction, make refactoring awkward, memory leaks)
code will become a giant spaghetti mess over time
will lead to refactoring to become a huge operation
your design will smell as its your only way of injecting dependencies
singletons rely on public functions which can change states in the singleton itself (very hard to track!)
can’t be designed with inversion of control in mind
forced to write functions such as IsSingletonReady leaving control of internal states to external entites
main reason why communication is injected
to allow objects to communicate with one another
What is the solutions to injecting dependencies?
resolve dependencies through service locator pattern (only to inject actual stateless services), injecting dependencies manually through constructors/setters (most optimal but hard), exploiting reflection through IOC container (Using reflection to automatically discover and create the necessary objects and inject them into your classes at runtime, without writing all the wiring code yourself.), singletons
either that or IOC containers
What does IOC container stand for and why and what is it?
Inversion of Control container bc the design is thought in such a way that the dependencies are never created by the user, but they are lazily created by the container when they are required.
container that creates and contains the dependencies that must be injected inside the application objects.
Article 2
t/f keeping your code maintainable is as important as getting it done
true
What is the most important condition to writing maintainable code?
simple code
What is composition root?
where the IoC container must be initialized before everything else starts using it
T/F. a project can have multiple composition roots
true
What is the cardinal issue of the whole Unity code design?
Unity doesn’t really have a single entry point that allows objects to be initialized
Dependency graph
dependency waterfall
one class dependent on another classs which is dependent on another
t/f if we don’t consider objects that must be created dynamically after the start-up phase, all the dependencies can be solved right at the beginning of the application, within the Composition Root context.
true
What does the Bind method do?
every time a dependency of a known type is found, it must be solved with an instance of the type bound to it
What is the [Inject] keyword for?
sed to mark a property or field where a dependency should be automatically filled in by an IoC (Inversion of Control) container.
Compare IOC container to singleton?
gives same flexibility without risking wildly using static classes everywhere/avoiding memory leaks
dependencies correctly injected only if apart of dependency graph
can be also injected through interfaces, which allows changing implementations without changing the code that uses them.
What is the most powerful benefit behind the IoC container?
the code will not depend on the implementation of the classes anymore, but always on their abstractions (if interfaces are used)
What to do if you want to create objects while the game is running without using new Keyword
use Factories instead
can use the container to inject dependencies into the object that they create
factories are the only classes that can use the Container outside the Composition Root
IoC container and Monobehavior relation
IoC container helps shift code patterns from intensive use of Monobehavior to use of normal classes that implement interfaces
How to make IoC container work with Unity game objects loaded in the scene?
To make IoC work with Unity's scene-loaded objects, here's what the author suggests:
Create a special GameObject, called something like "Context"
or "IoCContext"
.
Attach a script with your IoC container to this GameObject.
In this script’s Awake()
method, the container will:
Scan all child GameObjects
Find all MonoBehaviours
Inject their dependencies using [Inject]
tags — before any Start()
methods are called.
This works because Awake()
is called before Start()
, so dependencies are available in time.
T/F IoC container great with MVP
T
T/F IOC container library shouldnt support hierarchal containers and why?
false
bad practice to just bind every possible dependency in a single container, as this won’t promote separation of concerns.
they encapsulate the dependencies needed for a specific context, they will also inherit dependencies from parent containers.
Article 3
What is the main problem with dependencies?
the IoC container solution was scarily used too often as an alternative to the Singleton pattern to inject dependencies
TOO MANY DEPENDENCIES
Why is dependency injection not supposed to do and why?
couple object implementations,
interfaces are passed instead of classes. This is necessary to allow changing implementations in the composition root without needing to touch any existing class code (open/closed in SOLID)
Should you pass in many dependencies in the IoC container and why?
you shouldn’t because its badly layered/structured code (j coupling now with interfaces becoming irreleavant)
T/F IoC containers should only be used by experienced teams or else people will just start injecting dependencies everywhere
true
T/F All forms of communications involve dependency injection.
T
What are several ways of letting objects communicate?
interface injection: usually the interface A is injected in B, B is coupled with A [e.g.: Inside a B method A is used, B.something() { A.something());]
Standard events (callbacks): Inside A, B is injected to expose the event, B.onSomething += A.Something
Commands: B and A are uncoupled, B could call a command that calls a method of A. Commands are great to encapsulate business logic that could potentially change often. A Command Factory is usually injected in B.
Mediator: usually B and A do not know each other, but know their mediator. B and A pass themselves into the mediator and the mediator wires the communication between them (i.e.: through events or interfaces). Alternatively, B and A are passed to the mediator in the Composition Root, making A and B dependencies for the Mediator and not the other way around.
Various other patterns like Observers, Event Bus[2], Event Queue[3] where A and B do not know each other but are put in communication through these patterns like it happens with the Mediators.
What are the two worst methods of the ones mentioned above?
they couple interfaces that could change over time.
T/F Dynamic objects can still be dependencies
false
What are services?
units of logic that allow a client to communicate with external devices, service can be executed from inside Commands or from inside GUI Presenters
Where are services usually stored?
service layer in an interface
Why is an IoC container called Inversion of Control container?
IoC containers take away the responsibility to create objects from the user and shift it to the framework. In this sense, they “invert” the control over the creation of dependencies.
Does IoC give control to specialized or abstract classes and what does it give control?
abstract
framework classes should control both the dependency creation (Inversion Of Creation Control) and the logic execution (Inversion of Flow Control) of more specialised objects.
T/F factories are always created and passed as a dependency from the Composition Root.
true
Why is inverting the creation control important?
code becomes independent of constructor implementation (reduces coupling as dependency can be passed as interface instead)
code only dependent on abstraction not implementation
objects injected come already created with its dependencies resolved
flow of code can change according to context
Can Inversion of Creation Control be achieved without using an IoC container?
yes
What is Inversion of Flow control?
Inversion of control (IoC) describes a design in which custom-written portions of a computer program receive the flow of control from a generic, reusable library. A software architecture with this design inverts control as compared to traditional procedural programming: in traditional programming, the custom code that expresses the purpose of the program calls into reusable libraries to take care of generic tasks, but with inversion of control, it is the reusable code that calls into the custom, or task-specific, code
Article 5
How does the Entities Component System design work?
entities are essentially ids
components are units of data. they wrap data that can be shared between systems and do not have logic to manage data
systems are classes where the logic lies
how we can apply the Open/Close principle without ever breaking it
every time we need to introduce a new specific behaviour, we must create a new System that simply uses components as data to apply it.
GameEngine is a _____ while monobehaviors are _____
high-level module, low level module
t/f DIP wants us to find the common behaviours in our implementations, abstract them and package them away in higher-level modules.
t
What is strategy pattern?
behavioral design pattern that lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.
Hollywood principle
specialized code must never call directly methods of more high-level module classes. It is the high-level module mechanism to control the flow of our specialised code.
The author believes that IoC should have a _____ approach
top down
should be doing more abstract stuff and making interfaces for them in lower end modules
What is a utility class?
every class that exposes many public functions ends up being a Utility class, used in several places. Utility classes should be static and stateless. Stateful Utility classes are always a design error.
What is a way to achieve Inversion of Flow Control
events
take home message
The take-home message here is that IoC and DIP are asking us to step away from the Bilas/Unity Gameobject approach that tells us to write behaviours in our specialised components but instead asks us to find the common behaviours of our entities, abstract and pack them in higher-level modules, make our specialised entities implement the high-level interfaces and eventually register them in the high-level managers to be later on processed by the framework.
MVC Pattern Articles Division
Why is it difficult to unit test in Unity?
don’t have direct access to the Composition root
What is a solution to the unit testing problem in Unity?
create a GameObject in an EditMode unit test and then call AddComponent<> to it to initialize an object and then use method injection or setter injection to inject dependencies, but this isn’t the most elegant way.
test these objects in PlayMode tests (essentially integration tests), but these tests run slower and serve a different person than unit testing does.
create composition root and AfterSceneLoad
Why do we use AfterSceneLoad instead of BeforeSceneLoad
we need the game objects to be loaded into the scene
Model-view-controller pattern (MVC)
Model: Manages data, business logic, and interacts with databases.
View: Represents the user interface and displays data from the model.
Controller: Handles user input, interacts with the Model to update data, and updates the View to reflect changes
Dependency inversion principle rules
Recall that the D in SOLID stands for the Dependency Inversion Principle.
A. HIGH LEVEL MODULES SHOULD NOT DEPEND UPON LOW LEVEL MODULES.
BOTH SHOULD DEPEND UPON ABSTRACTIONS.
B. ABSTRACTIONS SHOULD NOT DEPEND UPON DETAILS. DETAILS SHOULD
DEPEND UPON ABSTRACTIONS.
What is an out parameter modifier?
return value is a boolean that lets us know
if the operation succeeded and the out parameter has an actual value or is null.
if you are practicing strict Test Driven Development (TDD) when would you write unit tests
first
T/F Model view controller pattern makes your code more complex but maintainable
true
Model view presenter pattern
Presenter
replaces the Controller and the Presenter is actually responsible for observing changes in the
model and updating the View. The Presenter would listen to UI events (such as clicking a UI
button or typing into a UI text box).