单例模式
//懒汉式单例模式
|
Sington1 sington11 = Sington1.getInstance(); |
//饿汉式 |
Sington2 sington21 = Sington2.getInstance(); |
简单工厂模式,生产水果,生产手机,生产玩具
|
public interface Product {
|
public class ProductFactory {
|
public class Fruit implements Product{
|
public class Phone implements Product{
|
public class Toy implements Product {
|
工厂方法模式
|
public interface Product1 {
|
public interface ProductFactory1 {
|
public class FruitFactory implements ProductFactory1{
|
public class PhoneFactory implements ProductFactory1 {
|
public class ToyFactory implements ProductFactory1{
|
public class Fruit implements Product1{
|
public class Phone implements Product1{
|
public class Toy implements Product1 {
|
public static void main(String[] args) {
|
public interface Product {
|
public abstract class ProductFactory {
|
public class DefualtFactory extends ProductFactory {
|
public class Fruit implements Product{
public class Toy implements Product {
|
public class Phone implements Product{
|
单例模式:
懒汉式:
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();
}
}

浙公网安备 33010602011771号