单例模式
public class danLi {
public static void main(String[] args) {
Hunger hunger01 = Hunger.getHunger();
Hunger hunger02 = Hunger.getHunger();
System.out.println(hunger01==hunger02);
}
}
class Hunger{
private static Hunger hunger=new Hunger();
private Hunger(){}
public static Hunger getHunger(){
return hunger;
}
}
class Lazy{
private static Lazy Lazy=null;
private Lazy(){}
public static Lazy getHunger(){
return Lazy;
}
}
简单工厂
public class Factory {
public static void main(String[] args) {
Factory.getCar("baoma").show();
}
public static Car getCar(String type){
if("baoma".equals(type)){
return new BaoMa();
}
if("benchi".equals(type)){
return new BenChi();
}
return null;
}
}
interface Car{
void show();
}
class BenChi implements Car{
@Override
public void show() {
System.out.println("我是奔驰");
}
}
class BaoMa implements Car{
@Override
public void show() {
System.out.println("我是宝马");
}
}
静态代理
public class StaticProxy {
public static void main(String[] args) {
HR hr = new HR(new Manager("王经理"));
hr.adduser();
}
}
interface AddUser{
void adduser();
}
class Manager implements AddUser{
private String name;
public Manager(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void adduser() {
System.out.println("轮到"+this.name+"去面试技术");
}
}
class HR implements AddUser{
private Manager manager=null;
public HR(Manager manager) {
this.manager = manager;
}
@Override
public void adduser() {
System.out.println("邀请笔试");
System.out.println("笔试通过");
System.out.println("邀请面试");
manager.adduser();
System.out.println("开始谈薪资");
}
}