Lunski's Clutter

This is a place to put my clutters, no matter you like it or not, welcome here.

0%

Interface and Abstract Class

Summon Chimera。

img

It looks like lion, but has wings like bird, yelling like goat and it can spit out fire from mouth like dragon.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Lion
growl
speed 5

Pig
wee
speed 1

Dragon
is legend: true
fire level: 5

Chimera
has wings: true
can fly: true
bleat
speed 5
is legend: true
fire level: 5

bleat
bleat
bleat
bleat
bleat
bleat
bleat
bleat
bleat
bleat
bleat
bleat
bleat
bleat
bleat
bleat
bleat
bleat
bleat
bleat

Process finished with exit code 0

Interfaces v.s. Abstract Classes

  • Interface: Define a set of methods that a class must implement.

  • Abstract Class: Provide a common implementation to several related classes.

Both create abstract types that cannot be instantiated directly.

  1. Interface can be implemented, abstract class can’t.

  2. Interface don’t have implementation when it declared, but abstract class can have abstract and concrete methods.

  3. We can implement multiple interface, but only inherit one class.

  4. Interface can’t have constructor, but class can.

  5. Interface only implements another interface not abstrace class, but abstract class can implements interface.

1
2
3
4
5
6
7
8
9
10
11
12
13
public abstract class Animal {
private String name;

public Animal(String name) {
this.name = name;
}

public abstract void makeSound();

public String getName() {
return name;
}
}

Use Singleton for Chimera

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
* Eagerly way rely on JVM, but bad for multi-thread

private static Chimera uniqueInstance = new Chimera(3);

private Chimera(int level) {super(level);}

public static Chimera getInstance() {
return uniqueInstance;
}

* Double lock Chimera reduce synchronized, but suitable for Java 5 above

/*
private and static for prevent public calling
and allocate specific memory location

volatile means the working memory of each thread is
not used, and access, reading and writing are always
done from the main memory.
*/
private volatile static Chimera uniqueInstance;

private Chimera(int level) {super(level);}

public static Chimera getInstance() {
// if not instantized, into synchronized block
if (uniqueInstance == null){
synchronized (Chimera.class){ // use synchronized good for multithread, but lower performance
// check again in synchronized block
if(uniqueInstance == null)
uniqueInstance = new Chimera(3);
}
}
return uniqueInstance;
}

* Main

Chimera chimera = Chimera.getInstance();

Lambda + runnable is more flexible and support multi-core cpu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
* Chimera

@Override
public void Sound() {
System.out.println("growl");
}

public void run(Runnable action){
for(int i= 0; i< 20; i++){
action.run();
}
}



* Main

chimera.run(()-> {chimera.Sound();});

1. Replace statement lambda with expression lambda

chimera.run(()-> chimera.Sound()); // remove {;}

2. Replace lambda with method reference

chimera.run(chimera::Sound); // use Class::Method

Code

PanltUML

img

File-> Settings-> Plugins-> Browse repositories-> panltUML-> Install plugin

img

PlantUML File

Graphviz

img

$ sudo rm -rf /Library/Developer/CommandLineTools
$ sudo xcode-select –switch /Applications/Xcode.app
$ /bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
$ brew update
$ brew install graphviz

img

Improvement(Strategy Pattern)

Public class Pig{
SoundBehavior soundBehavior;

public void performSound(){  
    soundBehavior.makeSound();  }}  

if a class extends Pig, they have pig’s behavior

  • Dynamic method

public void setSoundBehavior(SoundBehavior sb){
soundBehavior = sb;}

how to use…
setSoundBehavior(new Oink());

Head First Design Patterns


如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Welcome to my other publishing channels