Design Patterns: Introduction Flashcards
What are Design Patterns?
describes generic solutions to software design problems.
What are Creational Patterns?
provide approaches to object instantiation.
What are Structural Patterns?
provide approaches for combining classes and objects to form larger structures.
What are Behavioural Patterns?
provide approaches for handling communication between objects.
Explain: “Program to an interface, not an implementation.”
By “interface” is meant the general concept of abstraction, which could refer to a Java interface or an abstract class.
To accomplish this, use the most general type (e.g. interface) possible when declaring variables, constructor and method arguments, etc. Doing so gives extra flexibility as to the actual types that are used at run-time.
Explain: “Prefer object composition over inheritance.”
Where a class is related to another in some way, you should distinguish between “is a” and “has a” relationships. Sometimes using a “has a” relationship is more flexible even when an “is a” relationship seems the natural choice.
Explain: “Keep objects loosely-coupled.”
Ideally, classes should model just one thing, and only be composed of other objects that are genuinely required (such as Vehicle requiring an Engine). By keeping this to a minimum, you make your class more re-usable.
Explain: “Encapsulate the concept that varies.”
If you’ve written a class in which some parts are the same for each instance but another part of the class varies for each instance, consider extracting the latter into a class of its own, which is referenced by the original class.