Big talk design mode 9 Singleton mode Bridging Mode

Source: Internet
Author: User

1. Singleton Mode

As the name suggests, a class can only have one instantiated object pattern. The Singleton mode is divided into lazy and ELE. Me Based on the morning and evening of Object Instantiation.

If you don't call it, you don't need to instantiate the object...

/*** Lazy **/class Singleton {Private Static Singleton minstance; private Singleton () {} public static Singleton getinstance () {If (null = minstance) {minstance = new Singleton ();} return minstance ;}}

Ele. Me, because of hunger, can't wait to instantiate objects when defining them.

/*** Hunger style **/class hungrysingleton {Private Static hungrysingleton minstance = new hungrysingleton (); Private hungrysingleton () {} public static hungrysingleton getinstance () {return minstance ;}}

In the case of multiple threads, the lazy style requires Double Locking to prevent repeated instantiation.

The first null = minstance is easy to understand. When it is not instantiated, synchronous judgment is added and instantiated.

The second null = minstance is used to prevent multiple threads from waiting outside the synchronization lock when calling at the same time. As a result, a thread has been instantiated and other threads have been instantiated.

/*** Double lock in multi-thread situations **/class singletonmultithread {Private Static singletonmultithread minstance; Private Static byte [] mlock = new byte [0]; private singletonmultithread () {} public static singletonmultithread getinstance () {If (null = minstance) {synchronized (mlock) {If (null = minstance) {minstance = new singletonmultithread ();}}} return minstance ;}}

I remember reading an article written by an oracle user and analyzed it.The dual lock mechanism is actually unreliable....

So... OrUse the hungry Chinese style.Without these troubles...


2. Bridging Mode

The name of the Bridge Mode seems to be because the UML diagram looks like a bridge...

In fact, the Bridge Mode means that when the system implements classification from multiple angles, we try to separate different classifications as much as possible, because they change independently and reduce coupling.

The bridge mode makes full use of the principle of combined aggregation reuse, namely:Use combination aggregation as much as possible, rather than inheritance. inheritance is considered only when there is a strong "is a" relationship..

A poor design that uses inheritance.

abstract class CellPhoneBrand{public abstract void run();}class CellPhoneBrandH extends CellPhoneBrand{public void run(){System.out.println("HTC");}}class CellPhoneBrandS extends CellPhoneBrand{public void run(){System.out.println("SAMSUNG");}}class CellPhoneBrandHGame extends CellPhoneBrandH{public void run(){super.run();System.out.println("Play game");}}class CellPhoneBrandSGame extends CellPhoneBrandS{public void run(){super.run();System.out.println("Play game");}}class CellPhoneBrandHMusic extends CellPhoneBrandH{public void run(){super.run();System.out.println("Play music");}}class CellPhoneBrandSMusic extends CellPhoneBrandS{public void run(){super.run();System.out.println("Play music");}}public class BridgePattern {public static void main(String[] args) {// TODO Auto-generated method stubCellPhoneBrand c;c=new CellPhoneBrandHGame();c.run();c=new CellPhoneBrandSGame();c.run();c=new CellPhoneBrandHMusic();c.run();c=new CellPhoneBrandSMusic();c.run();}}

Improved Bridging Mode

abstract class CellPhoneSoftware{public abstract void run(); }class CellPhoneGame extends CellPhoneSoftware{public void run(){System.out.println("Play game");}}class CellPhoneMusic extends CellPhoneSoftware{public void run(){System.out.println("Play music");}}abstract class CellPhoneBrand{protected CellPhoneSoftware mCellPhoneSoftware;public void setSoftware(CellPhoneSoftware cellPhoneSoftware){mCellPhoneSoftware=cellPhoneSoftware;}public abstract void run();}class HTC extends CellPhoneBrand{public void run(){System.out.println("HTC");if(null!=mCellPhoneSoftware)mCellPhoneSoftware.run();}}class SAMSUNG extends CellPhoneBrand{public void run(){System.out.println("SAMSUNG");if(null!=mCellPhoneSoftware)mCellPhoneSoftware.run();}}public class BridgePatternBetter {public static void main(String[] args) {// TODO Auto-generated method stubCellPhoneBrand c;c=new HTC();c.setSoftware(new CellPhoneMusic());c.run();c=new SAMSUNG();c.setSoftware(new CellPhoneGame());c.run();}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.