单例模式

//懒汉式单例模式

public class Sington1 {
private static Sington1 instance;

private Sington1() {
}

public synchronized static Sington1 getInstance() {
if (instance == null) {
instance = new Sington1();
}
return instance;
}
}
Sington1 sington11 = Sington1.getInstance();
Sington1 sington12 = Sington1.getInstance();
System.out.println(sington11);
System.out.println(sington12);
//饿汉式
public class Sington2 {
private static Sington2 instance = new Sington2();

private Sington2() {
}

public static Sington2 getInstance() {
return instance;
}

}
Sington2 sington21 = Sington2.getInstance();
Sington2 sington22 = Sington2.getInstance();
System.out.println(sington21);
System.out.println(sington22);
简单工厂模式,生产水果,生产手机,生产玩具

Toy toy = (Toy)ProductFactory.getInstance("toy");
toy.printProducePrice();
Phone phone = (Phone)ProductFactory.getInstance("phone");
phone.printProducePrice();
public interface Product {
void printProducePrice();
}
 
public class ProductFactory {
static Product product = null;

public static Product getInstance(String name) {
switch (name) {
case "fruit":
product = new Fruit();
break;
case "phone":
product = new Phone();
break;
case "toy":
product = new Toy();
break;
}
return product;
}
}
 
 
public class Fruit implements Product{
@Override
public void printProducePrice() {
System.out.println("Fruit : 5");
}
}
 
public class Phone implements Product{
@Override
public void printProducePrice() {
System.out.println("Phone : 5000");
}
}
 
public class Toy implements Product {
@Override
public void printProducePrice() {
System.out.println("Toy : 50");
}
}
工厂方法模式

public class Demo {
public static void main(String[] args) {
ProductFactory1 factory = null;
factory = new FruitFactory();
Fruit fruit = (Fruit) factory.createProduct();
fruit.printProducePrice();
factory = new PhoneFactory();
Phone phone = (Phone) factory.createProduct();
phone.printProducePrice();
}
}
public interface Product1 {
void printProducePrice();
}
 
public interface ProductFactory1 {
Product1 createProduct();
}
 
 
public class FruitFactory implements ProductFactory1{

@Override
public Product1 createProduct() {
return new Fruit();
}
}
 
public class PhoneFactory implements ProductFactory1 {
@Override
public Product1 createProduct() {
return new Phone();
}
}
 
public class ToyFactory implements ProductFactory1{

@Override
public Product1 createProduct() {
return new Toy();
}
}
 
public class Fruit implements Product1{
@Override
public void printProducePrice() {
System.out.println("Fruit : 5");
}
}
 
public class Phone implements Product1{
@Override
public void printProducePrice() {
System.out.println("Phone : 5000");
}
}
 
public class Toy implements Product1 {
@Override
public void printProducePrice() {
System.out.println("Toy : 50");
}
}
public static void main(String[] args) {
DefualtFactory factory = new DefualtFactory();
Phone phone = factory.createPhone();
phone.printProducePrice();
Toy toy = factory.createToy();
toy.printProducePrice();
}
public interface Product {
void printProducePrice();
}
public abstract class ProductFactory {
public abstract Fruit createFriut();
public abstract Phone createPhone();
public abstract Toy createToy();
}
public class DefualtFactory extends ProductFactory {
@Override
public Fruit createFriut() {
return new Fruit();
}

@Override
public Phone createPhone() {
return new Phone();
}

@Override
public Toy createToy() {
return new Toy();
}
}
public class Fruit implements Product{
@Override
public void printProducePrice() {
System.out.println("Fruit : 5");
}
}


public class Toy implements Product {
@Override
public void printProducePrice() {
System.out.println("Toy : 50");
}
}
 
public class Phone implements Product{
@Override
public void printProducePrice() {
System.out.println("Phone : 5000");
}
}

 

单例模式:

懒汉式:

1. 定义类

public class Singleton {

private static Singleton singleton;

private Singleton(){}

public static Singleton getInstance(){

if(singleton ==null){

singleton = new Singleton();

}

return singleton;

}

}

2. Test

public class DemoForSingleton {

public static void main(String[] args) {

Singleton singleton1 = Singleton.getInstance();

System.out.println(singleton1);

Singleton singleton2 = Singleton.getInstance();

System.out.println(singleton2);

}

}

饿汉式模式:

1. 定义类

public class Singleton1 {

private Singleton1() {

}

 

private static final Singleton1 singleton1 = new Singleton1();

 

public static Singleton1 getInstance() {

return singleton1;

}

}

