Design Patterns : Performance

  • use the flyweight pattern to reduce object creation [The flyweight pattern uses a factory instead of ‘new’ to reuse objects rather than always create new ones].
  • The Singleton pattern and the Flyweight (object factory) pattern are useful to limit numbers of objects of various types and to assist with object reuse and reduce garbage collection.
  • Use the Data Access Object pattern to decouple business logic from data access logic, allowing for optimizations to be made in how data is managed.
  • Use the Fast-Lane Reader pattern to accelerate read-only data access by not using enterprise beans.
  • Use the Front Controller pattern to centralize incoming client requests, allowing optimizations to be made in aggregating the resulting view.
  • Use the Front Controller pattern to channel all client requests through a single decision point, which allows the application to be balanced at runtime.
  • Use the Page-by-Page Iterator pattern to efficiently access a large, remote list by retrieving its elements one sublist of value objects at a time.
  • Use the Session Facade pattern to provide a unified, workflow-oriented interface to a set of enterprise beans, thus minimizing client calls to server EJBs.
  • Use a Session Facade to provide a simple interface to a complex subsystem of enterprise beans, and to reduce network communication requirements
  • Use the Value Object pattern to efficiently transfer remote, fine-grained data by sending a coarse-grained view of the data.
  • Use the factory pattern to enable reuse or cloning of objects
  • The Abstract Factory design pattern uses a single class to create more than one kind of object.
  • An alternative to the Flyweight pattern is the Prototype pattern, which allows polymorphic copies of existing objects. The Object.clone() method signature provides support for the Prototype pattern.
  • Prototypes are useful when object initialization is expensive, and you anticipate few variations on the initialization parameters. Then you could keep already-initialized objects in a table, and clone an existing object instead of expensively creating a new one from scratch.
  • Immutable objects can be returned directly when using Prototyping, avoiding the copying overhead.
  • Combine multiple remote calls for state information into one call using a value object to wrap the data (the Value Object pattern, superceded by local interfaces in EJB 2.0).
  • Where long lists of data are returned by queries, use the Page-by-Page Iterator pattern: a server-side object that holds data on the server and supplies batches of results to the client.
  • When the client would request many small data items which would require many remote calls to satisfy, combine the multiple calls into one call which results in a single Value Object which holds all the data required to be transferred. Use the Value Object to send a single coarse-grained object from the server to the client(s).
  • For read-only access to a set of data that does not change rapidly, use the Fast Lane Reader pattern which bypasses the EJBs and uses a (possibly non-transactional) data access object which encapsulates access to the data. Use the Fast Lane Reader to read data from the server and display all of them in one shot.
  • Wrap multiple entity beans in a session bean to change multiple EJB remote calls into one session bean remote call and several local calls (pattern called SessionFacade).
  • Cache EJBHome references to avoid JNDI lookup overhead (pattern called ServiceLocator).
  • The ServiceLocator/EJBHomeFactory Pattern reduces the expensive JNDI lookup process by caching EJBHome objects.
  • The SessionFacade Pattern reduces network calls by combining accesses to multiple Entity beans into one access to the facade object.
  • The MessageFacade/ServiceActivator Pattern moves method calls into a separate object which can execute asynchronously.
  • The ValueObject Pattern combines remote data into one serializable object, thus reducing the number of network transfers required to access multiple items of remote data.
  • The ValueObjectFactory/ValueObjectAssembler Pattern combines remote data from multiple remote objects into one serializable object, thus reducing the number of network transfers required to access multiple items of remote data.
  • The ValueListHandler Pattern: avoids using multiple Entity beans to access the database, using Data Access Objects which explicitly query the database; and returns the data to the client in batches (which can be terminated) rather than in one big chunk, according to the Page-by-Page Iterator pattern.
  • The CompositeEntity Pattern reduces the number of actual entity beans by wrapping multiple java objects (which could otherwise be Entity beans) into one Entity bean.
  • The Recycler pattern fixes only the broken parts of a failed object, to minimize the replacement cost.
  • Use a factory class instead of directly calling the “new” operator, to allow easier reuse of objects.
  • Instead of making lots of remote requests for data attributes of an object, combine the attributes into another object and send the object to the client. Then the attributes can be queried efficiently locally (this is called the Value Object pattern). Consider caching the value objects where appropriate.
  • The Proxy design pattern
    • Using a proxy, you can delay image loading until the image is required.
    • The Proxy pattern often instantiates its real object, the Decorator pattern (which can also use proxy objects) rarely does.
    • The java.lang.reflect package provides three classes to support the Proxy and Decorator patterns: Proxy, Method, and InvocationHandler.

Design Patterns : Drill

What is a design pattern.
————————————–
“Design patterns are recurring solutions to design problems you see over
Alpert, et. al., 1998).
“Design patterns constitute a set of rules describing how to accomplish
certain tasks in the realm of software development.” (Pree, 1994)

How many catogeries are there in Java patterns.
————————————————–
Creational patterns are ones that create objects for you, rather than
having you instantiate objects directly. This gives your program more
flexibility in deciding which objects need to be created for a given case.
Structural patterns help you compose groups of objects into larger
structures, such as complex user interfaces or accounting data.
Behavioral patterns help you define the communication between objects
in your system and how the flow is controlled in a complex program.

