标签:设计模式
设计模式分类:
创建型:
单例模式(Singleton Pattern)
工厂方法模式(Factory Pattern)
抽象工厂模式(Abstract Factory)
建造者模式(Builder Pattern)
原型模式(Prototype Pattern)
结构型:
代理模式(Proxy)
装饰模式(Decorator)
适配器模式(Adapter)
组合模式(Composite)
桥梁模式(Bridge)
外观模式(Facade)
享元模式(Flyweight)
行为型:
模板方法模式(Template Method)
命令模式(Command)
责任链模式(Chain of Responsibility)
策略模式(Strategy)
迭代器模式(Iterator)
中介者模式(Mediator)
观察者模式(Observer)
备忘录模式(Memento)
访问者模式(Visitor)
状态模式(State)
解释器模式(Interpreter)
设计原则:
单一职责原则 Single Responsibility Principle
User.java
package com.DesignPattern.principle.srp; public class User { private String username; private String userpass; private int role; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpass() { return userpass; } public void setUserpass(String userpass) { this.userpass = userpass; } public int getRole() { return role; } public void setRole(int role) { this.role = role; } }BaseDao.java
package com.DesignPattern.principle.srp; public class BaseDao { }UserDao.java
package com.DesignPattern.principle.srp; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class UserDao extends BaseDao { //返回所有用户 public List<User> getAllUser() { List<User> userList = new ArrayList<User>(); String sql = "SELECT * FROM userdetail"; ResultSet rs = this.executeQuery(sql, null); try { while (rs.next()) { User user = new User(); user.setUsername(rs.getString("username")); user.setUserpass(rs.getString("userpass")); user.setRole(rs.getInt("role")); userList.add(user); } } catch (SQLException e) { e.printStackTrace(); } finally { this.closeAll(); } return userList; } //根据用户名查找用户 public User getUserByName(String name) { User user=null; String sql="SELECT * FROM userdetail WHERE username=?"; ResultSet rs=this.executeQuery(sql, new String[]{name}); try { if(rs.next()){ user=new User(); user.setUsername(rs.getString("username")); user.setUserpass(rs.getString("userpass")); user.setRole(rs.getInt("role")); } } catch (SQLException e) { e.printStackTrace(); }finally{ this.closeAll(); } return user; } //添加新用户 public boolean addUser(User user){ boolean r=false; String sql="INSERT INTO userdetail(username,userpass,role)values(?,?,?)"; int rs=this.executeUpdate(sql,new String[]{user.getUsername(),user.getUserpass(),""+user.getRole()}); if(rs>0){ r=true; } return r; } private int executeUpdate(String sql, String[] strings) { return 0; } private void closeAll() { } private ResultSet executeQuery(String sql, Object object) { return null; } }UserManager.java
package com.DesignPattern.principle.srp; public class UserManager { private UserDao userDao=new UserDao(); //验证用户名和密码 public boolean checkUser(String name,String pass){ boolean flag=false; User user=userDao.getUserByName(name); if(user!=null&&user.getUserpass().equals(pass)){ flag=true; } return flag; } //注册新用户 public void registUser(User user){ if(userDao.getUserByName(user.getUsername())!=null){ System.out.println("change username"); return; } if(userDao.addUser(user)){ System.out.println("success"); }else{ System.out.println("fail"); } } }里氏替换原则 Liskov Substitution Principle
Animal.java
package com.DesignPattern.principle.lsp; public abstract class Animal { public abstract void move(); }Horse.java
package com.DesignPattern.principle.lsp; public class Horse extends Animal { @Override public void move() { System.out.println("horse run"); } }Bird.java
package com.DesignPattern.principle.lsp; public class Bird extends Animal{ @Override public void move() { System.out.println("bird fly"); } }TestLSP.java
package com.DesignPattern.principle.lsp; public class TestLSP { public static void main(String[] args){ Animal animal; animal=new Horse(); animal.move(); animal=new Bird(); animal.move(); } }依赖倒置原则 Dependence Inversion Principle
IDriver.java
package com.DesignPattern.principle.dip; public interface IDriver { public void drive(ICar car); }Driver.java
package com.DesignPattern.principle.dip; public class Driver implements IDriver { @Override public void drive(ICar car) { car.run(); } }ICar.java
package com.DesignPattern.principle.dip; public interface ICar { public void run(); }Benz.java
package com.DesignPattern.principle.dip; public class Benz implements ICar{ public void run(){ System.out.println("Benz run"); } }BMW.java
package com.DesignPattern.principle.dip; public class BMW implements ICar{ @Override public void run() { System.out.println("BMW run..."); } }TestDIP.java
package com.DesignPattern.principle.dip; public class TestDIP { public static void main(String[] args){ IDriver tom=new Driver(); ICar car=new Benz(); tom.drive(car); car =new BMW(); tom.drive(car); } }接口隔离原则 Interface Segregation Principle
IOrderForPortal.java
package com.DesignPattern.principle.isp; public interface IOrderForPortal { public String getOrder(); }IOrderForOtherSys.java
package com.DesignPattern.principle.isp; public interface IOrderForOtherSys { public void insertOrder(); }IOrderForAdmin.java
package com.DesignPattern.principle.isp; public interface IOrderForAdmin{ public String getOrder(); public void insertOrder(); public void updateOrder(); public void deleteOrder(); }Order.java
package com.DesignPattern.principle.isp; public class Order implements IOrderForAdmin,IOrderForOtherSys,IOrderForPortal { public static Order getOrderFor(){ return new Order(); } // public static IOrderForOtherSys getOrderForOtherSys{ // return new Order(); // } // public static IOrderForAdmin getOrderForAdmin{ // return new Order(); // } @Override public String getOrder() { return "get Order"; } @Override public void insertOrder() { System.out.println("insert Order..."); } @Override public void updateOrder() { System.out.println("update Order..."); } @Override public void deleteOrder() { System.out.println("delete Order..."); } }TestISP.java
package com.DesignPattern.principle.isp; public class TestISP { public static void main(String[] args){ IOrderForPortal op=Order.getOrderFor(); System.out.println(op.getOrder()); IOrderForOtherSys os=Order.getOrderFor(); os.insertOrder(); IOrderForAdmin oa=Order.getOrderFor(); System.out.println(oa.getOrder()); oa.insertOrder(); oa.updateOrder(); oa.deleteOrder(); } }迪米特原则 Law of Demeter
Someone.java
package com.DesignPattern.principle.LoD; public class Someone { public void call(Friend friend){ friend.forward(); } }Friend.java
package com.DesignPattern.principle.LoD; public class Friend { private Stranger stranger=new Stranger(); public void forward(){ stranger.strangerMethod(); } public void friendMethod(){ System.out.println("this friend's Methoed"); } }Stranger.java
package com.DesignPattern.principle.LoD; public class Stranger { public void strangerMethod(){ System.out.println("this is stranger's method"); } }TestLod.java
package com.DesignPattern.principle.LoD; public class TestLoD { public static void main(String[] args){ Someone one =new Someone(); one.call(new Friend()); } }开闭原则 Open-Closed Principle
IBook.java
package com.DesignPattern.principle.ocp; public interface IBook { public String getName(); public double getPrice(); public String getAuthor(); }NovelBook.java
package com.DesignPattern.principle.ocp; public class NovelBook implements IBook { private String name; private double price; private String author; public NovelBook(String name, double price, String author) { super(); this.name = name; this.price = price; this.author = author; } @Override public String getName() { return this.name; } @Override public double getPrice() { return this.price; } @Override public String getAuthor() { return this.author; } }BookStore.java
package com.DesignPattern.principle.ocp; import java.util.ArrayList; public class BookStore { private ArrayList<IBook> bookList=new ArrayList<IBook>(); public BookStore(){ bookList.add(new NovelBook("xiyouji",99,"wuchenen")); bookList.add(new NovelBook("shuihu",99,"shishi")); } public void showBooks(){ System.out.println("--------------------bookList-------------------"); System.out.println("bookname\t\tprice\t\tauthor"); for(IBook book:bookList){ System.out.println(book.getName()+"\t\t¥"+book.getPrice()+"\t\t"+book.getAuthor()); } } public static void main(String[] args){ BookStore bookStore=new BookStore(); bookStore.showBooks(); } }
版权声明:本文为博主原创文章,未经博主允许不得转载|Copyright ©2011-2015,Supernatural, All Rights Reserved.
DesignPattern_Java:设计模式分类和设计原则
标签:设计模式
原文地址:http://blog.csdn.net/williamfan21c/article/details/47791689