http://stackoverflow.com/questions/1001767/what-is-the-basic-difference-between-the-factory-and-abstract-factory-patterns

블로그 이미지

낭만가을

,

CI 툴을 사용하고자 Jenkins 사용법에 대해서 알아보자.

빌드 방법은 maven을 이용해 war 생성 후 tomcat manager를 통해 war를 업로드 하는 식이다.

1. 

먼저 Jenkins를 설치한 후 웹 사이트에 접속한다.

2. 

시스템 설정

Jenkins 관리 > 시스템 설정 메뉴 접속 후 다음과 같이 JDK, Maven, 이메일, SVN 버전 설정을 한다.

이메일 서버는 gmail을 이용하였다.

3. 

새로운 Job을 클릭

Job 이름 입력 후 Build a maven2/3 project 라디오 버튼 클릭

4. 

소스 코드 관리 설정은 다음과 같이 한다.

나는 naver SVN 저장소를 이용했다. 사실 우리 회사 SVN 서버가 가끔씩 말썽을 일으키는 경우가 있는데 회사 프로젝트도 네이버의 SVN 서버를 이용하여 형상 관리를 하면 좋을 것 같다는 생각이 든다.

5.

빌드 트리거 설정

매일 매시 49분에 자동 빌드가 되도록 설정


6.

빌드 설정

maven Root POM 지정 후 Goals을 넣어준다.

clean 으로 target 디렉토리를 정리한 후 install을 이용하여 war를 생성한다.


7.

Post-build Actions 지정

톰켓의 webapps 디렉토리에 war를 배포하기 위한 설정이다. (Deploy Plugin을 설치해야 war를 tomcat으로 배포할 수 있다.)

여기서 manager 아이디와 패스워드는 TOMCAT_HOME/conf/tomcat-users.xml 파일에서 셋팅한다.

<role rolename=”tomcat”/>

<role rolename=”role1″/>

<role rolename=”manager”/>

  

<user username=”tomcat” password=”tomcat” roles=”tomcat”/>

<user username=”both” password=”tomcat” roles=”tomcat,role1″/>

<user username=”role1″ password=”tomcat” roles=”role1″/>

<user username=”lng1982″ password=”********” roles=”manager”/>




기본적으로 위와 같이 설정하면 Jenkins를 이용하여 지속적인 통합을 할 수 있다.

Jenkins 프로젝트 job 히스토리 삭제 방법



1. ${JENKINS_HOME}/jobs/프로젝트명/builds 이동

2. 삭제하고자 하는 히스토리 디렉토리를 삭제

3. Jenkins 관리 메뉴에서 Reload Configuration from Disk 선택

SSH를 이용한 배포


위에서 설명한 tomcat manager를 통한 배포 방식 외에 SSH를 이용하여 배포하는 방법도 있다.

먼저 jenkins 플러그인 화면으로 이동 후 “Publish Over SSH”를 설치한다.

Job > 설정으로 이동 후 Post-build Actions에 다음과 같이 설정하면 끝

실제 war 파일을 SSH 프로토콜을 이용하여 서버에 업로드 하는 것이다.

FTP를 이용한 배포


FTP를 이용한 배포를 위해선 “Publish Over FTP” 플러그인을 설치 후 Jenkins 관리 페이지 > 시스템 설정 페이지로 이동한다.

Publish over FTP 에 다음과 같이 설정

적용하고자 하는 프로젝트의 Job > 설정 페이지 이동 후 업로드 하고자 하는 파일에 대한 정보를 입력하면 끝


블로그 이미지

낭만가을

,

http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns-in-javas-core-libraries





Creational patterns

Abstract factory (recognizeable by creational methods returning the factory itself which in turn can be used to create another abstract/interface type)

Builder (recognizeable by creational methods returning the instance itself)

Factory method (recognizeable by creational methods returning an implementation of an abstract/interface type)

Prototype (recognizeable by creational methods returning a different instance of itself with the same properties)

