Friday, May 24, 2019

Liskove Sustituion

The Liskov Substitution Principle states that any class that is the child of a parent class should be usable in place of its parent without any unexpected behaviour.

If S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program
One rule that should be abided by in PHP is that any method should return the same type as that of its parent. For example, if the search method of T returns an instance of Illuminate\\Support\\Collection then any search method of S should also return an Illuminate\\Support\\Collection.

The four conditions for abiding by the Liskov Substitution principle are as follows:

Method signatures must match
Methods must take the same parameters
The preconditions for any method can’t be greater than that of its parent
Any inherited method should not have more conditionals that change the return of that method, such as throwing an Exception
Post conditions must be at least equal to that of its parent
Inherited methods should return the same type as that of its parent
Exception types must match
If a method is designed to return a FileNotFoundException in the event of an error, the same condition in the inherited method must return a FileNotFoundException too
Practical Example(s)
As PHP does not enforce the return type of a method (unless explicitly stated), we must ensure that we adhere to the documentation and return the same types that the parent class or interface define.

As most of the SOLID Principles overlap, one of the best things that you can do to ensure that you follow the Liskov Substitution Principle is code to an interface. Rely on abstractions rather than concretions.

Abiding by the LSP
Below is a basic example of a Repository following the Liskov Substitution Principle:

interface LessonRepositoryInterface
{
/**
* Gets all lessons.
*
* @return array
*/
public function getAll();
}
class FilesystemLessonRepository implements LessonRepositoryInterface
{
public function getAll()
{
// Fetch the lessons from the filesystem
return $files;
}
}
class DatabaseLessonRepository implements LessonRepositoryInterface
{
public function getAll()
{
return Lesson::all()->toArray();
}
}

As you can see from the code above, both classes implement the LessonRepositoryInterface by having a getAll method. While this code would compile normally, we have ensured that the Eloquent Model returns an array of the results so that it follows the documentation correctly.

Conclusion
The Liskov Substitution Principle means that you can inherit from a base class as long as you conform to the standards that it sets, such as having the same method name and parameters, you do not specify any deeper conditions that must be fulfilled, you return the same type that the base method does and that any Execption that is thrown must match the ones thrown by the base method.

Tuesday, May 21, 2019

Interview MVC

Limitations of TPL in comparision of Thread


Advantages: Declaritive way to write concurrent processes w/o having to deal with the lower level concurrency contructs Easily convert existing sequential code to concurrent code by simply converting to parallel loops and plinq The PFX framework makes it very easy to scale your app to more powerfull hardware without you having to change any code. (the system will automatically create a thread per core) Lowers the risk of screwing your code up with a small concurrency errors Limitations: PFX will run your code in parallel even if the work load is very small. This will usually cause the program to run slower than a regular loop or query would. You do not have full control over the concurrency of your code when using plinq or the static Parallel class (I think this is good, it relates to what I wrote above about not screwing your code up) Its pre-release, it may have bogs and it may be refactored before its released PFX only works with .Net 3.5, you can't use it on shared hosting that hasn't upgraded yet Soruce

What is template pattern?
What need to do so that a class load as soon as program load, without accessing it.
Ans : Make it static
If you are implementing Liskov susbtitution and you have not parnent class with you, than how will you do that.

Followers

Link