OOD 小总结

Singleton object:

只能生成一个instance, 就叫singleton object

public class CSingleton
{
   private static CSingleton instance;
   private CSingleton(){} 

   public static CSingleton getInstance(){
       if(instance== null){
          instance= new CSingleton();
       }
      return instance;
   }
};

调用时:

public class SingletonPatternDemo {
   public static void main(String[] args) {
      //Get the only object available
      CSingleton object = CSingleton.getInstance();
   }
}

下面是Singleton与static class的区别。
Singleton object stores in Heap but, static object stores in Stack
We can clone the object of Singleton but, we can not clone the static class object
Singleton class follow the OOP(object oriented principles) but not static class
we can implement interface with Singleton class but not with Static class.

其中什么是OOP:
https://chesterli0130.wordpress.com/2012/10/04/four-major-principles-of-object-oriented-programming-oop/

JAVA static变量:
记:static method 和 static variable 都不用创建 instance,可以类名直接引用。
http://crunchify.com/java-static-methods-variables-static-block-and-class-with-example/

http://blog.csdn.net/zhandoushi1982/article/details/8453522
static的好处是:全局唯一,一改都改。同时引用方便。
static final:全局固定常量

工厂模式:
http://blog.csdn.net/jason0539/article/details/23020989

使用不同factory to create 不同的class,product。Client just need to create the instance to factory. 不用考虑具体product是如何create,生产的。

产品:

abstract class BMW {  
    public BMW(){  
          
    }  
}  
public class BMW320 extends BMW {  
    public BMW320() {  
        System.out.println("制造-->BMW320");  
    }  
}  
public class BMW523 extends BMW{  
    public BMW523(){  
        System.out.println("制造-->BMW523");  
    }  
}  

工厂:

interface FactoryBMW {  
    BMW createBMW();  
}  
  
public class FactoryBMW320 implements FactoryBMW{  
  
    @Override  
    public BMW320 createBMW() {  
  
        return new BMW320();  
    }  
  
}  
public class FactoryBMW523 implements FactoryBMW {  
    @Override  
    public BMW523 createBMW() {  
  
        return new BMW523();  
    }  
}  

Client:

public class Customer {  
    public static void main(String[] args) {  
        FactoryBMW320 factoryBMW320 = new FactoryBMW320();  
        BMW320 bmw320 = factoryBMW320.createBMW();  
  
        FactoryBMW523 factoryBMW523 = new FactoryBMW523();  
        BMW523 bmw523 = factoryBMW523.createBMW();  
    }  
}  
State Machine Design Pattern

Similar to the State design pattern, a class behavior changes based on its state. And State Machine is the same as an intent of State: to make it possible for an object to alter its behavior when its internal state changes. It contains three main classes: State class, Event class, and Context class.

Context classes implements transition logic, and the next state is done by notifying the Context class with an event. State class transits to other state by passing Event to Context.

SNAKE原则

Scenario: case/interface
Necessary: constrain/hypothesis
Application: service/algorithm
Kilobit: Data
Evolve:

比如,design a FM Radio

Steps 1: Enumerate use cases,
Register/Login, Play Music, Music recommendation. 并说明,最重要的feature是什么。

Steps 2: Sort
Priority 1. Play Music
get channels, select channel, play music in this channel

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,917评论 0 23
  • 2012年6月,美国科学家首次提出绘制大脑活动图谱计划,研究人员在《科学》杂志在线版阐述了该计划。未来15年,利...
    Stan森阅读 387评论 0 0
  • 最近忙着背书,背书是为了职称,职称看起来遥遥无期。所以美好的假期显得前路迷茫。我不善言辞,尤其在众多人的表现中。 ...
    mylkevin阅读 185评论 0 0
  • 文图/猫小慧 吃,我生命中最擅长的事! 一生下来,对于吃我自学成才!长大一点,自己买着吃!上班后,更是大吃特吃!南...
    猫小慧阅读 268评论 1 1
  • 白云,飘逸而洒脱,在湛蓝的天空下,自由自在地来去。 常常独自一个人站在田野,痴痴地地遥望白云, 并静静地想:那圣洁...
    王虎林阅读 191评论 0 1