Singleton (recognizeable by creational methods returning the same instance (usually of itself) everytime)


Structural patterns

Adapter (recognizeable by creational methods taking an instance of different abstract/interface type and returning an implementation of own/another abstract/interface type which decorates/overrides the given instance)

Bridge (recognizeable by creational methods taking an instance of different abstract/interface type and returning an implementation of own abstract/interface type which delegates/uses the given instance)

  • None comes to mind yet. A fictive example would be new LinkedHashMap(LinkedHashSet<K>, List<V>) which returns an unmodifiable linked map which doesn't clone the items, but usesthem. The java.util.Collections#newSetFromMap() and singletonXXX() methods however comes close.

Composite (recognizeable by behavioral methods taking an instance of same abstract/interface type into a tree structure)

Decorator (recognizeable by creational methods taking an instance of same abstract/interface type which adds additional behaviour)

Facade (recognizeable by behavioral methods which internally uses instances of different independent abstract/interface types)

Flyweight (recognizeable by creational methods returning a cached instance, a bit the "multiton" idea)

Proxy (recognizeable by creational methods which returns an implementation of given abstract/interface type which in turndelegates/uses a different implementation of given abstract/interface type)


Behavioral patterns

Chain of responsibility (recognizeable by behavioral methods which (indirectly) invokes the same method inanother implementation of same abstract/interface type in a queue)

Command (recognizeable by behavioral methods in an abstract/interface type which invokes a method in an implementation of a different abstract/interface type which has been encapsulated by the command implementation during its creation)

Interpreter (recognizeable by behavioral methods returning a structurally different instance/type of the given instance/type; note that parsing/formatting is not part of the pattern, determining the pattern and how to apply it is)

Iterator (recognizeable by behavioral methods sequentially returning instances of a different type from a queue)

Mediator (recognizeable by behavioral methods taking an instance of different abstract/interface type (usually using the command pattern) which delegates/uses the given instance)

Memento (recognizeable by behavioral methods which internally changes the state of the whole instance)

Observer (or Publish/Subscribe) (recognizeable by behavioral methods which invokes a method on an instance of another abstract/interface type, depending on own state)