Design Patterns is a catalog of 23 generally useful patterns for writing objectoriented software.

Give me an example of Creational Patterns.
——————————————————
All of the creational patterns deal with the best way to create instances of objects. This is important because your program should not depend on how objects are created and arranged. In Java, of course, the simplest way to create an instance of an object is by using the new operator.

Sharat = new Sharat(); //instance of Fred class

However, this really amounts to hard coding how you create the object within your program. In many cases, the exact nature of the object that is created could vary with the needs of the program from time to time and abstracting the creation process into a special “creator” class can make your program more flexible and general.

The Factory Pattern is used to choose and return an instance of a class from a number of similar classes based on data you provide to the factory.
The Abstract Factory Pattern is used to return one of several groups of classes. In some cases it actually returns a Factory for that group of classes.
The Builder Pattern assembles a number of objects to make a new object, based on the data with which it is presented. Frequently, the choice of which way the objects are assembled is achieved using a Factory.
The Prototype Pattern copies or clones an existing class rather than creating a new instance when creating new instances is more expensive.
The Singleton Pattern is a pattern that insures there is one and only one instance of an object, and that it is possible to obtain global access to that one instance.

What is Singleton pattern. Where did you use it.
——————————————————
Definition: Singleton pattern is used whenever we require a class to have only one instance throughout the application. Sometimes it’s appropriate to have exactly one instance of a class: If we need some data to be stored , used and manipulated the same instance by the whole application, we will use singleton pattern. 

  • Ensure that only one instance of a class is created
  • Provide a global point of access to the object
  • Allow multiple instances in the future without affecting a singleton class’s clients

Examples:

  • window managers, print spoolers, and filesystems are prototypical examples.
  • Search window in Microsoft word, even if multiple documents are open, only one search window will be opened.  
  • One important usage of singleton pattern is when you need to instantiate a subclass and check which class to be instantiated during run time. For example there are two subclasses one for production and one for development and we need to instantiate one during run time depending on the environment.

Syntax:

public class ClassicSingleton {
private static ClassicSingleton instance = null;
protected ClassicSingleton() {
// Exists only to defeat instantiation.
}
public static ClassicSingleton getInstance() {
if(instance == null) {
instance = new ClassicSingleton();
}
return instance;
}
The ClassicSingleton class maintains a static reference to the lone singleton instance and returns that reference from the static getInstance() method.

public class SingletonInstantiator {
  public SingletonInstantiator() {
   ClassicSingleton instance = ClassicSingleton.getInstance();
ClassicSingleton anotherInstance =
new ClassicSingleton();
       …
  }
}
Explanation: In the first line we have created an instance of this class as null. We have declared a private constructor for the class to make sure that no object of class can be created outside the class.

getInstance() method is the entry point for the class which will return the only instance present for the class. This method is marked as static as it will be called directly from other classes. In this method we check if the instance of singletonExample is already present, if yes, return that instance else create the instance and return it. This makes sure that only one instance of the class is present at any point.

In the user class, that is calling our singleton class, we just need to call our singletonExample.getinstance() method which will return us the instance of singletonExample.

Types:

  • we create an instance of singleton class only when it is called for the first time is called lazy instantiation.
  • Another variation can be creating the instance before it is called, this kind of instantiation is called eager instantiation.

Problems:

  • A side effect of using singleton class: as the constructor of the singleton class is private, you cannot create a subclass of a singleton class, but in most of the cases that is desirable.
  • Additionally you can declare the class as final to explicitly show that the class can’t be subclassed. But in case if it is required to create a subclass of singleton class one can define the constructor of singleton class as protective instead of private.
  • Another point to note is that the singleton classes are not thread-safe.

Making your singleton class thread-safe is easy, you just have to make your getinstance method synchronized.

Public synchronized static singletonExample getInstance()
{
//if instance is null create else return old instance
}

What is Factory pattern. Explain it.
——————————————————

  • if we have a super class and n sub-classes, and based on data provided, we have to return the object of one of the sub-classes, we use a factory pattern.
  • provides a simple decision making class which returns one of several possible subclasses of an abstract base class depending on data it is provided.
  • Usually all of the classes it returns have a common parent class and common methods,but each of them performs a task differently and is optimized for differentkinds of data.

You should consider using a Factory pattern when
· A class can’t anticipate which kind of class of objects it must create.
· A class uses its subclasses to specify which objects it creates.
· You want to localize the knowledge of which class gets created.

Needs to isolate concrete classes from their super classes.

  • A system needs independent of how its products are created, composed, and represented.
  • Java Design Pattern

    1.What is a design pattern.
    2.How many catogeries are there in Java patterns.
    3. How many patterns did you use in your project.
    4.Give me an example of Creational Patterns.
    5.What is Singleton pattern. Where did you use it.
    6.How do you use Factory pattern.
    7.Can explaing examples of Structural Patterns.
    8.What is difference between adapter pattern and decorator pattern.
    9.What is Facade Pattern. Give an example.
    10.Did you use proxy pattern.
    11.Give me some examples of Behavioral Patterns.
    12.What is Observer Pattern.
    13.Do you know the difference between Command pattern and interpreter pattern.
    14.What is Visitor Pattern.