APM Welcome, APM North West Network Conference, Synergies Across Sectors
Review oop and ood
1. 1 ថនសារិ
Review OOP and OOD
I. QCM
CLASS
Class is a blueprint for an object. In fact, classes describe the type of objects, while objects are usable instances
of classes.
We use Class because:
They provide useful abstraction and structure
They help you think about the application you are building
Class name are nouns, not verbs
Good: Expense Claim, Employee, Printer
Bad: Ordering, Personnel, System
Don’t Overdo inheritance………
How We use Class:
A class is a user defined blueprint or prototype from which objects are created. It represents the set of
properties or methods that are common to all objects of one type. In general, class declarations can
include these components, in order:
Modifiers : A class can be public or has default access (Refer this for details).
Class name: The name should begin with a initial letter (capitalized by convention).
Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword
extends. A class can only extend (subclass) one parent.
Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded
by the keyword implements. A class can implement more than one interface.
Body: The class body surrounded by braces, { }.
Constructors are used for initializing new objects. Fields are variables that provides the state of the class
and its objects, and methods are used to implement the behavior of the class and its objects.
There are various types of classes that are used in real time applications such as nested
classes, anonymous classes, lambda expressions.
Abstract Class
Abstract Class are classes that contain one or more abstract methods.
We use Abstract Class Because:
An abstract class is a special kind of class that cannot be instantiated. So the question is why we need
a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other
words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it
enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all
the subclasses to carry on the same hierarchies or standards.
How we use Class:
An abstract class is a class that is declared abstract—it may or may not include abstract
methods. Abstract classes cannot be instantiated, but they can be subclassed.
An abstract method is a method that is declared without an implementation (without braces, and
followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
If a class includes abstract methods, then the class itself must be declared abstract, as in:
public abstract class GraphicObject {
// declare fields
// declare nonabstract methods
abstract void draw();
}
2. 2 ថនសារិ
When an abstract class is subclassed, the subclass usually provides implementations for all of the
abstract methods in its parent class. However, if it does not, then the subclass must also be
declared abstract.
Inheritance
inheritance is the concept that when a class of objects is defined, any subclass that is defined can inherit the
definitions of one or more general classes.
We use inheritance because:
Inheritance is a term for reusing code by a mechanism of passing down information and behavior from a
parent class to a child or sub class. It’s pretty easy to understand really. Just like a child can inherit various
mannerisms and behaviors from his or her biological parents, in software this same concept holds true. By
leveraging the power of inheritance and creating child classes that extend their parent, we can make sub
classes with super powers that have everything their parent has and more. Let’s take a look at how
inheritance works in PHP.
II. Question
Singleton Vs Factory Method
Singleton
A singleton pattern ensures that you always get back the same instance of
whatever type you are retrieving, whereas the factory pattern generally gives you a
different instance of each type.
The purpose of the singleton is where you want all calls to go through the same
instance. An example of this might be a class that manages a disk cache, or gets
data from a static dictionary; wherever it is important only one known instance
interacts with the resource. This does make it less scalable.
Factory Method:
Object creation without exposing in to client
Refer to newly created objects through interface
Define the interface for creating an object
The purpose of the factory is to create and return new instances. Often, these won’t
actually be the same type at all, but they will be implementations of the same base
class. However, there may be many instances of each type
Creational Vs Structural
Creational
These design patterns provide ways to create objects while hiding the creation logic, instead of
instantiating objects directly using the newoperator. This gives the program more flexibility in deciding
which objects need to be created for a given use case.
Structural
These design patterns deal with class and object composition. The concept of inheritance is used to
compose interfaces and define ways to compose objects to obtain new functionality.
Structural Vs Behavioral
Structural
Structural modelling refers to that type of modelling in which we simply interconnect the components by
mapping there ports by seeing the rtl diagram whereas
Behavioral
Behavioral modelling uses sequential statement inside the process statement ,in it we implement the logic
directly and this modelling is preferred and is the highest level of abstraction
3. 3 ថនសារិ
Strategy vs Command
Strategy
Strategy, in contrasst, is used to customize an algorithm. A strategy might have a number of methods
specific to the algorithm. Most often strategies will be instanciated immediately before executing the
algorithm, and discarded afterwards.
Command
Command encapsulates a single action. It therefore tends to have a single method with a rather generic
signature. It often is intended to be stored for a longer time and to be executed later - or it is used to
provide undo functionality (in which case it will have at least two methods, of course).
What is the benefit of
Singleton
Ensures only one instance of class is ever created
Can get access to data in this class from anywhere
Acts like global variables
Can cause methods to have unintended side-effects due to data sharing via state in the Singleton
Factory
Allows the sub-classes to choose the type of objects to create
Promoted the loose –coupling by eliminating the need to bind application-specific classes into the code
.That mean the code interacts solely with the resultant interface or abstract class,so that it will work
with any classes that implement that interface or that extends that abstract class.
Usage:
1. When a class doesn’t know what the sub-classes will be required to create
2. When a class want that it’s sub-classes specify the object to be created
3. When the parent class choose the creation of objects to its sub-classes
Strategy
Forces organizations to look ahead
Improved fit with the environment
Better use of resources
Provides a direction/vision
Helps monitor progress
Ensures goal congruence
Command
It decouples the classes that invoke the operation from the object that knows how to execute the
operation
It allow you to create a sequence of command by providing a queue system
Extensions to add a new command is easy and can be done without changing the existing code
You can also define a rollback system with the command pattern.