State (recognizeable by behavioral methods which changes its behaviour depending on the instance's state which can be controlled externally)

Strategy (recognizeable by behavioral methods in an abstract/interface type which invokes a method in an implementation of a different abstract/interface type which has been passed-in as method argument into the strategy implementation)

Template method (recognizeable by behavioral methods which already have a "default" behaviour definied by an abstract type)

Visitor (recognizeable by two different abstract/interface types which has methods definied which takes each the otherabstract/interface type; the one actually calls the method of the other and the other executes the desired strategy on it)


블로그 이미지

낭만가을

,

http://stackoverflow.com/questions/3541077/design-patterns-web-based-applications


번역은 천천히 할생각


accepted

A bit decent web application consists of a mix of design patterns. I'll mention only the most important ones.


Model View Controller pattern

The core (architectural) design pattern you'd like to use is the Model-View-Controller pattern. TheController is to be represented by a Servlet which (in)directly creates/uses a specific Model andView based on the request. The Model is to be represented by Javabean classes. This is often further dividable in Business Model which contains the actions (behaviour) and Data Model which contains the data (information). The View is to be represented by JSP files which have direct access to the (DataModel by EL (Expression Language).

Then there are variations based on how actions and events are handled. The popular ones are:

  • Request (action) based MVC: this is the simplest to implement. The (BusinessModel works directly with HttpServletRequest and HttpServletResponse objects. You have to gather, convert and validate the request parameters (mostly) yourself. The View can be represented by plain vanilla HTML/CSS/JS and it does not maintain state across requests. This is how among others Spring MVCStruts and Stripes works.

  • Component based MVC: this is harder to implement. But you end up with a simpler model and view wherein all the "raw" Servlet API is abstracted completely away. You shouldn't have the need to gather, convert and validate the request parameters yourself. The Controller does this task and sets the gathered, converted and validated request parameters in the Model. All you need to do is to define action methods which works directly with the model properties. TheView is represented by "components" in flavor of JSP taglibs or XML elements which in turn generates HTML/CSS/JS. The state of the View for the subsequent requests is maintained in the session. This is particularly helpful for server-side conversion, validation and value change events. This is how among others JSFWicket and Play! works.

As a side note, hobbying around with a homegrown MVC framework is a very nice learning exercise, and I do recommend it as long as you keep it for personal/private purposes. But once you go professional, then it's strongly recommended to pick an existing framework rather than reinventing your own. Learning an existing and well-developed framework takes in long term less time than developing and maintaining a robust framework yourself.

In the below detailed explanation I'll restrict myself to request based MVC since that's easier to implement.


Front Controller pattern (Mediator pattern)

First, the Controller part should implement the Front Controller pattern (which is a specialized kind of Mediator pattern). It should consist of only a single servlet which provides a centralized entry point of all requests. It should create the Model based on information available by the request, such as the pathinfo or servletpath, the method and/or specific parameters. The Business Model is called Action in the below HttpServlet example.

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        Action action = ActionFactory.getAction(request);
        String view = action.execute(request, response);

        if (view.equals(request.getPathInfo().substring(1)) {
            request.getRequestDispatcher("/WEB-INF/" + view + ".jsp").forward(request, response);
        }
        else {
            response.sendRedirect(view); // We'd like to fire redirect in case of a view change as result of the action (PRG pattern).
        }
    }
    catch (Exception e) {
        throw new ServletException("Executing action failed.", e);
    }
}

Executing the action should return some identifier to locate the view. Simplest would be to use it as filename of the JSP. Map this servlet on a specific url-pattern in web.xml, e.g. /pages/**.door even just *.html.

In case of prefix-patterns as for example /pages/* you could then invoke URL's likehttp://example.com/pages/registerhttp://example.com/pages/login, etc and provide /WEB-INF/register.jsp/WEB-INF/login.jsp with the appropriate GET and POST actions. The parts registerlogin, etc are then available by request.getPathInfo() as in above example.

When you're using suffix-patterns like *.do*.html, etc, then you could then invoke URL's likehttp://example.com/register.dohttp://example.com/login.do, etc and you should change the code examples in this answer (also the ActionFactory) to extract the register and login parts by request.getServletPath() instead.


Strategy pattern

The Action should follow the Strategy pattern. It needs to be defined as an abstract/interface type which should do the work based on the passed-in arguments of the abstract method (this is the difference with the Command pattern, wherein the abstract/interface type should do the work based on the arguments which are been passed-in during the creation of the implementation).

public interface Action {
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception;
}

You may want to make the Exception more specific with a custom exception like ActionException. It's just a basic kickoff example, the rest is all up to you.

Here's an example of a LoginAction which (as its name says) logs in the user. The User itself is in turn a Data Model. The View is aware of the presence of the User.

public class LoginAction implements Action {

    public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = userDAO.find(username, password);

        if (user != null) {
            request.getSession().setAttribute("user", user); // Login user.
            return "home"; // Redirect to home page.
        }
        else {
            request.setAttribute("error", "Unknown username/password. Please retry."); // Store error message in request scope.
            return "login"; // Go back to redisplay login form with error.
        }
    }

}

Factory method pattern

The ActionFactory should follow the Factory method pattern. Basically, it should provide a creational method which returns a concrete implementation of an abstract/interface type. In this case, it should return an implementation of the Action interface based on the information provided by the request. For example, the method and pathinfo (the pathinfo is the part after the context and servlet path in the request URL, excluding the query string).

public static Action getAction(HttpServletRequest request) {
    return actions.get(request.getMethod() + request.getPathInfo());
}

The actions in turn should be some static/applicationwide Map<String, Action> which holds all known actions. It's up to you how to fill this map. Hardcoding:

actions.put("POST/register", new RegisterAction());
actions.put("POST/login", new LoginAction());
actions.put("GET/logout", new LogoutAction());
// ...

Or configurable based on a properties/XML configuration file in the classpath: (pseudo)

for (Entry entry : configuration) {
    actions.put(entry.getKey(), Class.forName(entry.getValue()).newInstance());
}

Or dynamically based on a scan in the classpath for classes implementing a certain interface and/or annotation: (pseudo)

for (ClassFile classFile : classpath) {
    if (classFile.isInstanceOf(Action.class)) {
       actions.put(classFile.getAnnotation("mapping"), classFile.newInstance());
    }
}

Keep in mind to create a "do nothing" Action for the case there's no mapping. Let it for example return directly the request.getPathInfo().substring(1) then.


Other patterns

Those were the important patterns so far.

To get a step further, you could use the Facade pattern to create a Context class which in turn wraps the request and response objects and offers several convenience methods delegating to the request and response objects and pass that as argument into the Action#execute() method instead. This adds an extra abstract layer to hide the raw Servlet API away. You should then basically end up with zero import javax.servlet.* declarations in every Action implementation. In JSF terms, this is what the FacesContext and ExternalContext classes are doing. You can find a concrete example in this answer.

Then there's the State pattern for the case that you'd like to add an extra abstraction layer to split the tasks of gathering the request parameters, converting them, validating them, updating the model values and execute the actions. In JSF terms, this is what the LifeCycle is doing.

Then there's the Composite pattern for the case that you'd like to create a component based view which can be attached with the model and whose behaviour depends on the state of the request based lifecycle. In JSF terms, this is what the UIComponent represent.

This way you can evolve bit by bit towards a component based framework.



블로그 이미지

낭만가을

,

public class Page {

String name;// 이름

//user setting var

int itemTotalCount = 0; //아이템크기

int pageItemCount = 10; //페이지에 한번에 보여줄 아이템크기

int pageCount = 1; //페이지수 보여줄 개수

//user chioce var

int currentItem = 1; //현재 아이템 번호

public Page() {

}

public Page(String name) {

this.name=name;

}

public Page(String name, int itemTotalCount, int pageItemCount, int pageCount, int currentItem) {

setName(name);

setItemTotalCount(itemTotalCount);

setPageCount(pageCount);

setCurrentItem(currentItem);

}

public String getName() {

return name;

}

public Page setName(String name) {

this.name = name;

return this;

}

//아이템크기

public int getItemTotalCount() {

return itemTotalCount;

}

public Page setItemTotalCount(int itemTotalCount) {

if(itemTotalCount < 1){

itemTotalCount = 0;

}

this.itemTotalCount = itemTotalCount;

return this;

}

//페이지에 한번에 보여줄 아이템크기

public int getPageItemCount() {

return pageItemCount;

}

//페이지당 보여줄 아이템개수

public Page setPageItemCount(int pageItemCount) {

this.pageItemCount = pageItemCount;

return this;

}

//페이지수 보여줄 개수

public int getPageCount() {

return pageCount;

}

public Page setPageCount(int pageCount) {

this.pageCount = pageCount;

return this;

}

///////////////////////////settting

//현재 페이지 번호

public int getCurrentPage() {

int currentPage = (currentItem/pageItemCount) + ((currentItem%pageItemCount)>0?1:0); //현재위치 / 페이지에보여줄아이템수  + 나머지0이상있는경우 +1

int pageMax = getPageMax();

int pageMin = getPageMin();

if(currentPage>pageMax){

currentPage=pageMax;

}else if(currentPage < 1){

currentPage = pageMin;

}

return currentPage;

}

public Page setCurrentPage(int currentPage) {

currentItem = (pageItemCount*currentPage - 1) + 1; //바로전페이지로 이동후 +1로 아이템위치셋팅

int totalCont = getItemTotalCount();

if(currentItem>getItemTotalCount()){

currentItem = totalCont;

}else if(currentItem<1){

currentItem=0;

}

int pageMax = getPageMax();

int pageMin = getPageMin();

if(currentPage>pageMax){

currentPage=pageMax;

}else if(currentPage<1){

currentPage = pageMin;

}

return this;

}

//현재 아이템 번호

public int getCurrentItem() {

return currentItem;

}

public Page setCurrentItem(int currentItem) throws IllegalArgumentException {

if(currentItem>getItemTotalCount()){

throw new IllegalArgumentException("max over");

}

this.currentItem = currentItem;

return this;

}

//////auto------

// public int[] getPage() {

// return page;

// }

public int getPageMin() {

return 1;

}

public int getPageMax() {

int max = (itemTotalCount/pageItemCount) + ((itemTotalCount%pageItemCount)>0?1:0);

int pageMin = getPageMin();

if(max<1){

max = pageMin;

}

return max;

}

//현재 페이지의 아이템 최소값

public int getCurrentPageItemMin() {

return (getCurrentPage() - 1 ) * getPageItemCount() + 1; //전페이지 

}

//현재 페이지의 아이템 최대값

public int getCurrentPageItemMax() {

int itemTotatCount = getItemTotalCount();

int max = getCurrentPageItemMin()+ getPageItemCount() - 1;

if(max>itemTotatCount){

max = itemTotatCount;

}

return max;

}

//페이징 표시의   처음 페이지  값

public int getCurrentPageMin() {

int currentPage = getCurrentPage();

int currentPageMin = currentPage - (getPageCount() / 2);

if(currentPageMin < 1){

currentPageMin = 1;

}

return currentPageMin;

}

//페이징 표시의   최대 페이지  값

public int getCurrentPageMax() {

int pageMax = getPageMax();

int currentPageMin = getCurrentPageMin() + (getPageCount()) - 1;

if(currentPageMin>pageMax){

currentPageMin = pageMax;

}

return currentPageMin;

}

public void process(){

getName();

getItemTotalCount();

getPageItemCount();

getPageCount();

getCurrentPage();

getCurrentItem();

getPageMin();

getPageMax();

getCurrentPageMin();

getCurrentPageMax();

}


@Override

public String toString() {

process();

return "getName()=" + getName() + ", \r\n getItemTotalCount(총아이템수)="

+ getItemTotalCount() + ",\r\n getPageItemCount(한페이지당 표시 아이템수)="

+ getPageItemCount() + ",\r\n getPageCount(페이징쪽에 페이지표시 보여줄수)=" + getPageCount()

+ ",\r\n getCurrentPage(현재페이지)=" + getCurrentPage()

+ ",\r\n getCurrentItem(현재아이템위치)=" + getCurrentItem() + ",\r\n getPageMin()="

+ getPageMin() + ",\r\n getPageMax(마지막 페이지 번호)=" + getPageMax()

+ ",\r\n getCurrentPageMin(보여질페이지에 처음페이지 번호)=" + getCurrentPageMin()

+ ",\r\n getCurrentPageMax(보여질페이지에 마지막페이지 번호)=" + getCurrentPageMax()

+ ",\r\n getCurrentPageItemMin(현재페이지 처음 아이템 번호)=" + getCurrentPageItemMin()

+ ",\r\n getCurrentPageItemMax(현재페이지 마지막 아이템 번호)=" + getCurrentPageItemMax()

;

}

// public static void main(String[] args) {

// Page page = new Page();

// page.setItemTotalCount(94);//총개수

// //page.setPageCount(1); //페이지수 보여질수

// page.setPageItemCount(10);

// page.setCurrentPage(1);

// System.out.println(page);

// }

}

'프로그래밍 > JAVA ' 카테고리의 다른 글

Rxjava 는 무엇인가?  (0) 2018.06.13
블로그 이미지

낭만가을

,