What are the Gang Of Four Principles?
- Program to an interface not an implementation
- Favor object composition over inheritance
What are the different types of design patterns?
- Creational Patterns
- Structural Patterns
- Behavioral Patterns
- J2EE Patterns
What is Composition?
- Composition refers to combining simple types to make more complex ones.
- Composition is a "has a" relationship (Can't exist on it's own). Inheritance is a "is a " relationship.
- Composition is about expressing relationships between objects. Think about the chair example. A chair has a Seat. A chairhas a back. And a chair has a set of legs. The phrase "has a" implies a relationship where the chair owns, or at minimum, uses, another object. It is this "has a" relationship which is the basis for composition.
Why use Composition over Inheritance?
- Promotes smaller class size
- Inheritance can potentially cause method signature collisions when implementing multiple interfaces. This will never happen in Composition.
- It provides flexibility in invocation of methods.
- Another problem with inheritance is that we are exposing all the superclass methods to the client and if our superclass is not properly designed and there are security holes,
What is a factory pattern?
- Creates objects without exposing the instantiation logic to the client and Refers to the newly created object through a common interface.
- Example: Car Factory that produces different types of cars
What is a Builder Pattern?
Defines an instance for creating an object but letting subclasses decide which class to instantiate and allows a finer control over the construction process.
Example: building a Soldier object that’s made of a person, training, uniform, etc…
Name the Creational Design Patterns
- Factory Method
- Abstract Factory
- Object Pool
Name the Behavioral Design Patterns
- Chain of Responsibility
- Template Method
- Null Object
Name the Structural Design Patterns
What is a Proxy Design Pattern?
- Provides a “Placeholder” for an object to control references to it.
- Example: Hibernate
What is an Adapter Pattern?
- Converts the interface of a class into another interface clients expect. Adapter lets classes work together, that could not otherwise because of incompatible interfaces.
- Example: An audio player device can play mp3 files only and wants to use an advanced audio player capable of playing vlc and mp4 files. Adapter would be between audio player and files.
What is an Observer Pattern?
- Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
- Example: Messaging Queue
What is an Iterator Pattern?
Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation
What is the Chain of Responsibility Pattern?
- It avoids attaching the sender of a request to its receiver, giving this way other objects the possibility of handling the request too.
- The objects become parts of a chain and the request is sent from one object to another across the chain until one of the objects will handle it.
- Example: MVC stack with the different filter and interceptors. Will have a next handler
What is the Prototype Pattern?
- Specifies the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
- Example: Hibernate’s Ehcache – keep a hashtable in the class, initialize the table in the constructor then return objects from hashtable when retrieved by an id.
What is a Facade Pattern?
- Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system.
- Example: Webservices
What is Cohesion?
- Cohesion refers to what the class (or module) will do.
- Low cohesion would mean that the class does a great variety of actions and is not focused on what it should do.
- High cohesion would then mean that the class is focused on what it should be doing, i.e. only methods relating to the intention of the class.
What is Coupling?
- Coupling refers to how related are two classes / modules and how dependent they are on each other.
- Low coupling means that changing something major in one class should not affect the other.
- High coupling would make your code difficult to make changes as well as to maintain it, as classes are coupled closely together, making a change could mean an entire system revamp.
- All good software design will go for high cohesion and low coupling.
What is an Abstract Factory Pattern
- Abstract Factory patterns work around a super-factory which creates other factories. This factory is also called as factory of factories.
- Example: Car Factory and Tire Factory being referenced by a single class
What is a Singleton Pattern
- This pattern involves a single class which is responsible to create an object while making sure that only single object gets created. This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class.
- Example: Spring beans
What is a Business Delegate Pattern?
- Business Delegate Pattern is used to decouple presentation tier and business tier. It is basically use to reduce communication or remote lookup functionality to business tier code in presentation tier code. In business tier we have following entities.
- Client- Presentation tier code may be JSP, servlet or UI java code.
- Business Delegate- A single entry point class for client entities to provide access to Business Service methods.
- LookUp Service- Lookup service object is responsible to get relative business implementation and provide business object access to business delegate object.
- Business Service- Business Service interface. Concrete classes implement this business service to provide actual business implementation logic.
What is a Service Locator Pattern?
- The service locator design pattern is used when we want to locate various services using JNDI lookup. Considering high cost of looking up JNDI for a service, Service Locator pattern makes use of caching technique. For the first time a service is required, Service Locator looks up in JNDI and caches the service object. Further lookup or same service via Service Locator is done in its cache which improves the performance of application to great extent. Following are the entities of this type of design pattern.
- Service- Actual Service which will process the request. Reference of such service is to be looked upon in JNDI server.
- Context / Initial Context- JNDI Context carries the reference to service used for lookup purpose.
- Service Locator- Service Locator is a single point of contact to get services by JNDI lookup caching the services.
- Cache- Cache to store references of services to reuse them Client- Client is the object that invokes the services via ServiceLocator.
What is Reflection?
- Reflection objects are used for obtaining type information at runtime. The classes that give access to the metadata of a running program are in theSystem.Reflection namespace.
- Reflection has the following applications:
- It allows view attribute information at runtime.
- It allows examining various types in an assembly and instantiate these types.
- It allows late binding to methods and properties
- It allows creating new types at runtime and then performs some tasks using those types.
What is Encapsulation?
Encapsulation conceals the functional details of a class from objects that send messages to it.
What is Inheritance?
‘Subclasses’ are more specialized versions of a class, which inherit attributes and behaviors from their parent classes, and can introduce their own.
What is Abstraction?
Abstraction is simplifying complex reality by modelling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.
What is Polymorphism?
- Polymorphism in simple terms means one name many forms. Polymorphism enables one entity to be used as a general category for different types of actions. The specific action is determined by the exact nature of the situation.
- 2 Forms of Polymorphism:
- Overriding a method: When you define a method is a subclass with the same signature as the method superclass. For example: toString() method in the class Object, normally you override this method in your own class and put something meaningful in it.
- Overloading a method: When you define a method in a class with different signatures. For example: a function that takes an int and another that takes a double.
What are the differences between abstract class and an interface?
- There are multiple differences between abstract class and interface in Java, but the most important one is Java's restriction on allowing a class to extend just one class but allows it to implement multiple interfaces. An abstract class is good to define default behavior for a family of class, but the interface is good to define Type which is later used to leverage Polymorphism.
- Abstract class is a class. Interface is a type.
- Abstract class can have protected and public members. Interface only public.
- Abstract class can have concrete and abstract methods and subclasses do not have to implement all methods. Interfaces can only have public members and all methods are abstract and must be implemented.
What is Aggregation?
There is a closely related concept to composition called aggregation. They are both a "HAS A" relationship, but with aggregation the object does not own the inherited object. The inherited object can exist without the object.
What does SOLID stand for in OOP?
- S – Single-responsiblity principle: A class should have one and only one reason to change, meaning that a class should have only one job.
- O – Open-closed principle: Objects or entities should be open for extension, but closed for modification.
- L – Liskov substitution principle: Specifies that functions that use pointers of references to base classes must be able to use objects of derived classes without knowing it.
- I – Interface segregation principle: A client should never be forced to implement an interface that it doesn’t use or clients shouldn’t be forced to depend on methods they do not use.
- D – Dependency Inversion Principle: Entities must depend on abstractions not on concretions. It states that the high level module must not depend on the low level module, but they should depend on abstractions.
What is a thread?
A thread is the path followed when executing a program. It's a single sequential flow of control within a program.
What are 3 reasons for multithreading an application?
- Execution Concurrency to improve compute performance: If you have a problem that can be broken down into pieces and you also have more than one execution unit (processor core) available then dispatching the pieces into separate threads is the path to being able to simultaneously use two or more cores at once.
- Concurrency of CPU and IO Operations:
- This is similar in thinking to the first one but in this case the objective is to keep the CPU busy AND also IO operations (ie: disk I/O) moving in parallel rather than alternating between them.
- Program Design and Responsiveness: Many types of programs can take advantage of threading as a program design benefit to make the program more responsive to the user. For example the program can be interacting via the GUI and also doing something in the background.
What is a Bridge pattern?
- Conceptually, you push the implementation details used by a class hierarchy into another object, typically with its own hierarchy. By doing so, you are removing a tight dependency on those implementation details and allowing the details of that implementation to change.
- Decouple an abstraction from its implementation so that the two can vary independently