A class (or method) should have just one responsibility or task.
Open Closed Principle
Open for extension but closed for modification.
Liskov Substitution Principle
Likov's Substitution Principle states that if a program module is using a Base class, then the reference to the Base class can be replaced with a Derived class without affecting the functionality of the program module.
Interface Segregation Principle
Make fine grained interfaces that are client specific.
Dependency Inversion Principle
According to this principle the way of designing a class structure is to start from high level modules to the low level modules: High Level Classes --> Abstraction Layer --> Low Level Classes
- High-level modules should not depend on low-level modules. Both should depend on abstractions.
- Abstractions should not depend on details. Details should depend on abstractions.
we have to make such decision,
for deck part():_
1.deck is shuffled
2.card are dealt one at a time from the deck and added to the player's hand
for card parts:-
1.cards can be removed from hands
2.new card can be added
so basically it comes in our mind that class would be
game,
player,
hands,
card,
deck,
value and
suit
now here i am going to explain how to design some class according to our analytical knowledge;
let i am going to design class for say deck class and hands
for deck class :- the question comes whether it is shuffled or whether its dealt.
so here ,obviously method would be shuffle() and dealcard()
we will think for hands in same fashion.
that is for hands class ,we have to think about card is added or removed.
so clearly here method becomes addcard() and removecard()
now here when we are dealing with remove card then the question comes,how we are going to remove or add from deck of cards.
and according to our analytics we will have to add cardleft() type method for deck class
so it would be like
class deck{
private:
int a[52];
.....
.....
public:
shuffle();
dealcard();
cardleft();
}
and for hands class
class hand{
private:
.........
........
public:
addcards();
getcardcount();
rearrange();
removecard();
discard();
}
etc. etc.
2. Elevator design
A. Basic function of an elevator is to transport people/things from one level to another. For this purpose, it needs to know the following things:
1. Floor on which to go
2. Weight to carry is in limit
3. Current floor on which the elevator is.
Based on these we have the following class
Class Elevator
{
int floor_levels;
int weight_limit;
int current_floors;
public void Elevator(int floor_levelsval, int weight_limitval)
{
floor_levels = floor_levelsval;
weight_limit = weight_limitval;
}
public setCurrentfloor(int value)
{
current_floor = value;
}
}
Difference between Interface and Abstract Class
A:
Feature | Interface | Abstract class |
Multiple inheritance | A class may inherit several interfaces. | A class may inherit only one abstract class. |
Default implementation | An interface cannot provide any code, just the signature. | An abstract class can provide complete, default code and/or just the details that have to be overridden. |
| Access Modfiers | An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public | An abstract class can contain access modifiers for the subs, functions, properties |
Core VS Peripheral | Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface. | An abstract class defines the core identity of a class and there it is used for objects of the same type. |
Homogeneity | If various implementations only share method signatures then it is better to use Interfaces. | If various implementations are of the same kind and use common behaviour or status then abstract class is better to use. |
Speed | Requires more time to find the actual method in the corresponding classes. | Fast |
Adding functionality (Versioning) | If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. | If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly. |
| Fields and Constants | No fields can be defined in interfaces | An abstract class can have fields and constrants defined |