2. Test

public class DemoForSingleton1 {

public static void main(String[] args) {

Singleton1 s1 = Singleton1.getInstance();

Singleton1 s2 = Singleton1.getInstance();

System.out.println(s1);

System.out.println(s2);

}

}

 

 

 

 

代理模式:

1. 定义一个接口

 

2. 定义一个实现类

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3. 定义一个代理类

 

4. Test

 

 

 

 

 

 

 

 

 

 

 

策略模式:

1. 抽象策略类(Strategy

public interface IStrategy {

//早晨就read,中午lunch,下午shop

public void operate();

}

2. 定义三个实现类

public class StratedyRead implements IStrategy {

@Override

public void operate() {

// TODO Auto-generated method stub

System.out.println("read");

}

}

3. 环境类(Context)

public class MyContext {

private IStrategy strategy;

 

public IStrategy getStrategy() {

return strategy;

}

public void setStrategy(IStrategy strategy) {

this.strategy = strategy;

}

public void operate(){

strategy.operate();

}

}

4. Test

public class DemoForStrategy {

public static void main(String[] args) {

MyContext context = new MyContext();

Scanner sc = new Scanner(System.in);

int time = sc.nextInt();

if (time == 9) {

context.setStrategy(new StratedyRead());

context.operate();

} else if (time == 12) {

context.setStrategy(new StragedyLunch());

context.operate();

} else if (time == 15) {

context.setStrategy(new StrategyShop());

context.operate();

}

}

}

 

 

观察者模式

1. 定义接口观察者

public interface Watcher {

public void update();

}

2. 定义接口被观察者

public interface Watched {

public void addWatcher(Watcher watcher);

public void removeWathcer(Watcher watcher);

public void notifyWatchers();

}

3. 定义具体的观察者

public class Audience implements Watcher {

@Override

public void update() {

// TODO Auto-generated method stub

System.out.println("XXX出场了,观众鼓掌");

}

}

4. 定义具体的被观察者

public class Singer implements Watched{

private List<Watcher> watchers = new ArrayList<Watcher>();

 

@Override

public void addWatcher(Watcher watcher) {

// TODO Auto-generated method stub

watchers.add(watcher);

}

 

@Override

public void removeWathcer(Watcher watcher) {

// TODO Auto-generated method stub

watchers.remove(watcher);

}

 

@Override

public void notifyWatchers() {

// TODO Auto-generated method stub

for (Watcher watcher:watchers) {

watcher.update();

}

}

}

5. Test

public class DemoForWatcher {

public static void main(String[] args) {

Singer singer = new Singer();

Acommpany acommpany = new Acommpany();

Fans fans = new Fans();

Audience audience = new Audience();

 

singer.addWatcher(audience);

singer.addWatcher(fans);

singer.addWatcher(acommpany);

 

singer.notifyWatchers();

}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

装饰者模式:

1. 定义被装饰者

public interface Wall {

public void beautify();

}

2. 定义装饰者

public abstract class Decorator implements Wall {

private Wall wall;

 

public Decorator(Wall wall) {

this.wall = wall;

}

 

@Override

public void beautify() {

// TODO Auto-generated method stub

wall.beautify();

}

}

3. 定义第一种装饰

public class Decorator1 extends Decorator{

 

public Decorator1(Wall wall) {

super(wall);

// TODO Auto-generated constructor stub

}

public void brush(){

System.out.println("brush red");

 

}

@Override

public void beautify() {

// TODO Auto-generated method stub

super.beautify();

brush();

}

}

4. 定义第二种装饰

public class Decorator2 extends Decorator{

 

public Decorator2(Wall wall) {

super(wall);

// TODO Auto-generated constructor stub

}

public void pastePhoteFrame(){

System.out.println("paste a photo frame");

}

@Override

public void beautify() {

// TODO Auto-generated method stub

super.beautify();

pastePhoteFrame();

}

}

5. 定义被装饰者

public class MyWall implements Wall {

@Override

public void beautify() {

// TODO Auto-generated method stub

System.out.println("the white mywall");

}

}

6. Test

public class DemoForDecorator {

public static void main(String[] args) {

Wall wall = new MyWall();

Decorator decorator = new Decorator3(new Decorator2(new Decorator1(wall)));

decorator.beautify();

}

}

 

posted @ 2021-03-16 21:33  YangYuJia  阅读(64)  评论(0)    收藏  举报