A-level Computing/AQA/Paper 1/Fundamentals of programming/OOP Techniques

Techniques in OOP
Techniques

* association aggregation * composition aggregation
 * encapsulation
 * aggregation
 * overriding

Best practices in OOP
OOP is probably the most widely-used programming paradigm today. As a result, there are many different ways it is used, and many ideas on good and bad styles of programming within OOP languages. However, there are three core ideals that are generally agreed to be valuable in most or all OOP situations:


 * Encapsulate your data, only allowing access on a need-to-know basis
 * Where you have the choice: choose Composition rather than Inheritance
 * When connecting different pieces of code together, use interfaces, rather than directly referencing them

The three ideals are typically abbreviated as:


 * "Encapsulate what varies"
 * "Favour composition over inheritance"
 * "Program to interfaces, not implementation"

Original OOP article follows, with only minor changes e.g. sp corrections

OO - PIIE
When talking about OOP you must remember the following: Where:
 * 00 = Object Orientation and
 * PIIE = Polymorphism / Inheritance / Instantiation / Encapsulation

Encapsulation
You noticed that we didn't have to use the dim statement for the attributes and we used the word  instead. What this means is that these attributes are not directly accessible once you have instantiated the class. Let's take our polo class as an example: In the example we access the fuel attribute of the polo class and give the car 100 units of fuel because fuel is declared as public, there are no restrictions in accessing it. However, when we try the following we run into trouble: The reason that this wouldn't work is because we have declared the maxSpeed attribute as private. If something is declared as private you can't access it externally, but how do you access it? The only way to access a private method or attribute is to use an interface, or public method. In the car code example we have: Because this method is public we can call it through:. And because setSpeed is declared inside the car object, it can have access to all the private attributes and methods.

We also need to find out the speed of a car to display to the user, we can do this by creating a get routine: Because this method is public we can call it through:. And because getSpeed is declared inside the car object, it can have access to all the private attributes and methods.

In general attributes should always be declared as private and only accessible through interfaces, using a setX command to give the private variable X a new value, and using the getX command to return the value of X. You should never directly access X!

Declare a colour attribute for the car that can only be accessed through an interface

Write an interface to set the colour

Write an interface to return the colour

Create an actor class with the following:
 * attributes: health, dexterity, x, y, strength
 * methods: walkforward, eat, gethit, displaystats, setHealth(p), setLocation(px,py)

For the actor class declared above instantiate:
 * Wizard called Barry with 100 Health starting at 4,5
 * Orc called Herbert with 35 Health starting at 20,2, then report on his status

Encapsulation is such a common thing that some languages have short cuts for making get and set routines. In VB.NET this involves using a : As we will never call  directly it doesn't matter that it has an underscore at the beginning. Using this code we can perform the following methods without having the fuel attribute as public:

Inheritance diagrams


What is the parent class of car in the above diagram? vehicle

Draw an inheritance diagram for the following classes:
 * motor bikes, bikes, vehicles, pedal bikes, trikes.



Draw an inheritance diagram for the following classes:
 * Monster, Character, Dragon, Hero, Orc