Facts about Patterns Flashcards
(23 cards)
True or False:
Patterns enable the re-use of an idea but not the re-use of code
True
Patterns describe logic and structure
Patterns form a vocabulary for software design
True
Vocab = words. Patterns allow you to give names to ways of doing things. Makes it easy for humans to understand and remember a standard way of doing things
A pattern is a Problem-Solution pair in a context
True
Patterns are reusable solutions to common problems in a given context
True or False:
Patterns help improve developer communication
True
If the dev’s all know the patterns, they can say “ yo use this pattern” or “ ye I used that pattern” rather than explaining each part of the code and how it works
Most of the common design patterns are object-oriented
True
Explains itself, because OO is common
Whether or not you are using design patterns for a project, being unfamiliar with patterns is a disadvantage for a team member
True
Because patterns make it easier to understand code and make it efficient. If someone does not know what pattern you have used then they are disadvantaged
Integrating patterns into a software development process is a human-intensive activity
True
Humans have to actively integrate patterns
A pattern describes a common programming problem and its solution
True
The Gang of Four book has become the de-facto standard pattern text
True
GoF is popular and good, so most dev’s like to use it
Gang of Four refers to the 4 main types of patterns
False
Gang of Four refers to the 4 authors
GRASP is a common Architectural Pattern
False
It is a set of design patterns. MVVC/ MVC and Client/Server are architectural patterns
Patterns enable the re-use of code
False
It enables the re-use of an idea
Consider the following C# code segment. The address-details “data clump”(postcode and city) is used in other parts of the program as well and should be factored out.
What refactoring do you suggest?
public class clsCustomer { private string _Name; private string _PostCode; private string _City;
public string Name { get { return _Name; } }
public string PostCode { get { return _PostCode; } }
public string City { get { return _City; } }
}
Extract Class
is the combination of the Move Method and the Move Field
Change the code so that it works as before: 1. Write the new code for the clsCustomer class after the change
public class clsCustomer { private string _Name; private string _PostCode; private string _City;
public string Name { get { return _Name; } }
public string PostCode { get { return _PostCode; } }
public string City { get { return _City; } }
}
public class clsCustomer
{
private string _Name;
private clsAddressDetails_AddressDetails;
public string Name
{
get { return _Name; }
}
public clsAddressDetails AddressDetails { get { return _AddressDetails; } } }
2: Write the code for the clsAddressDetails class:
public class clsCustomer
{
private string _Name;
private clsAddressDetails_AddressDetails;
public string Name { get { return _Name; } }
public clsAddressDetails AddressDetails { get { return _AddressDetails; } } }
public class clsAddressDetails
{
private string _PostCode;
private string _City;
public string PostCost { get { return _PostCode } }
public string City { get { return _City } }
}
Notice that although there are changes, the clsCustomer class doesn’t change cause the public facade of clsCustomer is exactly the same. Only internal changes have been made.
Duplicated Code:
- Why is it problematic?
- Why is it problematic?
a.) If two or more people write similar code in the same program, this will cause 2 parts of code to work the same job.
Duplicated code means that if that code changes, it needs to change in both places, so it adds size, complexity, and makes it harder to maintain.
Duplicated Code:
- How can it be fixed?
- How can it be fixed?
a. ) Extract Method - replace duplicate code with a method made from the repeating code. (followed up with pull-up field for the fields used in the method that your pulling)
b. ) Extract Superclass if that’s impossible, Extract class in one class and use new component in the other
Feature Envy:
- Why is it problematic?
- Why is it problematic?
a. ) Feature Envy comes in when an object is accessing properties that do not belong to it.
Basically, if class A is calling lots of methods/ fields from class B more than from itself, maybe the methods calling object B’s methods/fields should belong in object A instead of object B
b.) Dis-organized code
Feature Envy:
2.) How can it be fixed?
- How can it be fixed?
a. ) Move Field - Move the field to the appropriate class where it is used
b. ) Move Method - Take the whole method and move it to a different class
OR
c.) Extract Method - Take a piece of code out of a method and in this case place it as its own method in a different class
Name 5 pattern names from the “Fundamental” Category
a. ) Information Expert
b. ) Pure Fabrication,
c. ) Low Coupling,
d. ) Don’t talk to strangers - law of Demeter
e. ) Polymorphism - protected variations
f. ) Cohesion
g. ) Creator
h. ) Controller
i. ) Indirection
j. ) Delegation
k. ) Interface
l. ) Immutable
m. ) Marker Interface
Name 4 pattern names from the “Behavioural” Category
a. ) Strategy
b. ) Template Method
c. ) Memento
e. ) Observer
f. ) Command Action Transition
g. ) State Objects for States
h. ) Chain of Responsibility
i. ) Interpreter
j. ) Iterator Enumerator
k. ) Mediator
l. ) Visitor
Name 2 pattern names from the “Creational” Category
a. ) Singleton
b. ) Factory Method - virtual constructor
c. ) Prototype,
d. ) Builder
e. ) Abstract Factory Kit
f. ) Lazy Initialization,
g. ) Object Pool
Name 2 pattern names from the “Structural” Category
a. ) Adapter Wrapper
b. ) Facade
c. ) Proxy Surrogate
d. ) Bridge Handle / Body
e. ) Composite
f. ) Decorator Wrapper
g. ) Flyweight
h. ) Container