Thursday, June 19, 2014

Java Weak Reference


Simple Note

From reference:
A weak reference, simply put, is a reference that isn't strong enough to force an object to remain in memory. Weak references allow you to leverage the garbage collector's ability to determine reachability for you, so you don't have to do it yourself.

Program

https://github.com/benbai123/JSP_Servlet_Practice/blob/master/Practice/JAVA/Commons/src/test/WeakReferenceTest.java

References:

Understanding Weak References

Sunday, June 15, 2014

Composite Pattern in Java


Simple Note

From reference:
the composite pattern is a partitioning design pattern. The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.

Program

https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JAVA/DesignPattern/src/composite

Run TestMain.java to test.

A Calculator probably consists of several child Calculators, you can change mode from parent Calculator then get result as needed.

References:

Composite pattern

Saturday, June 14, 2014

Bridge Pattern in Java


Simple Note

From reference:
The bridge pattern is a design pattern used in software engineering which is meant to "decouple an abstraction from its implementation so that the two can vary independently". The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities into different classes.
When a class varies often, the features of object-oriented programming become very useful because changes to a program's code can be made easily with minimal prior knowledge about the program. The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction. The bridge pattern can also be thought of as two layers of abstraction.

Difference between Bridge Pattern and Adapter Pattern:
See reference.

Program

https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JAVA/DesignPattern/src/bridge

Run TestMain.java to test.

Assume there are several calculator implements interface DesiredCalculator and you access them via a Bridge class rather than access them directly.
Once the API of DesiredCalculator is changed, you just need to update the content within Bridge class, all other parts of your program can stay unchanged.

References:

Bridge pattern

Difference between Bridge pattern and adapter pattern

Monday, June 9, 2014

Adapter Pattern in Java


Simple Note

From reference:
The adapter design pattern is used when you want two different classes with incompatible interfaces to work together. Interfaces may be incompatible but the inner functionality should suit the need. The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients.

Program

https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JAVA/DesignPattern/src/adapter

Run TestMain.java to test.

Assume you hope all calculators implement the interface DesiredCalculator but the PowerOfTwoCalc didn't, and assume it is provided by 3rd party in a jar.
Now you can use Adapter Pattern to create DesiredPowerOfTwoCalculator that implements DesiredCalculator and let PowerOfTwoCalc do real operation inside rather than rewrite all functions yourself.

Reference:

Adapter pattern

Object Pool Pattern in Java


Simple Note

From reference:
The object pool pattern is a software creational design pattern that uses a set of initialized objects kept ready to use – a "pool" – rather than allocating and destroying them on demand. A client of the pool will request an object from the pool and perform operations on the returned object. When the client has finished, it returns the object to the pool rather than destroying it; this can be done manually or automatically.

Program

https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JAVA/DesignPattern/src/objectpool

Run TestMain.java to test.

The ThreadPool create and handle WorkingThread to execute Runnable task,
and use Decorator Pattern to wrap Runnable task to restore WorkingThread automatically for reuse.

Reference:

Object pool pattern

Singleton Pattern in Java


Simple Note

From reference:
singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The term comes from the mathematical concept of a singleton.

Program

https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JAVA/DesignPattern/src/singleton

Run TestMain.java to test.

There is only one Config instance in whole application.

References:

Singleton pattern

Difference between static class and singleton pattern?

Why use a singleton instead of static methods?

Friday, June 6, 2014

Prototype Pattern in Java


Simple Note

From reference:
The prototype pattern is a creational design pattern in software development. It is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. This pattern is used to:
* avoid subclasses of an object creator in the client application, like the abstract factory pattern does.
* avoid the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) when it is prohibitively expensive for a given application.

Program

https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JAVA/DesignPattern/src/prototype

Run TestMain.java to test.

There are several default membership packages of a gym and you can always talk to salesmen to obtain a customized package that better for you.

Reference:

Prototype pattern

Design patterns in the test of time: Prototype